gpt4 book ai didi

php - 我可以将 $variable 转换为表格吗?

转载 作者:行者123 更新时间:2023-11-29 00:33:18 28 4
gpt4 key购买 nike

我按照在线教程创建了一个管理后端,最后在 php 页面的 HTML 标记之前得到了这段代码:

<?php 
$product_list = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount

if ($productCount > 0)
{
while($row = mysql_fetch_array($sql))
{
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
}
}
else
{
$product_list = "You have no products listed in your store yet";
}
?>

当我把 <?php echo $product_list; ?>在页面内容上,我检索到这样的列表结果:

Product ID: 1 - Strawberries - $10 - Added Mar 22, 2013       edit • delete
Product ID: 2 - Apples 1 - $10 - Added Mar 22, 2013 edit • delete

我想要的是把它放在一个对称的 ListView 中,比如用一个表来显示结果和编辑/删除选项。试图在 $product_list 上放置一个 1 行 5 列的表格没有成功。

最佳答案

$mysqli = new mysqli("localhost", "username", "password", "database_name");

$query = "SELECT * FROM products ORDER BY date_added DESC";
$result = $mysqli->query($query);

$product_list = '<table>';
$product_list .= '<thead><tr>ID</tr><tr>Name</tr><tr>Price</tr><tr>Added</tr></thead>';
$product_list .= '<tbody>';

while($row = $result->fetch_array()){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$product_list .= "<tr><td>$id</td><td>$product_name</td><td>$price</td> <td>$date_added</td></tr>";
}

$product_list .= '</tbody>';
$product_list .= '</table>';

echo $product_list;

关于php - 我可以将 $variable 转换为表格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561488/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com