|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Query - General - SQL syntax for "all" in a dynamic menu
I'm using php for my webpages and I have built an sql query which is as follows.
"SELECT * FROM core_db WHERE (product_no LIKE %s OR model LIKE %s) AND brand IN ('%s')" The query has just two variables, both coming from a webpage form. The first is a text field and any string entry into this can match (vaguely) either product_no OR model. The second is a dynamic menu through which the results can be filtered by brand. This works just fine as it is but I want to add the functionality of being able to set the brand to "all" by default (or choice). So essentially I want the part of the query from "AND" onwards to be optional or is there some syntax which I can put manually into the dynamic menu which will equal "all" when the variable inserts it? My logic told me that using a "*" should work since this symbol represents all in the SELECT part of sql queries but I've given this a go and that doesn't work. My only other idea is if I could replace the whole end part of the query with a variable and set if statements outside of the query in the php, if this even possible ofcourse? It all seems like it must be the simplest thing to achieve, but its always the simple bits that end up taking ages to sort out. thanks for any help. |
|
#2
|
||||
|
||||
|
You on the right track with the IF statement. To return all brands, you would need to leave off the AND part of the query:
Code:
sql = "SELECT * FROM core_cd WHERE (product_no LIKE %s OR model LIKe %s)" if brand <> "ALL" sql += " AND brand in (%s)" note space before the AND.
__________________
Wolffy ------------------------ Teaching people to fish. |
|
#3
|
|||
|
|||
|
Quote:
mate, that looks like just the trick. but im a bit confused about how i should implement this code? |
|
#4
|
||||
|
||||
|
I missed the reference to PHP in you post -- sorry, I'm not a PHP whiz. Not even sure how you execute an SQL statement there. However, it has to have some control logic (IF THEN ELSE) and string concat functions(?), and I imagine this logic would after you determine the value of the brand and before you execute it command -- pretty vague I know, but don't know PHP a whit.
|
|
#5
|
|||
|
|||
|
in case anybody looks at this for the solution im posting it.
it was pretty obvious i just had to think about it along the lines wolfy gave me, but for php. What i didnt realise is that you can replace any part of an sql query with a variable. so i just put a php variable into the sql query and then set outside the value of the variable to be either null if drop down menu has 'all' in it or if its value is 'all' to add the $brand_extension variable to the sql query. thank f*** for that The code is as follows.Code:
//set variable
$brand_extension = null;
if ($brand_filter <> 'all') {
$brand_extension = " AND brand IN ('$brand_filter')";
} elseif ($brand_filter == 'ALL') {
$brand_extension = null;
//sql query
$query_rs_products = sprintf("SELECT * FROM core_db WHERE (product_no LIKE %s OR model LIKE %s)$brand_extension"
|
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Query - General - SQL syntax for "all" in a dynamic menu |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|
|