SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Iron Speed
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
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  
Old April 28th, 2008, 08:43 AM
alastairdunn alastairdunn is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 3 alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 h 15 m 2 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old April 28th, 2008, 08:54 AM
Wolffy's Avatar
Wolffy Wolffy is offline
Slaprentice of Wolves
Click here for more information.
 
Join Date: Aug 2007
Location: Mossville, IL
Posts: 1,015 Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 3 m 50 sec
Reputation Power: 329
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.

Reply With Quote
  #3  
Old April 28th, 2008, 09:14 AM
alastairdunn alastairdunn is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 3 alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 h 15 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by Wolffy
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.


mate, that looks like just the trick. but im a bit confused about how i should implement this code?

Reply With Quote
  #4  
Old April 28th, 2008, 09:27 AM
Wolffy's Avatar
Wolffy Wolffy is offline
Slaprentice of Wolves
Click here for more information.
 
Join Date: Aug 2007
Location: Mossville, IL
Posts: 1,015 Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level)Wolffy User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 3 m 50 sec
Reputation Power: 329
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.

Reply With Quote
  #5  
Old April 30th, 2008, 03:55 AM
alastairdunn alastairdunn is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 3 alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level)alastairdunn User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 h 15 m 2 sec
Reputation Power: 0
Cool the solution

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"
Comments on this post
micky agrees: Thanx for sharing the solution

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Query - General - SQL syntax for "all" in a dynamic menu


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway