Displaying table data column-wise in a while loop

对着背影说爱祢 提交于 2019-12-01 08:34:22

问题


I'm attempting to retrieve all that data from a database, put it in a table (more than one table if necessary) and display them column-wise in lots of 4 split across multiple pages.

I would like to know how I would get the tables to display horizontally e.g.

Table Header   Table Header  Table Header
Table Data       Table Data      Table Data
Table Data       Table Data      Table Data

Rather than:

Table Header
Table Data
Table Data

Table Header
Table Data etc.

Here is the code so far:

// While loop that will display the results in groups of 4
while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{ $newDate = $row['datenow']->format('d/m/Y'); ?>

<table id="syncresults">
<thead>
    <tr>
        <th scope="col" id="dateheader"> <?php echo $newDate ?></th>
    </tr>
</thead>

<tbody>
    <tr>
        <td><?php echo $row['nbsactive']; ?></td>
    </tr>
    <tr>
        <td><?php echo $row['nbsmanysynced']; ?></td>
    </tr>
    <tr>
        <td><?php echo $row['nbsthreedays']; ?></td>
    </tr>
</tbody>

Any suggestions on how to do this or to point me in the right direction would be greatly appreciated.


回答1:


There's not enough information here to say whether or not you should be using a table because you provided non-data.

If we're writing a closet organization application, our data might come out of the database like this:

John | Shoes | 3
John | Pants | 10
Sally | Shoes | 12
Sally | Pants | 8
Billy | Shoes | 4
Billy | Pants | 9
Kate | Shoes | 6
Kate | Pants | 6

But we want to display it like this:

Member | Shoes | Pants
John | 3 | 10
Sally | 12 | 8
Billy | 4 | 9
Kate | 6 | 6

We would write our loop something like this:

<table>
<tr>
<?
$last = null;
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
    if ($last != $row['member']) {
        if ($last) { // $last is null on our first pass through the loop, so don't print the tr's
?>
</tr>

<tr>
<?
        }
?>
    <td><?=$row['member']?></td>
<?
        $last = $row['member'];
    }
?>
    <td><?=$row['quantity']?></td>
<?
}
?>
</tr>
</table>

The headers? Well, typically I would recommend looping twice and resetting the pointer, but I don't see the equivalent of mysql_data_seek or pg_result_seek for the interface you're using, so I can't help you any farther than this. Using output buffering on the first "row" of results combined with a collector variable gathering up all of the headers as an array can work without the need to reset the pointer.

If you're just wanting to spit out the results in 4 columns because you think it looks prettier and not because it expresses tabular data, using CSS columns would be the better way to go.




回答2:


use table inside table.

// php fetch data
while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
    echo'<table>
    <tr><td><table> place data here</table></td></tr>

    <tr><td><table>or here </table></td></tr>

    <tr><td><table>or here, depending on what the data is</table></td></tr>

    </table>';
}



回答3:


Replace your code as:

    <table>
    <?php
$i=0;
    while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
    { $newDate = $row['datenow']->format('d/m/Y'); ?>

    $j=$i%3;
      ?>
      <?php
    if($j==0)
    {
      echo"<tr>";
    }
      ?>
    <td>
    <table id="syncresults">
    <thead>
        <tr>
            <th scope="col" id="dateheader"> <?php echo $newDate ?></th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td><?php echo $row['nbsactive']; ?></td>
        </tr>
        <tr>
            <td><?php echo $row['nbsmanysynced']; ?></td>
        </tr>
        <tr>
            <td><?php echo $row['nbsthreedays']; ?></td>
        </tr>
    </tbody></table>
    </td>
    <?php
    if($j==2)
    {
      echo"</tr><tr><td colspan='3'>&nbsp;</td></tr>";
    }
    $i++;
    }
    ?>
    </table>



回答4:


$th1="<tr>";
$td1="<tr>";
$td2="<tr>";
$td3="<tr>";

while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { 

    $newDate = $row['datenow']->format('d/m/Y');

    $th1.="<th scope='col' id='dateheader'>".$newDate."</th>";
    $td1.="<td>".$row['nbsactive']."</td>";
    $td2.="<td>".$row['nbsmanysynced']."</td>";
    $td3.="<td>".$row['nbsthreedays']."</td>";

}

$th1.="</tr>";
$td1.="</tr>";
$td2.="</tr>";
$td3.="</tr>";

$result="<table id='syncresults'>".$th1.$td1.$td2.$td3."</table>";


来源:https://stackoverflow.com/questions/13348559/displaying-table-data-column-wise-in-a-while-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!