【问题标题】:Horizontal table instead of vertical with PHP水平表而不是使用 PHP 的垂直表
【发布时间】:2019-02-27 05:44:41
【问题描述】:

我使用本文建议的解决方案用 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 提取数据。由于某种原因,最近日期的数据没有出现在表格中。我在这里想念什么?请谢谢。

【问题讨论】:

  • 请为Tbdata 表添加示例数据。然后,向我们展示您正在运行的查询的当前和预期输出。
  • 我相信 Date 是 MySQL 中的保留关键字。因此,当我命名列日期时,我总是使用反引号 () around the column name like this Date` 以确保 MySQL 知道我的意思是列名。我希望这就是问题所在。
  • @Rolfie date 未保留。 dev.mysql.com/doc/refman/5.5/en/keywords.html(注(R)表示保留)。
  • 我将列名 'date' 分别更改为 'Mdate' 和查询。问题依然存在。
  • 这会给你无效的 HTML。它会给你&lt;td&gt;'s in &lt;td&gt;'s。

标签: php


【解决方案1】:

每个fetch 调用都会将行计数提高1 个位置。只有while fetch 调用。删除前一个(或将其注释掉,正如我必须展示的那样)。

//$row=mysqli_fetch_assoc($result);

$Pr1 = '';
$Pr2 = '';
$Pr3 = '';
$Pr4 = '';

while($row = $result->fetch_assoc())

【讨论】:

    【解决方案2】:

    你丢弃了第一行...

    $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
    

    注释掉最后一行。

    此外,当您将每个项目包装在 &lt;td&gt; 标记中时,您不需要它们...

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

    所以删除这些中的&lt;td&gt;&lt;/td&gt; 标签。

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 2017-08-19
      • 1970-01-01
      • 2016-11-21
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      相关资源
      最近更新 更多