Page 1 of 1

Displaying Tables in PHP

Posted: Wed Mar 09, 2011 7:34 am
by McBeast
I am still kind of new to php, but do understand it quite well. I have been using the while loop to turn rows of mysql data into great looking html tables, but now I need to change it for a new project. I want to make a row of a database into a cell of a table that spreads three or four columns wide, breaks and repeats. here is an example of how I am used to doing it
<table>
<?php
while($rows = mysql_fetch_array($query))
echo '<tr><td>'.$rows['1'].'</td><td>'.$rows['2'].'</td></tr>';
</table>
as you can see the query terminates the table row, but I need to put three or four rows of database data in and then terminate the row. I can't find much help on this, or maybe I am going at this from the wrong angle. Any help is appreciated.

Re: Displaying Tables in PHP

Posted: Wed Mar 09, 2011 8:57 am
by social_experiment
Im not sure what you mean with the question but the code below is appended to how the while loop should be used. Do you want to display only 3 (or 4) rows out of the database?

Code: Select all

<table>
<?php 
while($rows = mysql_fetch_array($query)) {
 echo '<tr><td>'.$rows['1'].'</td><td>'.$rows['2'].'</td></tr>';
}
?>
</table>

Re: Displaying Tables in PHP

Posted: Wed Mar 09, 2011 9:45 am
by McBeast
Thanks for putting in the curly brackets for me, I am not sure yet which I will want, I'm not that far with it yet. The example I showed goes with a query like SELECT * FROM products, and the entire table row would be on one product. but now I want to do a grid of products, where instead of the query creating 15 rows of one product each, it would create 5 rows of 3 products each.

Re: Displaying Tables in PHP

Posted: Wed Mar 09, 2011 10:57 am
by tnrh1
The code is defentlly not perfect but this the best I could do:

Code: Select all

<table border="1">
</tr>
<?php 
$k=1;
while($row = mysql_fetch_array($result)) 
{
		if($k==3)
		{
				echo '<td>';
				echo $row["name"];
				echo '</td></tr><tr>';
		}
		else
				if($k==2)
				{
						echo '<td>';
						echo $row["name"];
						echo '</td>';
				}
				else
				{ 
						echo '<td>';
						echo $row["name"];
						echo '</td>';
				}
		if ($k==3)
				$k=1;
		else		
				$k=$k+1; 
}	 
?> 
</table>
</table>

Re: Displaying Tables in PHP

Posted: Wed Mar 09, 2011 12:22 pm
by McBeast
OK, that looks interesting, never thought of it that way but I see where you're going with it. I will try it and let you know how it works out.

Re: Displaying Tables in PHP

Posted: Wed Mar 09, 2011 12:26 pm
by social_experiment
You have the right idea but should use a for loop instead. The loop does X iterations, in your case 5. Each time the value of $i divided by 5 and if the remainder is 0, a closing and opening <tr> element is writing. The modulo operator (%) finds the remainder of division of one number by another (sic Wikipedia).

Code: Select all

echo '<tr>';
for ($i = 1; $i <= 5; $i++) 
 // do something
if ($i % 5 == 0) {echo '</tr><tr>';	}
}
echo '</tr>';