XeroXer Posted February 1, 2007 Share Posted February 1, 2007 Hi! I am using a foreach command to list my image gallery. The problem is I want the images in a table but don't know how to make it enter a new <tr>. Can I do so that after three images in my foreach it echos </tr><tr>? Quote Link to comment https://forums.phpfreaks.com/topic/36609-solved-foreach-special-entry-after-three-times/ Share on other sites More sharing options...
yzerman Posted February 1, 2007 Share Posted February 1, 2007 you would have to code it so it prints 3 images for each loop it does, and put the end table row after the 3rd image. Quote Link to comment https://forums.phpfreaks.com/topic/36609-solved-foreach-special-entry-after-three-times/#findComment-174415 Share on other sites More sharing options...
JasonLewis Posted February 1, 2007 Share Posted February 1, 2007 you can. outside your foreach add a $count variable and set it to 0. then inside your loop have this: if($count == 3){ $count = 0; echo "</tr><tr>"; } hopefully that works. Quote Link to comment https://forums.phpfreaks.com/topic/36609-solved-foreach-special-entry-after-three-times/#findComment-174418 Share on other sites More sharing options...
bibby Posted February 1, 2007 Share Posted February 1, 2007 naw, then if he had exactly 9 images, you're left with an extra tr. Use a ticker still though $tick=0; foreach ($images as $img) { $td.="\t<td>".$img."<td>\n"; $tick++; if($tick==3) { $rows.="<tr>".$td."</tr>\n"; unset($td); $tick=0; } } if ($td) // left over images $rows.="<tr>".$td."</tr>\n"; $table= "<table>\n".$rows."</table>\n"; echo $table; edit: my spelling edit2: reset my tick Quote Link to comment https://forums.phpfreaks.com/topic/36609-solved-foreach-special-entry-after-three-times/#findComment-174421 Share on other sites More sharing options...
redarrow Posted February 1, 2007 Share Posted February 1, 2007 Little snippet to help u ok, For the formating of html with php useing <tables> <td> <tr> in a foreach and array. pic to add should be easy ok. <html> <head> </head> <title>redarrow example</title> <table bordercolor="black" border="4"> <?php $array_one=array("hello there i am","redarrow"); $array_two=array("i love","php"); foreach($array_one as $one){ echo"<tr><td>"; echo $one; echo"</tr></td>"; } foreach($array_two as $two){ echo"<tr><td>"; echo $two; echo"</tr></td>"; } ?> </table> </html> Quote Link to comment https://forums.phpfreaks.com/topic/36609-solved-foreach-special-entry-after-three-times/#findComment-174422 Share on other sites More sharing options...
redarrow Posted February 1, 2007 Share Posted February 1, 2007 even an easer way ok <html> <head> </head> <title>image example</title> <table bordercolor="black" border="4"> <?php $images="image_folder/"; $array_one=array("img1.gif","img2.gif","img3.gif"); foreach($array_one as $one){ echo"<tr><td>"; echo "<img src='$images/$one'></img>"; echo"</tr></td>"; } ?> </table> </html> Quote Link to comment https://forums.phpfreaks.com/topic/36609-solved-foreach-special-entry-after-three-times/#findComment-174428 Share on other sites More sharing options...
XeroXer Posted February 1, 2007 Author Share Posted February 1, 2007 Thanks all! I used a edited version of ProjectFear's idea. At the top of the page I set $count to 0 <?php $count = "0"; ?> At the start of my foreach I check the $count and if it is 3 I enter the <tr> <?php foreach ($bilder as $bilderna) { if($count == 3){ $count = 0; echo "</tr><tr>"; echo "\n"; } else { echo ""; } ?> After the image code is entered I increse the $count by 1. <?php echo "<td><a href='$sokvag' target='_blank'><img src='$thumbsokvag' border='0' alt='$bilderna / $filstorlek bytes'></a><br>"; echo $bilderna . " - " . $filstorlek . " bytes</td>"; $count++; ?> And after the whole foreach section I make an echo out </tr> because that will always be needed because the other </tr> adding is at the beginning of the foreach. <?php echo "</tr>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/36609-solved-foreach-special-entry-after-three-times/#findComment-174453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.