Displaying Tables in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
McBeast
Forum Newbie
Posts: 3
Joined: Wed Mar 09, 2011 7:21 am

Displaying Tables in PHP

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying Tables in PHP

Post 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>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
McBeast
Forum Newbie
Posts: 3
Joined: Wed Mar 09, 2011 7:21 am

Re: Displaying Tables in PHP

Post 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.
tnrh1
Forum Newbie
Posts: 14
Joined: Tue Nov 30, 2010 8:27 am

Re: Displaying Tables in PHP

Post 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>
McBeast
Forum Newbie
Posts: 3
Joined: Wed Mar 09, 2011 7:21 am

Re: Displaying Tables in PHP

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying Tables in PHP

Post 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>';
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply