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
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>";

}
?>

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.