我在php中有一个应用程序,我必须使用Ajax按类别过滤一些产品,我不知道如何。
我所有的PHP代码:
<?php
session_start();
include_once("config.php");
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 align="center">Products </h1>
<!-- Products List Start -->
<?php
$results = $mysqli->query("SELECT product_code, product_name, product_desc, price FROM products ORDER BY id ASC");
if($results){
$products_item = '<ul class="products">';
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
$products_item .= <<<EOT
<form method="post" action="cart_update.php">
<table>
<tr>
<td> Name: {$obj->product_name}</td>
<td>Category: {$obj->product_desc}</td>
<td> Price: {$currency}{$obj->price} </td>
<td>
<span>Color: </span>
<select name="product_color">
<option value="Black">Black</option>
<option value="Silver">Silver</option>
</select>
</td>
<td>
<span>Quantity: </span>
<input type="text" size="2" maxlength="2" name="product_qty" value="1" />
</td>
<td>
<div align="center"><button type="submit" class="add_to_cart">Add</button></div></td>
<input type="hidden" name="product_code" value="{$obj->product_code}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />
</div></div>
</form>
</table>
EOT;
}
$products_item .= '</ul>';
echo $products_item;
}
?>
<!-- Products List End -->
<?php
if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0)
{
echo '<h3><center>Your Shopping Cart</center></h3>';
echo '<form method="post" action="cart_update.php">';
echo '<table width="30%" cellpadding="6" cellspacing="0"';
echo '<tbody>';
$total =0;
$b = 0;
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$product_color = $cart_itm["product_color"];
$bg_color = ($b++%2==1) ? 'odd' : 'even'; //zebra stripe
echo '<tr class="'.$bg_color.'">';
echo '<td>Qty <input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
echo '<td>'.$product_name.'</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /> Remove</td>';
echo '</tr>';
$subtotal = ($product_price * $product_qty);
$total = ($total + $subtotal);
}
echo '<td colspan="4">';
echo '<button type="submit">Update</button>';
echo '</td>';
echo '</tbody>';
echo '</table>';
echo '</h1>';
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
?>
<div id="maindiv">
<select id="options">
<option value="v1">Category</option>
</select>
<table id="destinations" border="1">
<tr>
<th>Product</th>
<th>Category</th>
<th>Price</th>
<th>Color</th>
<th>Quantity</th>
</tr>
</table>
</div>
</body>
</html>
我真的无法理解如何制作这个过滤器。有人可以给我一些想法来解决这个问题吗?
答案 0 :(得分:0)
对于这个“过滤器”来说,你没有给我们太多的帮助。如果我假设“过滤器”你的意思是你希望根据一些用户发起的AJAX调用来改变你的SQL查询,那么你需要以下内容:
WHERE
子句,由上面的POST或GET数据动态构建这就是它。
非常粗糙示例:
$sql = "SELECT product_code, product_name, product_desc, price FROM products";
// Where $_POST['filter'] comes from an AJAX POST request in the frontend
if (!empty($_POST['filter'])) {
$codeSql = ' ' . (!empty($_POST['code']) ? "product_code = '" . mysqli_escape_string($_POST['code']) . '" : '');
$nameSql = ' ' . (!empty($_POST['name']) ? "product_name = '" . mysqli_escape_string($_POST['name']) . '" : '');
$sql .= "WHERE " . $codeSql . $nameSql;
}
$sql .= " ORDER BY id ASC";
$results = $mysqli->query($sql);