Jump to content

[SOLVED] Create table with foreach statement


TwiztedCupid

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.