Jump to content

PHP Table Loop Issue


jason360

Recommended Posts

Hey guys,

 

I am using php loop to echo out all off the data in my database category within a html table. However I would like to reduce each table row to only 5 results then start a new row so that the table fits within the webpage.  Any suggestions?  Any help is much appreciated.  Thanks.

 

Jason

 

Here is what i am using, but does not fit within the page.

 

<table cellspacing="10">
<tr>
<?php
$query = mysql_query('SELECT photo_id, photo_name FROM gallery_photos WHERE photo_category = "Books" AND photo_approved = 1 ORDER BY rand()');

while ($row = mysql_fetch_array($query))

				{

				echo "<td><a href='http://www.mysite.com/view.php?pid=".$row['photo_id']."'><img src='photos/".$row['photo_id'].".jpg' width='115' height='115' /><center><h3><b>".$row['photo_name']."</b></h3></center></a></td>";	
				}
?>
</tr>
</table>

Link to comment
Share on other sites

<table cellspacing="10">
<tr>
<?php
$query = mysql_query('SELECT photo_id, photo_name FROM gallery_photos WHERE photo_category = "Books" AND photo_approved = 1 ORDER BY rand()');
$i = 1;
while ($row = mysql_fetch_array($query))
{
	if($i % 5 === 0)
	{
		echo '</tr><tr>';
	}
	echo "<td><a href='http://www.mysite.com/view.php?pid=".$row['photo_id']."'><img src='photos/".$row['photo_id'].".jpg' width='115' height='115' /><center><h3><b>".$row['photo_name']."</b></h3></center></a></td>";
	$i++;
}
?>
</tr>
</table>

 

Link to comment
Share on other sites

Here's a commented version of the above code using arrays instead of a database result, but the process is the same.

 

<?php 

// Make dummy array
$array = range(0,32);

// This will keep track of how many rows we've echo'd
$i = 0;
// This tells us how many columns
$cols = 3;
$total = count( $array );

echo '<table><tr>';
foreach( $array as $value ) {
// Echo the value in a cell
echo '<td style="border:1px solid black;">'.$value.'</td>';
// Increase our counter by 1 before we check
$i++;
// Check to see if we need to start a new row by using the modulus operator
// It'll give us the remainder of the two numbers divided.
// We check if $i != $total to make sure we don't have a redundant empty row
// at the end
if( ($i % $cols) == 0 && $i != $total ) echo '</tr><tr>'; // Start a new row
}
// Now after the foreach, if we have an odd number of results, we'll have an
// extra empty column we need to fill, we can do this automatically
while( ($i % $cols) != 0 ) {
echo '<td> </td>';
$i++;
}
echo '</tr></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.