问题描述
以下是我所拥有的,以及我想要做的:
我的MySql数据库中有12个项目。 4个产品是4.99,4个产品是3.99,4个产品是2.99。
我意识到我可以像这样查询数据库,它会给我一个产品列表该价格:
<?php
$ query =SELECT * FROM UFPProducts WHERE price = 4.99\" ;
$ results = mysql_query($ query,$ connect);
while($ row = @ mysql_fetch_array($ results))
{
print
< div id ='item'> 。
< p>产品ID。$ row [ProductID]。< / p>。
< p>< img src =。$ row [Image]。>< / p>。
< p>& pound。$ row [Price]。< / p>。
< p>。$ row [Description]。< / p>。
< / div>;
}
?>
然而,我想要的是一个按钮,我可以按价格排序产品。
做这件事的简单方法是什么?
谢谢 试试这个查询
$ query =SELECT * FROM UFPProducts order by price ASC;
以上查询将按升序列出所有价格。但是,如果您想将不同的价格分组在一起,请使用以下查询。
$ query =SELECT * FROM UFPProducts group by价格按价格排序ASC;
Here's what I have, and what I want to do:
I have 12 items in my MySql database. 4 products are 4.99, 4 products are 3.99 and 4 products are 2.99.
I realize I can query the database like this, and It will give me a list of products at that price:
<?php
$query = "SELECT * FROM UFPProducts WHERE price = 4.99";
$results = mysql_query ($query, $connect);
while ($row = @ mysql_fetch_array($results))
{
print
"<div id= 'item'>" .
"<p>Product id ".$row["ProductID"]."</p>".
"<p><img src=".$row["Image"]."></p>".
"<p>£".$row["Price"]."</p>".
"<p>".$row["Description"]."</p>".
"</div>";
}
?>
However, what I want, is a button I can press to sort products by price.
What's a simple way of doing this?
Thanks
Try this query
$query = "SELECT * FROM UFPProducts order by price ASC";
The above query will list all price in ascending order. But if you want to have distinct price grouped together, use the below query.
$query = "SELECT * FROM UFPProducts group by price order by price ASC";
这篇关于通过mysql数据以高低价格进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!