Sorry if this post is confusing. <ul> <li><div class="box">text</div></li> <li><div class="box">text</div></li> <li><div class="box">text</div></li> </ul> HTML: I've got 3 boxes on each row on my site design, coded like the above. Problem is I can just echo the content from the database out into the box because the coding wouldn't be right, because for every <ul></ul> there has to be three box codes within (see above code). So I need to show <ul> before the 1st, 4th, 7th row etc. and show </ul> after the 3rd, 6th, row etc I hope you understand that confusing question, all help appreciated.
try something like : $i=1; query here while (parse it) { if ($i%3==0) {echo '</ul>';} if ($i%4==0 || $i==1) {echo '<ul>';} echo the rest $i++; } Code (markup): Not sure if the above is error free but you'll get the idea
even shorter (found a mistake): $i=1; $result=mysql_query("select * from yourtable where something = something"); while ($row=mysql_fetch_assoc($result)) { if ($i%3==0) {echo '</ul><ul>';} if($i==1){echo '<ul>';} echo '<li><div class="box">'.$row["thetext"].'</div></li>'; $i++; } Code (markup):
Thanks, when I had 5 rows in a database though, it showed <ul> before 2 boxes and </ul> after, instead of 3, the next 3 boxes it showed <ul> to start but didn't end the </ul>
Just play with $i=1 ($i=0?) and make sure you end it with </ul> like : $i=1; $result=mysql_query("select * from yourtable where something = something"); while ($row=mysql_fetch_assoc($result)) { if ($i%3==0) {echo '</ul><ul>';} if($i==1){echo '<ul>';} echo '<li><div class="box">'.$row["thetext"].'</div></li>'; $i++; } echo '</ul>'; Code (markup): Note that it might close the tag twice but it will work.