data from database with foreach - from top to down

前端 未结 5 616
情话喂你
情话喂你 2021-01-24 19:15

database:

id | name
1  | aaa
2  | bbb
3  | ccc
.. | ...
250| zz3

foreach ($datafromdb as $value){
  echo $value->name();
}

this show me:

相关标签:
5条回答
  • 2021-01-24 19:18
    <?php
    
    $data = range(1,16);
    
    $maxColumns = 4;
    $maxRows = 4;
    
    ?>
    
    <table>
    
        <? for($r = 0; $r < $maxRows; $r++) { ?>
            <tr>
                <? for($c = 0; $c < $maxColumns; $c++) { ?>
                    <td><? echo $data[($c*$maxColumns)+$r]?></td>
                <? }?>
            </tr>
        <? } ?>
    </table>
    

    You might want to check if the data actually exists before echoing it.

    0 讨论(0)
  • 2021-01-24 19:24

    Look, I don't exactly know php but this is what I can think of:

    <table>
        <tr>
        var counter=0;
        foreach ($datafromdb as $value){
            if (i%12==0) { echo '<td>'; }
            echo $value->name()+'<br/>';
            if (i%12==0) { echo '</td>'; }
            counter++;
        }
        </tr>
    </table>
    

    Hope this helps!

    0 讨论(0)
  • 2021-01-24 19:27
    <table>
            $index=0;
            foreach ($datafromdb as $value)
            {
              if($index%12==0) echo '<tr>' ;
              echo '<td>' . $value->name() . '</td>';
              if($index%12==0) echo '</tr>' ;
            }
    </table>
    
    0 讨论(0)
  • 2021-01-24 19:33

    Daniel Cherrington's code is good, but with only PHP just do:

    <?php
    $data = range(1,240);
    $tab = array();
    $max = 12;
    for($i=0;$i<sizeof($data);$i++){
        $tab[($i % $max)][] = $data[$i];
    }
    
    echo "<table border='1'>";
    foreach($tab as $line){
        echo "<tr>";
        foreach($line as $row){
            echo "<td>".$row."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
    ?>
    
    0 讨论(0)
  • 2021-01-24 19:41

    You could try creating tables side by side (float them using css)

      $index=0;
        foreach ($data as $value)
        {
          if($index == '12') $index = 0;
    
          if($index == 0) echo '<table style="float:left;">';
          echo '<tr>' ;
          echo '<td>' . $value . '</td>';
          echo '</tr>' ;
          if($index==11) echo '</table>' ;
          $index ++;
        }
    
    0 讨论(0)
提交回复
热议问题