Welcome Guest, Not a member yet? Register   Sign In
print table horizontal..
#1

[eluser]tnathos[/eluser]
hi, i have a select to my db, this select retunr 12 results but have only 1 field.

so i need put the data, in a table not vertical else horizontal mode, printing 1 file each 3 items. example

1 2 3
4 5 6
7 8 9
10 11 12

now how i can do this?

i have this but doesnt works..

Code:
<?php
    
    <?php
    
    foreach($img_elite->result() as $row):
    $j=0;
    echo '<tr>';    
    
    echo  '<td><img >media .'"/></td>';
        
        while($j <= 2){    

        foreach($img_elite->result() as $row):

    echo'<td><img >media .'"/></td>';    

endforeach;

$j++;
        
}
        
endwhile;
    
        
      echo '</tr>';
    
    endforeach; ?&gt;
#2

[eluser]Colin Williams[/eluser]
Code:
$i = 0;
$per_row = 3;
print '<table><tr>';
foreach ($things as $thing)
{
  $i++;
  print "<td>$thing</td>";
  if ($i % $per_row == 0)
  {
     print '</tr><tr>';
  }
}
print '</tr></table>';
#3

[eluser]jedd[/eluser]
Code:
<table>
<tr>
&lt;?php

$foo = array ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l");

$i = 0;
foreach ($foo as $bar)  {
    // NOTE - this fails - the if modulo test & its echo need to come last, as per Colin's post above.
    // Left as an instructive insight into the dangers of typing faster than you think.
    if (! ($i % 3))
        echo "</tr>\n<tr>\n";
    echo "<td>". $bar ."</td>";
    $i++;
    }
?&gt;

</tr>
</table>

Is that what you're trying to do?

EDIT: Dang.

EDIT2: Dang*2 - I've got an excess empty tr pair in there. Colin's was always going to be better anyway. Wink
#4

[eluser]Colin Williams[/eluser]
Haha, jedd, you gotta get your operation in the right place Smile I remember doing this a million times before when I used to do stuff in Perl (and laid out pages with tables).
#5

[eluser]jedd[/eluser]
Yeah .. wouldn't even have noticed if I hadn't been trying to be smart and posting the page-source!

Fixed here and tested, and now I'm happy - I've learned something today. Will leave original embarrassing code partial to serve as a warning to others.
#6

[eluser]tnathos[/eluser]
thanks!!




Theme © iAndrew 2016 - Forum software by © MyBB