我正在学习如何创建电子商务网站。我想弄清楚如何按价格订购产品结果。下面的代码是一个菜单列表,用于过滤特定的产品类别或查看所有产品的选项 - 代码可用。
我不确定如何将代码添加到“按价格排序”(从低到高,从高到低)。
我已经尝试创建一个名为priceList
的函数并在函数categoryList
内调用(它在数据库中查询哪种产品类型或查看所有产品),但是不起作用。
function priceList() {
$con = getConnection();
if($pUserCat == "lowToHigh") {
$sqlQuery = "SELECT Price from Products
ORDER BY Price ASC";
} elseif($pUserCat == "highToLow") {
$sqlQuery = "SELECT Price from Products
ORDER BY Price DESC";
// Execute Query -----------------------------
$result = mysqli_query($con, $sqlQuery);
if(!$result) {
echo "Cannot do query" . "<br/>";
exit;
}
$row = mysqli_fetch_row($result);
$count = $row[0];
if ($count > 0) {
echo "Query works" . "<br/>";
} else {
echo "Query doesn't work" ."<br/>";
}
// Display Results -----------------------------
$num_results = mysqli_num_rows($result);
for ($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc ($result);
// print_r($row);
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
echo "Price: " . stripslashes($row['Price']);
}
// Close connection
closeConnection($con);
}
表格
<!--category and price form ------------------------- -->
<form action="register_script.php" name="frm" method="post">
<select name="category" id="category">
<option value="viewall">View All</option>
<option value="dress">Dress</option>
<option value="athletic">Athletic</option>
<option value="sandals">Sandals</option>
</select>
<input type="submit" value="Go" />
</form>
<form action="register_script.php" name="frm" method="post">
<select name="price" id="price">
<option value="lowToHigh">Low to High</option>
<option value="highToLow">High to Low</option>
</select>
<input type="submit" name="orderPrice" value="orderPrice" />
</form>
</div>
PHP
<?php
function categoryList($pUserCat=false) {
echo "I am in category list" . "<br/>";
$con = getConnection();
$sqlQuery = "SELECT * from Products";
if($pUserCat == "athletic") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='athletic'";
} elseif ($pUserCat == "dress") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='dress'";
} elseif ($pUserCat == "sandals") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='sandals'";
} elseif ($pUserCat == "viewall") {
$sqlQuery = "SELECT * from Products";
}
// Execute Query -----------------------------
$result = mysqli_query($con, $sqlQuery);
if(!$result) {
echo "Cannot do query" . "<br/>";
exit;
}
$row = mysqli_fetch_row($result);
$count = $row[0];
if ($count > 0) {
echo "Query works" . "<br/>";
} else {
echo "Query doesn't work" ."<br/>";
}
// Display Results -----------------------------
$num_results = mysqli_num_rows($result);
for ($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc ($result);
// print_r($row);
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
echo "Price: " . stripslashes($row['Price']);
}
// priceList();
$priceOrder = getPriceBtn(); //$priceOrder holds value of option selected in
//price order menu
priceList($priceOrder);
// Close connection
closeConnection($con);
}
function getPriceBtn() {
//echo "<br/>"."I am calling getPriceBtn function" . "<br/>";
$priceBtn = $_POST["orderPrice"]; // price button
return $price;
//echo $priceBtn;
}
?>
答案 0 :(得分:1)
我认为您需要将$ pUserCat传递给函数priceList($ pUserCat);
提示,您应该有一个默认查询,例如从高到低,如果选择了其他选项,则运行该查询:
$sqlQuery = "SELECT Price FROM Products ORDER BY Price DESC";
if($pUserCat == "lowToHigh") {
$sqlQuery = "SELECT Price FROM Products ORDER BY Price ASC";
}
你也可以重写这个:
$ sqlQuery =“SELECT * from Products”; //这需要吗?
if($pUserCat == "athletic") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='athletic'";
} elseif ($pUserCat == "dress") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='dress'";
} elseif ($pUserCat == "sandals") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='sandals'";
} elseif ($pUserCat == "viewall") {
$sqlQuery = "SELECT * from Products";
}
喜欢这个categoryList($ pUserCat):
if($pUserCat != 'viewall') {
$sqlQuery = "SELECT * FROM Products WHERE ProductType='%s'";
$sqlQuery = sprintf($sqlQuery, $pUserCat);
} else {
$sqlQuery = "SELECT * from Products";
}
答案 1 :(得分:0)
不共享本地函数变量。如果在函数中定义了函数变量,那么从该函数中调用的函数就无法访问父函数的本地变量,这就是它们被称为“本地”的原因。您需要将变量作为参数传递:
定义时:
function priceList( $pUserCat ) {
...
}
致电:
priceList( $pUserCat );