显示产品类别

时间:2011-08-18 17:32:11

标签: php mysql e-commerce categories

我正在为企业创建一个简单的网站来展示他们的产品。在索引页面上,我显示了3个最新产品。我现在需要做的是有一个类别页面,人们可以去查看特定类别的产品。我尝试调整索引页面中的代码,但产品没有显示在我的category.php。

我有一个带有不同类别的product_list.php,当选择一个时,我正在尝试使用所选类别中的产品加载category.php。

我有什么:

product_list.php

<?php
include "storescripts/connect_to_mysql.php";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">

</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $product_name; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
  <table width="100%" border="0" cellspacing="0" cellpadding="15">
  <tr>
  <td valign="top" align="center"><table width="100%" border="1">
    <tr>
      <td align="center"><p>Aluminum Docks</p>
        <p><a href="category.php?category=docks"><img src="inventory_images/aluminum docks/PN99002-6_24x4_PKG_LG.jpg" width="100" height="64" alt="24X4" /></a></p></td> // There's a field in my DB called categories, and 'docks' is the value assigned to the products
      <td align="center"><p>Floating Docks</p>
        <p><img src="inventory_images/floating dock/100225279.jpg" width="100" height="60" alt="Floating Dock" /></p></td>
      <td align="center"><p>Frame Docks</p>
        <p><img src="inventory_images/frame dock/frameDock.jpg" width="100" height="64" alt="Frame Dock" /></p></td>
      <td align="center"><p>Pipe Docks</p>
        <p><img src="inventory_images/pipe dock/PN99002_16X4_SECTION_LG.jpg" width="100" height="64" alt="Pipe Dock" /></p></td>
    </tr>
    <tr>
      <td align="center"><p>Boat Lifts</p>
        <p><img src="inventory_images/boat lifts/GM1060_LG.jpg" width="100" height="64" alt="Boat Lift" /></p></td>
      <td align="center"><p>Boat Lift Accessories</p>
        <p><img src="inventory_images/boat lift acceessories/canopy_lg (1).png" width="100" height="64" alt="Boat Lift Accessory" /></p></td>
      <td align="center"><p>Rollers &amp; Caddies</p>
        <p><img src="inventory_images/rollers and caddies/caddy270 (1).jpg" width="100" height="64" alt="Caddy" /></p></td>
      <td align="center"><p>Accessories</p>
        <p><img src="inventory_images/accessorries/2step_LG.png" width="100" height="64" alt="Accessory" /></p></td>
    </tr>
  </table> 
  </table>
  <p>&nbsp;</p>
  <?php include_once("template_footer.php");?>
</div>
</body>
</html>

在category.php中我正在尝试创建一个名为$ dynamicList的变量,其中所有产品都具有“停靠”作为类别。但当我回显$ dynamicList时,没有渲染。

<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$category=$_GET['category'];
$sql = mysql_query("SELECT * FROM products WHERE category='$category' LIMIT 6");
$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"]));
             $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
        <tr>
          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="64" border="1" /></a></td>
          <td width="83%" valign="top">' . $product_name . '<br />
            $' . $price . '<br />
            <a href="product.php?id=' . $id . '">View Product Details</a></td>
        </tr>
      </table>';
    }
} else {
    $dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">

</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $category; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
  <table width="100%">
      <tr>
        <td valign="top"><p><?php echo $dynamicList;?><br />
          </p>
          <p>&nbsp;</p>
        <p>&nbsp;</p>          <h2>&nbsp;</h2></td>
      </tr>
  </table>
  <p>&nbsp;</p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

编辑:$ dynamicList嵌套在条件语句中。只需写下

$dynamicList = '';
在查询之前

页面上的当前输出是什么?查询中返回的行数是否= 0? 您还应该使用mysql_real_escape_string()

清理$ category输入
WHERE category ='".mysql_real_escape_string($category)."' ...

答案 1 :(得分:1)

必须首先使用此函数清理字符串:

public static function real_escape($string)
{
    if (null != $string) {
        $string = (get_magic_quotes_gpc()) ? $string : addslashes($string);
        return mysql_real_escape_string($string);
    }
    return $string;
}

或者更好地使用第三方脚本,例如phpDataMapper

答案 2 :(得分:-2)

$sql = mysql_query("SELECT * FROM products WHERE category='$category' LIMIT 6");

应该是

$sql = mysql_query("SELECT * FROM products WHERE category='".$category."' LIMIT 6");

这是我现在唯一能发现的错误