Displaying table data column-wise in a while loop

前端 未结 4 1961
栀梦
栀梦 2021-01-15 15:28

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 multip

相关标签:
4条回答
  • 2021-01-15 15:45

    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.

    0 讨论(0)
  • 2021-01-15 15:46
    $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>";
    
    0 讨论(0)
  • 2021-01-15 15:55

    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>';
    }
    
    0 讨论(0)
  • 2021-01-15 15:55

    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>
    
    0 讨论(0)
提交回复
热议问题