TwiztedCupid Posted October 15, 2007 Share Posted October 15, 2007 I'm still learning PHP and need some help. I have a code that reads a page for a top 20 ranking type thing. The code works great but now I'm trying to build a table with it. Once it reads 5 names, I want it to start an new cell. Here's what I have so far. $count = 1; echo "<table><tr><td>"; foreach ($matches as $match){ $rank = "$match[1]. $match[2]"; if ($count <= 5) { echo ($rank . "</a><br />"); $count = $count+1; } } echo "<td>"; echo "</tr></table>" It does exactly what it's supposed to, so far. The problem is, once it reads the first or any other group of 5 names, I need it to insert <td> and and then continue with the next group of 5 names. If I was to write it out normal, this would be my results. <table> <tr> <td>1<br> 2<br> 3<br> 4<br> 5</td> <td>6<br> 7<br> 8<br> 9<br> 10</td> <td>11<br> 12<br> 13<br> 14<br> 15</td> <td>16<br> 17<br> 18<br> 19<br> 20</td> </tr> </table> Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/73371-solved-create-table-with-foreach-statement/ Share on other sites More sharing options...
BlueSkyIS Posted October 15, 2007 Share Posted October 15, 2007 if ($count <= 5) { echo ($rank . "<br />"); // Don't know what you're doing with the /a in there. $count = $count+1; } else { echo ("</TD><TD>".$rank . "<br />"); $count = 1; } or similar... Quote Link to comment https://forums.phpfreaks.com/topic/73371-solved-create-table-with-foreach-statement/#findComment-370170 Share on other sites More sharing options...
TwiztedCupid Posted October 15, 2007 Author Share Posted October 15, 2007 Thank you for the help. First of all the /a is there for part of my pregmatch code. Second of all THANK YOU!! I had to change the count to $count =2; for it to display exactly how I wanted it to. Here's the whole code. $count = 1; echo "<table><tr><td>"; foreach ($matches as $match){ $rank = "$match[1]. $match[2]"; if ($count <= 5) { echo ($rank . "</a><br />"); // Don't know what you're doing with the /a in there. $count = $count+1; } else { echo ("</TD><TD>".$rank . "</a><br />"); $count = 2; } } echo "</tr></table>" But everything works just fine. Quote Link to comment https://forums.phpfreaks.com/topic/73371-solved-create-table-with-foreach-statement/#findComment-370189 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.