I am creating a PHP page where the page prints out the whole of a database I have of meat packaging. I am printing the results of the database in order of their shape (Rectangular, Oval, Square, Insert). I can happily get the database to print out the results but what I want to do for one of the columns is change the result that is printed into a HTML link.
This column is ‘Imagename’, it prints the name of a JPEG that is stored on the server. I want it so that when the result for ‘Imagename’ is printed with the JPEG on the side it isn’t text but actually a link to a new tab that displays the picture for the user to view.
Here is my current PHP that loops through the database printing it in a table:
<?php
$con = mysql_connect("localhost", "horizon1", "");
mysql_select_db("horizon1_delyn", $con);
if ( ! $con)
{
die("Could not connect: " . mysql_error());
}
$rectangular = mysql_query("SELECT toolcode, description, traysize, imagename FROM range WHERE trayshape ='rectangular' ORDER BY traysize");
$oval = mysql_query("SELECT toolcode, description, traysize, imagename FROM range WHERE trayshape ='oval' ORDER BY traysize");
$square = mysql_query("SELECT toolcode, description, traysize, imagename FROM range WHERE trayshape ='square' ORDER BY traysize");
$insert = mysql_query("SELECT toolcode, description, traysize, imagename FROM range WHERE trayshape ='insert' ORDER BY traysize");
$fields_num = mysql_num_fields($rectangular);
echo "<table width='97%' border='0' cellspacing='0' cellpadding='0'><tr>";
//*RECTANGULAR*//
for ($i = 0; $i < $fields_num; $i++)
{
$field = mysql_fetch_field($rectangular);
echo "<td bgcolor='#E8CF24' style='font-family: arial;font-size: 12px;'>{$field->name}</td>";
}
echo "</tr>\n";
while ($row = mysql_fetch_row($rectangular))
{
echo "<tr>";
foreach ($row as $cell)
echo "<td style='font-family: arial;font-size:12px'>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($rectangular);
//* OVAL *//
for ($i = 0; $i < $fields_num; $i++)
{
$field = mysql_fetch_field($oval);
echo "<td bgcolor='#E8CF24' style='font-family: arial;font-size: 12px'>{$field->name}</td>";
}
echo "</tr>\n";
while ($row = mysql_fetch_row($oval))
{
echo "<tr>";
foreach ($row as $cell)
echo "<td style='font-family: arial;font-size:12px'>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($oval);
//* SQUARE *//
for ($i = 0; $i < $fields_num; $i++)
{
$field = mysql_fetch_field($square);
echo "<td bgcolor='#E8CF24' style='font-family: arial;font-size: 12px'>{$field->name}</td>";
}
echo "</tr>\n";
while ($row = mysql_fetch_row($square))
{
echo "<tr>";
foreach ($row as $cell)
echo "<td style='font-family: arial;font-size:12px'>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($square);
//* INSERT *//
for ($i = 0; $i < $fields_num; $i++)
{
$field = mysql_fetch_field($insert);
echo "<td bgcolor='#E8CF24' style='font-family: arial;font-size: 12px'>{$field->name}</td>";
}
echo "</tr>\n";
while ($row = mysql_fetch_row($insert))
{
echo "<tr>";
foreach ($row as $cell)
echo "<td style='font-family: arial;font-size:12px'>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($insert);
?>
Does anyone have any idea how I could in a sense isolate the ‘Imagename’ column to make the images clickable? Thanks for your help?
First of all, they are going to rip you to shreds for not escaping, but I’ll let someone else tell you off for that.
At the minute you are outputting everything from the table in one go, you need to split it up, so instead of
You will want something like
You need to echo the values individually, and then wrap an
<a>tag around the hyperlink, with a href