Limit Number of Row Items in Dynamically Populated Table - PHP, MySQL
I'm trying to display the r开发者_如何学Goesults of an image search in a table with rows that are no more than 6 items across. How do I get the table to automatically create a new row once that limit is reached? For now, I just have a very simple table that will put all images in one column:
while($info = mysql_fetch_array( $data ))
{
echo "<table id=table1>";
echo "<tr><td>";
echo "<img src=http://www.website.com/pictures/".$info['photo'] ."> <br>";
echo "<a href=http://www.website.com/pictures/".$info['photo'] . " >Full Quality </a><br> ";
echo "</td></tr>";
}
Thanks
You could do something like this with the modulus operator and check to see if your counter is divisible by 6. You will have to do some logic to see if that ending is actually needed after the row.
$counter = 1;
echo "<table id=table1><tr>";
while($info = mysql_fetch_array( $data ))
{
if ($counter % 6==0) {
echo "</tr>";
echo "<tr>"
}
echo "<td>";
echo "<img src=http://www.website.com/pictures/".$info['photo'] ."> <br>";
echo "<a href=http://www.website.com/pictures/".$info['photo'] . " >Full Quality </a><br> ";
echo "</td>";
$counter++;
}
echo "</tr>"
i tried this approach in order to have 3 items/row but i have nothing after the second row (i have 8 items).
while($res = mysql_fetch_array( $cat ))
{
echo "<tr height=\"40\">";
for($i=0; $i < 3; $i++){
echo "<td>";
echo "<a href= 'http://www....org/".$res['tipologie']."'>"; echo $res['tipologie']; echo "</a>";
echo "</td>";
if(!$res = mysql_fetch_array( $cat )){
break;
}
}
echo "</tr>";
}
echo "</table>";
}
You can do something like this:
echo "<table id=table1>";
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
for($i=0; $i < 6; $i++){
echo "<td>";
echo "<img src=http://www.website.com/pictures/".$info['photo'] ."> <br>";
echo "<a href=http://www.website.com/pictures/".$info['photo'] . " >Full Quality </a><br> ";
echo "</td>";
if(!$info = mysql_fetch_array( $data )){
break;
}
}
echo "</tr>";
}
echo "</table>";
Basic idea here is that you just need a loop inside of your loop to keep track of when you've reached 6 items and start a new row accordingly.
Also as a commenter pointed out, be sure to open your table at the appropriate place, and also be sure to close it. I tried to give an example of that in the code below.
精彩评论