TheWebs TheWebs - 1 month ago 7
PHP Question

How to create two columns PHP?

I am having the worst time trying to create this, it should be simple. I have an

$info
array that consists of
$person=>$personInfoArray
and I am trying to say, for the first six, do one column, for the next x amount, do another column.

What I have is:

$infoCounter = 0;
foreach($info as $person => $information){
$infoCounter++;
echo '<tr>';
if($infoCounter <= 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}elseif($infoCounter > 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}
echo '</tr>';
}


I am essentially trying to create a table that looks like:

Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545 Name 9785
Some One else 54212 Something else 44121


But I am getting:

Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545
Name 9785
Some One else 54212
Something else 44121


Thoughts? This should be ridiculously easy.

Answer

See that you are creating a <tr> for each person, try doing something like this:

$infoCounter = 0;
foreach($info as $person => $information){
    if ($infoCounter % 2 == 0) {
        echo '<tr>';
    }
    echo '<td>'.$person.'</td>';
    echo '<td>'.$information['Extension'].'</td>';
    if ($infoCounter % 2 == 0) {
        echo '</tr>';
    }
    $infoCounter++;
}

Check that % its the modulus