感谢您的光临!
我正在尝试为我的学校项目做一个电子商务网站。目的是用数据库中的内容填充我的网页,并且似乎没有记录 1 个特定条件,以便我能够从数据库中提取数据以呈现在网站上:
在我的主页上,我在侧边栏上有这个代码:它会导致一个 php 页面根据产品类别检索所有产品
<ul class="left_menu">
<li class="odd"><a href="categories.php?catno=10familia">Familia Originals</a></li>
<li class="even"><a href="categories.php?catno=20familia">Ready to Cook</a></li>
<li class="odd"><a href="categories.php?catno=30familia">Siomai and Buns</a></li>
<li class="even"><a href="categories.php?catno=40familia">Pork Snacks</a></li>
<li class="odd"><a href="categories.php?catno=50familia">Ready Made Dishes</a></li>
</ul>
我有这个代码/页面,它将通过使用 (id)catno= 来根据产品类别显示产品,以引用应该显示的类别。
// Sanitize the $_GET['catno'] and match with the correct category:
$sanitize = mysqli_real_escape_string($dbc, $_GET['catno']);
//match cases according to the product category
if ($sanitize == '10familia') {
$catid = 1;
} elseif ($sanitize == '20familia') {
$catid = 2;
} elseif ($sanitize == '30familia') {
$catid = 3;
} elseif ($sanitize == '40familia') {
$catid = 4;
} elseif ($sanitize == '50familia') {
$catid = 5;
} else {
$cat_error = '<div class="center"><h2>There are no products in this category. Please try again later.</h2></div>';
}
?>
<div class="center_content">
<div class="center_title_bar">Latest Products</div>
<div class="scroll_box_tall">
<?
$query = "SELECT product_id, name, price, thumbnail FROM products WHERE category_id = '$catid' ORDER BY product_id ASC";
$request = mysqli_query($dbc, $query);
if (mysqli_affected_rows($dbc) == 1) {
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$item_id = $row[0]; // Assign product_id to $item_id to be passed on the href
$item_name = $row[1]; // Assign name to var $item_name
$item_price = $row[2]; // Assign price to var $item_price
$item_thumb = $row[3]; // Assign thumbnail to $item_thumb
// echo item name
$div1 = '<div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"><div class="product_title">' . $item_name . '</div>';
// echo the thumbnail
$div2 = '<div class="product_img"><a href="show_product.php?idno=' . $item_id . '"><img src="product/thumb/' . $item_thumb . '" alt="' . $item_name . '"/></a></div>';
// echo the price
$div3 = '<div class="prod_price"><span class="price">RRP £ ' . $item_price . '</span></div></div><div class="bottom_prod_box"></div></div>';
echo "$div1$div2$div3";
}
} else { // Say an error message if there is no products in the category or the query is unsuccessful
echo '<div class="center"><h2>There are no products in this category. Please try again later.</h2></div>';
} ?>
</div>
条件 if/else 运行良好,并且在检索产品并将其显示在页面上时完美运行。(categories($catid) 2,3,4,5 - 如果点击了各自的链接,则工作正常)我的主要问题是所有条件 if/else 都记录了除第一个之外的值:
if ($sanitize == '10familia') {
$catid = 1;
}
没有为查询记录值 $catid= 1 以从数据库中的类别 1 中提取所有产品。
我不知道为什么这个特定的条件不起作用,但其他四个相同的工作..
注意:我刚开始使用 php,如果有更好的方法来执行 if-else,我深表歉意,我使用的是 if/elseif 方法。
谢谢你们。:)