PHP & MySQL - 仅显示所选类别的产品和相关信息 - 三张表
PHP & MySQL - display only prodcucts and related information from selected category - three tables
这是我的家庭作业,我目前卡在第 5 部分:
http://yorktown.cbe.wwu.edu/sandvig/mis314/assignments/Assignment06.aspx
结果应该是这样的(Select 一个类别,它用相关产品填充右侧的 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 [...]" 语句中调用一个选定类别中的每个项目。
我知道我必须加入表格,但如何才能使 echo 语句在指定 CatName 时显示与特定 CatID 关联的 ItemID 等?
下面我将post我的.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>
谢谢!
您可以re-write或将查询更改为:
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}"
这是我的家庭作业,我目前卡在第 5 部分: http://yorktown.cbe.wwu.edu/sandvig/mis314/assignments/Assignment06.aspx
结果应该是这样的(Select 一个类别,它用相关产品填充右侧的 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 [...]" 语句中调用一个选定类别中的每个项目。 我知道我必须加入表格,但如何才能使 echo 语句在指定 CatName 时显示与特定 CatID 关联的 ItemID 等?
下面我将post我的.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>
谢谢!
您可以re-write或将查询更改为:
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}"