SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 20th, 2005, 08:09 AM
mikajussi mikajussi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 2 mikajussi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 14 sec
Reputation Power: 0
Question several form fields for search

Hello there,

I'm fighting a problem with a search function with several form fields.

Three of the fields (below: city, dep and title)are filled out by choosing something out of a pull-down menu, two of the fields are filled out by writing.

The problem is to get all of these to function together. At the moment it works only if the user fills out all form fields. If a pull-down menu form field is left unfilled, the function doesn't find any matching rows, due to the fact that the unchosen field is interpreted as a "NULL" by the function.

How do I make the search function work even if all of the fields are not filled out, so that the unchosen would be interpreted as "whatever"? The fields that are filled out by writing (name and surname) do not mess anything up, even though left unfilled.


$sql="SELECT * FROM persons WHERE city='$city' and dep='$dep' and title='$title' and name like '%$name%' and surname like '%$surname%' ORDER BY surname, name ASC";

I'd be thankful for any help.

Reply With Quote
  #2  
Old October 20th, 2005, 10:10 AM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
You're going to need to build your query dynamically in your script based on what fields are filled out. If there's no name choice, leave out the name part of the conditional entirely.
__________________
"A pawn is the most important piece on the chessboard -- to a pawn"


Reply With Quote
  #3  
Old October 21st, 2005, 03:07 AM
mikajussi mikajussi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 2 mikajussi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 14 sec
Reputation Power: 0
Hi there Madpawn,

I've tried it by specifying it like this, one query for each situation:
if ($city==''){
$sql="SELECT * FROM persons WHERE dep='$dep' and title='$title' and name like '%$name%' and surname like '%$surname%' ORDER BY surname, name ASC";
}

The problem is, though, that even though I specified all the possible situations (if one isn't filled out - if two aren't - if none of them are and so on), it at some point didn't work anymore. Meaning I guess that I'd need one query that allows a situation where any or all of the fields are not filled out.

Edit: OK, I got it working now in the way I said I've tried before. Must have left something out earlier.

Last edited by mikajussi : October 21st, 2005 at 06:44 AM. Reason: Solution found

Reply With Quote
  #4  
Old October 21st, 2005, 10:11 AM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
Instead of all those if's another format you may like better would be:

PHP Code:
 $query "SELECT * FROM persons WHERE 1=1";

if( isset(
$dep) && !empty($dep) )
{
  
$query .= " AND dep = '$dep'";
}

if( isset(
$title) && !empty($title) )
{
  
$query .= " AND title = '$title'";
}

//etc. 


That way, you're only adding the AND clauses you need.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > several form fields for search


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


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





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