簡體   English   中英

使用SQL查詢回顯表

[英]Using SQL Query to echo table

<?php
$product_list="";
if(isset($_GET['cat'])){
$sql = mysql_query("SELECT*FROM products WHERE category LIKE'$category'");

while($row=mysql_fetch_array($sql)){
$tablerow='3';
$product_list.='<tr>';
   while($tablerow >= 0){
      $id=$row["id"];
      $name=$row["name"];
      $price=$row["price"];
      $date_added = strftime("%b %d, 20%y",strtotime($row["date_added"]));
      $discriptiontags = $row['category'];
      $discription = $row['subcategory'];
      $size = $row['details'];
      $qty= $row['inv'];
      $product_list.='<td><img src="inventory_images/'.$id.'.jpg" width="250" height="167"/><br/>$name</td>';
      $tablerow=$tablerow-1;
}
$product_list.='</tr>';
 }
}else{
$product_list = "no products in this category";
exit();
 }          
?>

所以這背后的想法是; 使用$ _GET來獲取我的數據庫的類別標簽,並使用LIKE查找行信息,並從一個3列的表中的單個表單元格中顯示每個行信息,並動態添加其他行。

這是應該顯示的位置的html

      <table width="760px" border="1" cellpadding="4">
      <?php echo $product_list ?>
      </table>

鏈接到相關頁面

我選擇綠色類別進行測試,因為它有3個項目。

也由於某種原因,如果未設置url變量,則頁面根本不會顯示... ????

頁面上其余的php:

<?php 
///conect to mysql
///grab page variable
include "storescript/connect_to_mysql.php";
$category="";
$tab="-1";
if(isset($_GET['cat'])){
    $category=$_GET['cat'];
}
if(isset($_GET['tab'])){
    $tab=$_GET['tab'];
}
?>

不要使用exit(); 它停止腳本。 因此,它永遠不會“回聲”甚至“沒有該類別的產品”。

如果包含第一個代碼,請使用return; 而不是exit()。 但是我看不到在哪插入代碼。

您必須使用category LIKE '%red%'來匹配“紅色中度聖誕節”和“ Alsored類別”等類別。 category LIKE 'red'僅與類別“紅色”匹配。

請參考http://www.techonthenet.com/sql/like.php以了解有關LIKE更多信息。

暫無
暫無

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

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