tags:

views:

79

answers:

6

Hi,

I have

  foreach ($a as $key => $value) {
     echo "<DIV id='container'>
     <DIV id='middle'>$value</DIV>
     </DIV>";
     //echo "<br />$key:$value<br />\n";
  }

which displays result one below other, like

1234
5678
2010-05-20
5678
1590
2010-05-19

but i want it in a table like structure like

1234 5678 2010-05-20
5678 1590 2010-05-19

how can i do that?

+1  A: 

You could keep track with for example modulus if you should begin a new row or not.

Don
Hi,all thank you, it was all helpful.... thnaks again ... :)@nico: no problem. thanks.
San82moon
+1  A: 

EDITED after reformatting of the question

I think what you need is simply something on the line of:

echo '<table><tr>';
$n = 0;

foreach ($a as $key => $value) 
        {
        if ($n==3)
             {
             $n = 0;
             echo '</tr><tr>';
             }

        echo '<td>'.$value.'</td>';
        $n++;
        }
 echo '</tr></table>';
nico
but it displays vertically,i want it in table like structure.Thanks, slc
San82moon
@San82moon: I rewrote the answer. It was not clear what you wanted before the question was correctly formatted :) Hope this helps
nico
+1  A: 

You can do like:

$counter = 0;
foreach ($a as $key => $value) {
   $counter++;

   if (($counter % 3) === 0)
   {
     echo "<DIV id='container'>
      <DIV id='middle'>$value</DIV>
      </DIV>";
   }
   else
   {
     echo "<DIV id='container' style='float:left;'>
      <DIV id='middle'>$value</DIV>
      </DIV>";
   }
}
Sarfraz
A: 

Simply use like this,

foreach ($a as $key => $value) {
     echo "<DIV id='container' style='display:inline'>
     <DIV id='middle' style='display:inline'>$value</DIV>
     </DIV>";
     //echo "<br />$key:$value<br />\n";
  }

add the break statement depending on your second line.

Karthik
A: 

By default, div's don't float next to each other. You could give all the div's, except every third one float: left in the style. However it's okay to use table's if your displaying a table.

If you can derive from the value of $key wether to start a new row or not you can do something like:

echo '<table><tr>';
foreach ($a as $key => $value) {
    if($key == 'somevalue'){
        //start a new row if the $key allow
        echo '</tr><tr>';
    }
    echo '<td>'.$value.'</td>';
}
echo '</tr></table>';

If you can't derive from the value of $key wether to start a new row or not, use a counter, like so:

echo '<table><tr>';
$cnt = 0;
foreach ($a as $key => $value) {
    if($cnt % 3 == 0){
        //start a new row if this row contians 3 cells
        echo '</tr><tr>';
    }
    echo '<td>'.$value.'</td>';
    $cnt++;
}
echo '</tr></table>';
Lex
+1  A: 

Kind of what nico was saying:

foreach ($a as $key => $value) 
{
    echo '<div style="width:33%;float:left">'.$value.'</div>';
}

Or what don was saying:

$i = 0;
$open = false;

echo '<table>';

foreach( $a as $key => $value )
{
    if( 0 == $i % 3 ) {
        echo '<tr>';
        $open = true;
    }

    echo "<td>$value</td>";

    if( 2 == $i % 3 ) {
        echo '</tr>';
        $open = false;
    }

    $i++;
}

if( $open )
    echo '</tr>';

echo '</table>';
Kerry