代码之家  ›  专栏  ›  技术社区  ›  Max

用PHP实现水平表而不是垂直表

php
  •  0
  • Max  · 技术社区  · 7 年前

    我使用这篇文章中建议的解决方案,用php创建了一个水平html表: Printing out a table horizontal instead of vertical using PHP

    我的代码是这样的:

    $sql="SELECT Pr1, Pr2, Pr3, Pr4 FROM Tbdata ORDER BY Date DESC";
    $result=mysqli_query($con,$sql);
    $row=mysqli_fetch_assoc($result);
    
    $Pr1 = '';
    $Pr2 = '';
    $Pr3 = '';
    $Pr4 = '';
    
    while($row = $result->fetch_assoc())
    {
    
        $Pr4 .= '<td>'.$row['Pr4'].'</td>';
        $Pr3 .= '<td>'.$row['Pr3'].'</td>';
        $Pr2 .= '<td>'.$row['Pr2'].'</td>';
        $Pr1 .= '<td>'.$row['Pr1'].'</td>';
    }
    
    
    echo '
        <table class="table">     
            <tbody>
                <tr>
                 <td>'.$Pr4.'</td>
                </tr>
                <tr>
                 <td>'.$Pr3.'</td>
                </tr>
                 <tr>
                 <td>'.$Pr2.'</td>
                 </tr>
                 <tr>
                 <td>'.$Pr1.'</td>
                </tr>
        </tbody>
        </table>
    '; 
    
    ?>
    

    代码运行良好。唯一的问题是我在查询中用Date DESC提取数据。由于某种原因 最近的日期 不会出现在桌子上。我错过了什么?拜托,谢谢。

    2 回复  |  直到 7 年前
        1
  •  1
  •   user3783243    7 年前

    fetch while 接电话。删除前面的一个(或者注释掉它,正如我必须展示的那样)。

    //$row=mysqli_fetch_assoc($result);
    
    $Pr1 = '';
    $Pr2 = '';
    $Pr3 = '';
    $Pr4 = '';
    
    while($row = $result->fetch_assoc())
    
        2
  •  1
  •   Nigel Ren    7 年前

    你放弃第一行。。。

    $sql="SELECT Pr1, Pr2, Pr3, Pr4 FROM Tbdata ORDER BY Date DESC";
    $result=mysqli_query($con,$sql);
    $row=mysqli_fetch_assoc($result);   // Reads row, comment this out
    

    把最后一行注释掉。

    当你把每一件东西都包起来的时候 <td> 标签,你不需要在。。。

    <td>'.$Pr4.'</td>
    

    因此,请移除 <td> </td> 这些标签。

    推荐文章