Caio Ferrari Caio Ferrari - 1 month ago 6
PHP Question

Get PHP array values and print in a loop

I have a table with 9 rows. The first column should print the name of each participant of the ranking. First of, I did an array containing the names of them:

$names = array("Mike", "Kyle", "Johnny", "Will", "Vasques");


For this task I coded a loop with 5 rows and 10 columns. So, as I mentioned, the first column should print the name of each one.

for($x=1; $x<=count($names); $x++) {
echo "<tr>";
for($td=1; $td<=10; $td++) {
echo "<td></td>";
}
echo "</tr>";
}


As you can see, there is a loop for
<tr>
and a loop inside for
<td>
! How do I print each name in the first row?

Answer
$names = array("Mike", "Kyle", "Johnny", "Will", "Vasques");
for($td=0; $td<=9; $td++) {
  echo "<tr>";
  if ($td == 0) {
    foreach ($names as $name) {
      echo "<td>$name</td>";
    }
  }
  echo "<td></td>";
  echo "</tr>";
}
Comments