为想要在线销售杂货的客户创建电子商务项目
并且我有一个价格列,其中包含相应数量的产品的逗号分隔价格。问题是我如何按'价格从高到低或'价格从低到高功能进行排序。
这是示例表
**id** **Price** **Quantity** Name
1 20,30,50 250mlgm,500ml,1lt Coca-Cola
2 40,60,70 250mlgm,500ml,1lt Pepsi
现在的问题是,如果我想将价格列从低到高或从高到低排序,查询会是什么样子
ORDER BY Price DESC或ORDER BY价格ASC在我的案例中不起作用
请指导我这个
通过爆炸逗号并将它们保存在这样的数组中,在select标签中填充每个产品的价格和数量
<select ><option style='font-size:1vw;' value="Select Unit" >Select Unit</option>
<?php
$drq=$rows['Quantity'];
$drp=$rows['Price'];
$degh=explode(',',$drp);
$tryGH=explode(',',$drq);
for($ij=0;$ij<count($tryGH);$ij++){
echo "<option style='font-size:1vw;' value=$degh[$ij] >$tryGH[$ij] - $degh[$ij]</option>";
}
?> </select>
结果如下所示
答案 0 :(得分:1)
您可以通过几种方式安排数据库。一种方法是
**id** **Price** **Quantity** Name
1 20 250mlgm Coca-Cola
2 30 500ml Coca-Cola
3 50 1lt Coca-Cola
然后在您的代码中,您可以输出到客户端以使用
选择值<select name="unit" >
<option style='font-size:1vw;' value="Select Unit" >Select Unit</option>
<?php
$query = 'select Quantity, Price, Id, Name from Products ORDER BY name asc, Price DESC';
//execute the query, I don't know your driver
while($row = FETCH_FROM_YOUR_DRIVER) {
$quantitY = $row['Quantity'];
$price = $row['Price'];
$id = $row['Id'];
$company = $row['Name'];
echo "<option style='font-size:1vw;' value='$id'>$quantity - $price $company</option>";
}?>
</select>
然后您应该选择他们选择的ID并从您的数据库中查询价格。在您当前的代码中,我认为您直接从客户端获取价格。然而,客户可以操纵它并向您发送0的价格。
<强>更新强>
<style type="text/css">
.container {
display:block;
float:left;
margin:15px;
}
</style>
<?php
$query = 'select Quantity, Price, Id, Name from Products ORDER BY name asc, Price DESC';
//execute the query, I don't know your driver
$count = 0;
while($row = FETCH_FROM_YOUR_DRIVER /*again not sure of your driver*/) {
$products[$count]['id'] = $row['Id'];
$products[$count]['price'] = $row['Price'];
$products[$count]['quantity'] = $row['Quantity'];
$products[$count]['name'] = $row['Name'];
$count++;
}
/* Can be removed, I haven't set up a DB so this is how I test
$products = array(array('id' => 2, 'price' => 30, 'quantity' => '500ml', 'name' => 'Coca-Cola'),
array('id' => 3, 'price' => 50, 'quantity' => '1lt', 'name' => 'Coca-Cola'),
array('id' => 4, 'price' => 20, 'quantity' => '25mlgm', 'name' => 'Pepsi'),
array('id' => 5, 'price' => 30, 'quantity' => '500ml', 'name' => 'Pepsi'),
array('id' => 6, 'price' => 50, 'quantity' => '1lt', 'name' => 'Pepsi'));
*/
$sameproduct = false;
foreach($products as $key => $product) {
if ($key == 0 || $products[($key - 1)]['name'] != $product['name']) {
if ($key != 0) { ?>
</select>
</div>
<?php } ?>
<div class="container">
<h4><?php echo $product['name'];?></h4>
<img src="/images/LOCATION/<?php echo strtolower(str_replace(array(' ', '-'), '_', $product['name']));?>.jpg" /> <br />
<select name="unit" >
<?php } ?>
<option style='font-size:1vw;' value='<?php echo $product['id'];?>'><?php echo $product['quantity'];?> - <?php echo $product['price'];?></option>
<?php } ?>
这要求您的名称一致,并且还假定数据库中的名称与文件系统中的图像名称相匹配。它用下划线替换名称中的空格和短划线。
答案 1 :(得分:0)
尝试使用convert将字符串转换为数字,然后对其进行排序
SELECT id, Price, Quantity FROM Products ORDER BY CONVERT(Price, UNSIGNED INTEGER) ASC