How to display two table columns per row in php loop

foreach, html, html-table, loops, php

Solution

$i=0;
foreach ($x as $key=>$value)
  {
  if (fmod($i,2)) echo '<tr>';
  echo '<td>',$value,'</td>';
  if (fmod($i,2)) echo '</tr>';
  $i++;
  }

this will output TR (row) each second time

ps: i haven't tested the code, so maybe you will need to add ! sign before fmod, if it doesn't output TR on first iteration, but on second iteration in the beginning...

Problem

I would like to display data, two columns per row during my foreach. I would like my result to look like the following: ``` <table> <tr><td>VALUE1</td><td>VALUE2</td></tr> <tr><td>VALUE3</td><td>VALUE4</td></tr> <tr><td>VALUE5</td><td>VALUE6</td></tr> </table> ``` Any help would be greatly appreciated.

Original source