Jump to content

For every 5 rows, use <tr> in table


slyte33

Recommended Posts

Hi, what i'm trying to figure out what to do, after googling and no answers, is for every 5 rows, i'd like to start a new row in a table.

 

Short Example:

echo "<table><tr>";
select * from example
while loop
{
for every 5:
echo "<tr>";
echo "<td>";
echo "example, this line will show 5 times per row, without having to do 'limit 5' in every query";
echo "</td>";
echo "</tr>";
}
echo "</table>";

 

I hope that explains it.

Thanks :)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/183597-for-every-5-rows-use-in-table/
Share on other sites

just add an incremental value in there, and an IF statement to handle things when the value is reached:

 

echo "<table><tr>";
select * from example
$i = 1;
while loop
{
//for every 5:
echo "<td>";
echo "example, this line will show 5 times per row, without having to do 'limit 5' in every query";
echo "</td>";
if ($i == 5): echo '</tr><tr>'; endif;
$i++;
}
echo "</tr></table>";

 

something very, very basic, but that should give you something to play with.

just add an incremental value in there, and an IF statement to handle things when the value is reached:

 

echo "<table><tr>";
select * from example
$i = 1;
while loop
{
//for every 5:
echo "<td>";
echo "example, this line will show 5 times per row, without having to do 'limit 5' in every query";
echo "</td>";
if ($i == 5): echo '</tr><tr>'; endif;
$i++;
}
echo "</tr></table>";

 

something very, very basic, but that should give you something to play with.

 

I tried it, but it only does it one time. Should I just do this:

 

if ($i == 5): echo '</tr><tr>'; endif;

if ($i == 10): echo '</tr><tr>'; endif;

if ($i == 15): echo '</tr><tr>'; endif;

if ($i == 20): echo '</tr><tr>'; endif;

 

...and so on? I only ask because I only need to use about 100 rows.

 

Thanks for the feedback :)

just add an incremental value in there, and an IF statement to handle things when the value is reached:

 

echo "<table><tr>";
select * from example
$i = 1;
while loop
{
//for every 5:
echo "<td>";
echo "example, this line will show 5 times per row, without having to do 'limit 5' in every query";
echo "</td>";
if ($i == 5): echo '</tr><tr>'; endif;
$i++;
}
echo "</tr></table>";

 

something very, very basic, but that should give you something to play with.

 

I don't think this will work will it? My expertise isn't php but my advice would be to increment the counter variable like he is doing in a loop and instead of putting the part where he has:

if($i==5)

 

put a mod operator

if($i%5==0)

Hi

 

I would be tempted to use something like this, using mod (ie, %) to check whether it is the fifth row or not.

 

echo "<table><tr>";
select * from example
$i = 1;
while loop
{
if (($i % 5) == 0)
{
	echo "<tr>";
	echo "<td>";
	echo "example, this line will show 5 times per row, without having to do 'limit 5' in every query";
	echo "</td>";
	echo "</tr>";
}
$i++;
}
echo "</table>";

 

All the best

 

Keith

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.