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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.