这是我的家庭作业,我目前停留在第5部分: http://yorktown.cbe.wwu.edu/sandvig/mis314/assignments/Assignment06.aspx
结果应该是这样(选择一个类别,并在右边的div中填充相关产品): http://yorktown.cbe.wwu.edu/sandvig/MIS314/Assignments/A06/CategoryItems.php?catID=1
这是我的版本的当前状态: https://yorktown.cbe.wwu.edu/students/192/roversg/A06/categoryitems.php?category=T-shirts
这里的三个表格均为PDF格式: https://1drv.ms/f/s!AqSyu9NNp2DFtZMQgnJuOWgwhVELsQ
我不知道如何在第二个$ sql =“ SELECT [...]”语句中调用一个选定类别中的每个项目。 我知道我必须加入表,但是当指定CatName时,如何使echo语句显示与特定CatID相关的ItemID等?
下面,我将发布我的.php文件。
第一部分工作正常
<!DOCTYPE html>
<html><head>
<title>Product Categories</title>
<link href="/sandvig/mis314/assignments/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<?php
include 'databaseconnection.php';
//connect to database
$link = fConnectToDatabase();
$category = $_GET['category'];
?>
<div class="pageContainer">
<div class="centerText">
<h3>Product Category Count</h3>
<hr>
</div>
<div class='equalColumnWraper'>
<div class='leftColumn'>
<div class="centerText">
<h3>Categories</h3>
</div>
<?php
//List records
$sql = "SELECT ab.ItemID, ab.CatID, CatName
,COUNT(CatName) as count
from geekproducts a, geekcategories b, geekproductcategories ab
WHERE b.CatID = ab.CatID AND a.ItemID = ab.ItemID
GROUP BY CatID"
;
//$result is an array containing query results
$result = mysqli_query($link, $sql)
or die('SQL syntax error: ' . mysqli_error($link));
// iterate through the retrieved records
while ($row = mysqli_fetch_array($result)) {
//Field names are case sensitive and must match
//the case used in sql statement
echo "
<a class='menuLink' href='?category={$row['CatName']}'>{$row['CatName']} ({$row['count']})</a>";
}
?>
</div>
以下是我的问题
//List records
if (isset($category)) {
$sql = "SELECT ab.ItemID, ab.CatID, CatName, Name, Image, price
from geekproducts a, geekproductcategories ab, geekcategories b
WHERE b.CatName = '$category'
";
} else {
$sql = "SELECT Name, Image, price
FROM geekproducts, geekcategories
ORDER by Name";
}
//$result is an array containing query results
$result = mysqli_query($link, $sql)
or die('SQL syntax error: ' . mysqli_error($link));
echo "<div class='itemCount'>We sell " . mysqli_num_rows($result) . " items in '$category'</div>";
while ($row = mysqli_fetch_array($result)) {
//Field names are case sensitive and must match
//the case used in sql statement\
echo "
<div class='productItem'>
<img src='/sandvig/mis314/assignments/a06/images/m_{$row['Image']}' class='productImage'>
<div class='productName'>{$row['Name']}</div>
<div class='productPrice'>Price: ${$row['price']}</div>
<div class='productDesc'> {$row['ShortDesc']}</div>
</div>
";
}
?>
<div class="clearfix"></div>
</div>
</body></html>
谢谢!
答案 0 :(得分:0)
您可以重新编写或将查询更改为此:
SELECT gp.ItemID, gc.CatID, gc.CatName, gp.Name, gp.Image, gp.Price
FROM geekproducts gp
JOIN geekproductcategories gpc ON gpc.ItemID = gp.ItemID
JOIN geekcategories gc ON gc.CatID = gpc.CatID
WHERE gc.CatName = "{$category}"