Hello,
I have a table with each row having a different price for each good. How can I have PHP look at the rows in my table and select the price that's the lowest and the highest. So all I want it to output is: We have ____ type of goods ranging in price from $Min - $Max
Thanks
you can do this with your sql query
SELECT MAX(price) from products; SELECT MIN(price) from products
Originally posted by Huuggee you can do this with your sql query SELECT MAX(price) from products; SELECT MIN(price) from products [/B]
Originally posted by Huuggee you can do this with your sql query
[/B]
yup, and combine them into one query....
SELECT *, MIN(price) AS minprice, MAX(price) AS maxprice FROM products
SELECT *, MIN(price) AS minprice, MAX(price) AS maxprice FROM products [/B]
To make the query faster you can leave out the * like so:
<?php $sql=mysql_query("SELECT MIN(`price`) AS `minprice`, MAX(`price`) AS `maxprice` FROM `product_table` WHERE `category` = '$categoryvar' ;"); $r = mysql_fetch_array($sql) ?> Products in this category are priced from <?php echo $r['minprice']; ?> to <?php echo $r['maxprice']; ?>
You can see a demo here: http://www.discostudio.co.uk/djshop/product.php?cat_id=11&listtype=grid
Hope that helps 🙂
I only left the cause I assumed he would be using it for more than just that 1 line shrug its best to never query with unless you intend to be using all the data as previously named in the column anyways[/banter]