簡體   English   中英

我可以將$ variable變成表格嗎?

[英]Can I turn a $variable into a table?

我遵循了一個在線教程來創建一個管理后端,最終在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; ?> 在頁面內容上的<?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

我想要的是將它放在一個對稱的列表視圖中,例如表格,以顯示結果和“編輯/刪除”選項。 試圖將具有1行5列的表放在$product_list ,但沒有成功。

$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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM