Jump to content

nicely displaying output from database


robcrozier

Recommended Posts

hi all,

 

what i'm trying to do is display some output from a database in a table until there are no more records left. The data being displayed is actually a link to an image, so images are therefore outputted in the table. I can quite easily get the table to display one '<tr><td>$content</td></tr>' above another until there are no more records, however i would like to display 3 pictures in a row before returning to the next. so for example:

 

<tr><td>$content</td><td>$content</td><td>$content</td></tr>

repeat until no more data....

 

However... i just keep getting a pile of garbage. it outputs far too many rows!

 

Please help!

Link to comment
https://forums.phpfreaks.com/topic/40708-nicely-displaying-output-from-database/
Share on other sites

	

<?php 
$query = "SELECT * FROM table";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

	echo "<br><br><b>Let's fetch data...</b><br>";

	echo "<table border='1' width='98%'>\n";

	echo "<tr>
	<th>HEADER1</th>
	<th>HEADER2</th>
	<th>HEADER3</th>
	<th>HEADER4</th>
		  </tr>";

		$count = 0;

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

	echo "\t<tr>\n";

foreach ($line as $col_value) {

	echo "\t\t<td>$col_value</td>\n";

}

		$count++;

	echo "\t</tr>\n";

}

	echo "</table>\n";

if ($count < 1) {

	echo "<br><br>No rows were found in this table.<br><br>";

} else {

	echo "<br><br>".$count;
	echo " rows were found in this table.<br><br>";

}
?>

You could display them like this. Set the column variable to whatever you want:

 

<?php
// display results
$totalColumns = 3;
$i = 1;
echo "<table border='0' cellpadding='2'>";
while ($row = mysql_fetch_array($results)){
if ($i == 1) echo "<tr>";
echo "<td>";
echo "your_image_reference_here";
echo "</td>";
if ($i == $totalColumns)
	{
	echo "</tr>";
	$i = 0;
	}
$i++;
}
echo "</table>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.