unidox Posted June 25, 2008 Share Posted June 25, 2008 I am having some trouble. I am trying to make a gallery that shows the images 5 acrosse, and unlimited down. So like: Image Image Image Image Image Image Image Image Image Image Image Image Like that. But I dont know how to use a while loop, the only way I use with while displays only 1 column. How do I do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/111910-tables-and-while/ Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 $cols = 1; while (blah) { if ($cols == 5) { echo "<br />"; $cols = 1; } // display image here $cols++; } Quote Link to comment https://forums.phpfreaks.com/topic/111910-tables-and-while/#findComment-574415 Share on other sites More sharing options...
Jabop Posted June 25, 2008 Share Posted June 25, 2008 <table> <tr> <? $x=0; while ($true) { ?> <td>Stuff Here</td> <? if ($x%5) { ?> </tr><tr> <? } $x++; } // end while ?> </tr> </table> Haven't tested it but it should work Quote Link to comment https://forums.phpfreaks.com/topic/111910-tables-and-while/#findComment-574416 Share on other sites More sharing options...
corbin Posted June 25, 2008 Share Posted June 25, 2008 Edit: Beaten by 2 people.... Teach me to go afk lol.... Jabop, where is $true set? Crayon, wouldn't that do 4 a row? --------------------------------------------------- You want a table (well... easiest way to do it) with 5 images a row? psuedo-code: i = 0; c = number of rows if(c > 0) { open table open row while(you have results from database) { if(i % 5 == 0) { close row open row } put table td } if(c % 5) { //the last row didn't finish.... if that makes sense offset = 5 - c % 5; for(i = 0; i < c; ++i) { output an empty <td></td> } } close row close table } Quote Link to comment https://forums.phpfreaks.com/topic/111910-tables-and-while/#findComment-574421 Share on other sites More sharing options...
unidox Posted June 25, 2008 Author Share Posted June 25, 2008 Crayon Violent, your code works, but if there isint all 5, it doesnt close the row. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/111910-tables-and-while/#findComment-574428 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 well my code didn't actually have tables added into it I was just showing you how to move to another row. Jabop's code shows you that though. Basically you open the table and first row before the loop. Inside the loop, if condition is true, you close the previous row and open a new row. Once the loop is done, you simply close the last row and table. Quote Link to comment https://forums.phpfreaks.com/topic/111910-tables-and-while/#findComment-574435 Share on other sites More sharing options...
Jabop Posted June 25, 2008 Share Posted June 25, 2008 Jabop, where is $true set? I meant whatever his condition is in his while loop. Quote Link to comment https://forums.phpfreaks.com/topic/111910-tables-and-while/#findComment-574441 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.