0

I use this code mount table header after getting some values from DB

I experience a trouble, results are VERTICAL instead horizontal:

Where do I wrong?

EXPECTED

1 2 3 4 5 6

CODE

$query = '1,2,3,4,5,6';
$data = explode(',',$query);

echo '<table>';
foreach($data as $row){
    echo '<tr>';

    $row = explode(' ',$row);
    foreach($row as $cell){
        echo '<th>';
        echo $cell;
        echo '</th>';
    }
    echo '</tr>';
}

echo '</table>';

Simple HTML work good

                <tr>
                    <th>1</th>
                    <th>2</th>
                    <th>3</th>
                    <th>4</th>
                    <th>5</th>
                    <th>6</th>
                </tr>
4

2 回答 2

4

因为您每次都在循环中创建新表行,所以请在循环 <tr>之外</tr>和 wtry 中编写

<table>
    <tr>
      <?php
         foreach($data as $row){
           $row = explode(' ',$row);
              foreach($row as $cell){
                echo "<td>{$cell}</td>";
             }
         }
      ?>
    </tr>
</table>
于 2013-05-06T04:28:10.450 回答
0
$query = '1,2,3,4,5,6';
$query= '<TH>'.str_replace(',', '</TH><TH>', $query).'</TH>';
echo '<TABLE BORDER=1><TR>'.$query.'</TR></TABLE>';
于 2013-05-06T05:08:59.560 回答