【问题标题】:html/mysql echo query results into table?html/mysql 将查询结果回显到表中?
【发布时间】:2015-04-20 12:37:05
【问题描述】:

我正在使用以下 MySQL 查询从我的数据库中选择数据并将结果回显到表中。

我的问题是第一组结果被正确回显,但是下一行显示我的结果垂直向下而不是水平。

这是发生了什么:

Heading1      Heading2      Heading3      Heading4     Heading5
a             b             c             d            e
a
b
c
d
e

请有人告诉我哪里出错了。这是我的代码:

    <?php 
$conn = new mysqli($host, $username, $password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); } 
$sql = "select * from new_supplier_request where status!= 'complete' and action_taken ='actioned'";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {
            echo '<table><tr><td><p><u>Request By</u></p></td><td><p><u>Date</u></p></td><td><p><u>Status</u></p></td><td><p><u>Supplier Name</u></p></td><td><p><u>Action</u></p></td></tr>';

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

        $datetime = strtotime($row['date']);
        $mysqldate = date("D, d M Y ", $datetime);

        echo '<tr>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p><a href="process/action.php?reference='.$row['reference'].'" id="action">Action</a>&nbsp;/&nbsp;<a href="process/decline.php?reference='.$row['reference'].'" id="decline">Decline</a></p></td></tr>';
}
echo '</table>';

            }else{

                echo'<div class="no_requests">No New Supplier Request&#39;s</div>';


            }  ?>

【问题讨论】:

  • 为什么每次循环迭代都关闭表&lt;/table&gt;?你应该在事后关闭它
  • 你在 while 循环中关闭了 &lt;/table&gt;.. 把它放在外面

标签: php mysql html-table


【解决方案1】:

您在循环中关闭表格。第一组结果被正确回显,因为它仍在第一个循环中,它会以您想要的方式输出,但由于关闭表格,其他结果被搞砸了。 &lt;/table&gt; 标签在循环中,但&lt;table&gt; 不在循环中,所以&lt;/table&gt; 甚至没有关闭任何东西。这意味着您的其余结果实际上并不在表格中。您需要将您的 &lt;/table&gt; 排除在外。

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

        $datetime = strtotime($row['date']);
        $mysqldate = date("D, d M Y ", $datetime);

        echo '<tr>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p>'.$row['user_id'].'</p></td>';
        echo '<td><p><a href="process/action.php?reference='.$row['reference'].'" id="action">Action</a>&nbsp;/&nbsp;<a href="process/decline.php?reference='.$row['reference'].'" id="decline">Decline</a></p></td></tr>';
}
echo '</table>';

还有你while上面的随机&lt;?php是怎么回事?

【讨论】:

  • 感谢此代码。我已经用您建议的代码更新了我的问题,但它仍然无法正常工作,结果仍然垂直显示在页面下方。
  • 没关系,我得到它的工作,是一个 div 问题。谢谢
猜你喜欢
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 2015-07-04
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多