Click to See Complete Forum and Search --> : Output Data to a Table


scottyrob
04-03-2006, 12:41 PM
Hi how would i go about doing this? I would like the data from my datbase being put into a table so that i have 2 rows of 5. The data that would be in each cell is just an Image and a name, underneath each other if possible... My current code is below, but that just outputs the data into a list...


<?
require 'db_connect.php';

echo "<link rel='stylesheet' type='text/css' href='css/main.css' />";

$start_year = $_GET["s"];
$end_year = $_GET["e"];

$query = "SELECT * FROM PLATE_BG_CHRISTMAS WHERE PROD_DATE >= '".$start_year."' and PROD_DATE <= '".$end_year."'";
$result = mysql_query($query) or die("Error: " . mysql_error() . " with query $query");

if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

while ($row = mysql_fetch_array($result)) {

echo "<div class='position'>";
echo "<img src=\"IMAGES/PLATE_BG_CHRISTMAS/{$row['IMAGE']}\" / width='50' height='50' />"; echo "<br />";
echo $row['PROD_DATE'];
echo "</div>";
}

mysql_free_result($result);
?>

intrivious
04-03-2006, 12:52 PM
Maybe something like this:

$counter = 0;
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
if ($counter%5==0)
echo "<tr>\n";

echo "<td><img src='IMAGES/PLATE_BG_CHRISTMAS/".$row['IMAGE']."' width='50' height='50' />";
echo "<br />".$row['PROD_DATE']."</td>\n";

if ($counter%5==0)
echo "</tr>\n";

$counter++;
}
echo "</table>\n";

scottyrob
04-03-2006, 01:30 PM
Hi, thanks for that, It half works now... I get this... (where XX is a set of data)

XX

XX XX XX XX

XX

XX XX XX XX

intrivious
04-03-2006, 04:10 PM
Ahhh, sorry about that.... should be:

$counter = 0;
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
if ($counter%5==0)
echo "<tr>\n";

echo "<td><img src='IMAGES/PLATE_BG_CHRISTMAS/".$row['IMAGE']."' width='50' height='50' />";
echo "<br />".$row['PROD_DATE']."</td>\n";

if ($counter%5==4) // <-----
echo "</tr>\n";

$counter++;
}
echo "</table>\n";


You could set a variable to the number of elements (pictures/description in your case) per row you want it to display. This makes it a little easier to change:
$perRow = 5;
$counter = 0;
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
if ($counter%$perRow==0)
echo "<tr>\n";

echo "<td><img src='IMAGES/PLATE_BG_CHRISTMAS/".$row['IMAGE']."' width='50' height='50' />";
echo "<br />".$row['PROD_DATE']."</td>\n";

if ($counter%$perRow==$perRow-1) // <-----
echo "</tr>\n";

$counter++;
}
echo "</table>\n";