![]() |
PHP -MYSQL Aprox price Calc help
Hi,
I have a table in MYSQL that has a column named price. I have multiple other columns in the table that have type, subtype, ssubtype. etc. eg: Table Bob ==================================== type | subtype | ssubtype | price ==================================== 1 | 2 | 1 | 40 1 | 2 | 2 | 50 1 | 2 | 3 | 60 2 | 3 | 2 | 10 I would like to know how i would go about selecting the lowest and highest price of an item given there may be 2+ versions of that product as above where there are 3 type 1's and the price ranges from 40-60. That is the info i would like to display in a form just the 40-60. My initial select statement looks like the below that gathers input from user defined variables. PHP Code:
PHP Code:
What this produces is just a list like the following: Aprox Lowest Price | Aprox Highest Price $40.00 | $40.00 $50.00 | $50.00 $60 | $60.00 but what i want is: Aprox Lowest Price | Aprox Highest Price $40.00 | $60.00 Hope someone can help out as i'm new to this.. ps I want to then go on and provide a button that shows some of the other fields of the initial user defined variables if the user wants to know ho can supply the product type. :) |
This is more of a SQL question.
Use the MIN and MAX aggregates: Code:
SELECT type, MIN(price) AS minprice, MAX(price) AS maxprice FROM bob GROUP BY type WHERE type='$drop' AND subtype='$drop_2' AND ssubtype='$drop_3' |
Thanks for the reply but when i use your code it fails as per below:
*********************** Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\index.php on line 134 *********************** line 134 is the following: PHP Code:
PHP Code:
Aprox Lowest Price Aprox Highest Price $40.00 $50.00 is there something i'm missing ? |
Yeah you need the group by.
My bad though, the group by clause comes after the where clause, not before; I pasted the copy into the wrong place. Switch them around and it should work. 'type' is not required within the select, but helps to keep track of the data you need. |
Yep the query now works.. :thumbsup:
Any idea how i then go about adding a button to display the additional fields within each row if the user so desires ? The code to display the table would be this: PHP Code:
|
Depends on what you mean by additional fields.
You can get it to display however you want, but PHP's job is server side so it only works in request/response approaches. What you would do is either use AJAX or simply populate the entire table with data you need, and use javascript or jquery or whatever to hide and show the columns. PHP would need to keep getting information on what to show. |
ok so the additional fields(Column names) are as above/below:
sprovidername sprovideraddress sproviderurl semail ssubcategorytype1 Which have already been selected and defined within the $Drop, $Drop_1 etc. variables chosen by the user. So flow would be like so: User selects each drop down box and enters desired options. This then displays lowest/highest price (which we got working above with your help) A button then displays that would give the user an option to display further details about the rows that are defined between lowest/higest price. hope that makes sense.. :) |
..bump..
|
Okay, I may be a bit confused here. So you are just wanting to add like, a link to let you fetch specific data?
The query would be pretty much identical to the one I posted, but without the min/max aggregates and no group by. That would give all matching results of type, subtype and ssubtype. That would be all the records. You could probably use the HAVING clause with a group by if you wanted to remove the min/max off of it as well, but that's really a question for the SQL guys as they can tell you what to do for that. |
Quote:
The user enters there search createria and it produces a list of possible prices between $X and $Y. (This I have completed already) After this i need a button to appear that the user can then submit and display further details of the items the price range may include: this would be the following code: PHP Code:
hope that makes sense.:) |
...Bump...
|
...Bump...
|
I still don't know what you are looking for here.
What is the relationship here between this last block of code and the use of MIN and MAX? Are you dealing with multiple pages here for results or what? |
| All times are GMT +1. The time now is 06:12 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.