How Do You Use Php To Display A Two Columns Per Row Html Table?

December 28th, 2009 by CTO WebOjO.com website designers Lahore Pakistan Leave a reply »

$arr=array(”apple”,”orange”,
“banana”,”grape”,
“pineapple”);
I want to display the array in a html table using php (foreach loop preferred), two columns per row like this:
apple orange
banana grape
pineapple
Many thanks to you all.

Advertisement

3 comments

  1. Gavin says:

    < ?php
    $arr = array("apple","orange","banana","grape",…
    $n = count($arr);
    $q = ceil($n /2);
    echo '

    ‘;
    $arrN = 0;
    for($i=$q;$i< =$n;$i++) {
    echo "

    “;
    $arrN++;
    }
    echo ““;
    ?>

  2. just "JR" says:

    A bit basic, is it not?
    < ?php
    $arr = array ("apple", "orange", "banana", "grape", "pineapple");
    $txt = "

  3. $arr[$arrN] “;
    $arrN++;
    echo “$arr[$arrN]
    “;
    $siz = sizeof ($arr);
    $col = 0;
    for ($i=0; $i< $siz; $i++)
    {
    if ($col == 0)
    $txt .= "
    “;
    $txt .= “

    “;
    $col++;
    if ($col == 2)
    {
    $col = 0;
    $txt .= ““;
    }
    }
    if ($col != 0)
    $txt .= ““;
    $txt .= ““;
    echo ($txt);
    ?>

  4. ▐▀▀♦▀▀▌ ♦Oprah♦ ▐▄▄♦▄▄▌ says:

    $a = $arr;
    if (count($a) % 2 != 0) $a[] = ”; // Pad to an even # of elements
    echo ‘

  5. “.$arr[$i].”
    ‘;
    for ($i=0; $i echo '

    ‘;
    echo ‘

    ‘, $a[$i], ‘ ‘, $a[$i+1], ‘

    ‘;