【问题标题】:how to put sql data into an html table如何将sql数据放入html表中
【发布时间】:2017-03-31 14:20:51
【问题描述】:

我正在尝试将我的 SQL 数据放入 HTML 数据中,这是我目前拥有的代码,但它还不能工作。我试图将 HTML 放入我的 PHP 代码中,我想知道是否应该反过来(将 PHP 放入 HTML),任何回复都非常感谢

  while ($rowObj = $queryResult->fetch_object()) {
echo "<tr>";
echo"<td>" . $AE_events ['eventTitle'] . "</td>;
echo"<td>" . $AE_events ['eventDescription'] . "</td>;
echo"<td;>" . $AE_events ['eventStartDate'] . "</td>;
echo"<td>" . $AE_events ['eventEndDate'] . "</td>;
echo"<td>" . $AE_events ['eventPrice'] . "</td>;

    $AE_eventTitle = $rowObj->eventTitle;
    $AE_eventDescription = $rowObj->eventDescription;
    $AE_eventStartDate = $rowObj->eventStartDate;
    $AE_eventEndDate = $rowObj->eventEndDate;
    $AE_eventPrice = $rowObj->eventPrice;
    echo "<div>  $AE_eventTitle<br> $AE_eventDescription<br>   
$AE_eventStartDate<br> 
$AE_eventEndDate<br> $AE_eventPrice<br> </div>";

}

【问题讨论】:

  • 语法错误,解析错误
  • 您在每个&lt;/td&gt; 之后都缺少一个"
  • echo"&lt;td&gt;" . $AE_events ['eventTitle'] . "&lt;/td&gt;; 第一个错误
  • echo"&lt;td&gt;" . $AE_events ['eventDescription'] . "&lt;/td&gt;;第二个错误

标签: php html mysql sql


【解决方案1】:

如果你想用 PHP 中的数据绘制一个表格,你可能不得不使用嵌套循环。第一个循环管理行,嵌套循环管理列。示例:

$data = [
    [1, 3, 5],
    [2, 4, 6]
];
print "<table>";
for ($i = 0; $i < count($data); $i++) {
    $row = $data[$i];
    print "<tr>";
    for ($k = 0; $k < count($row); $k++) {
        print "<td>{$row[$k]}</td>";
    }
    print "</tr>";
}
print "</table>";

输出:

<table>
    <tr>
        <td>1</td>
        <td>3</td>
        <td>5</td>
    </tr>
    <tr>
        <td>2</td>
        <td>4</td>
        <td>6</td>
    </tr>
</table>

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 2016-04-25
    • 2018-05-13
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多