Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 07-06-2005, 04:10 PM Searching fields
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
'lo

Im trying to make a search script to search through a database based on various fields. I want users to be able to use a dropdown list to choose a value and then click search (there will be about 6-7 fields). The problem is that if they don't pick a value and leave it at the "default" value then it shouldn't be included in the mysql query. I know this could be done with one monster load of IF statements and the like but I wondered if there is a more easy and efficient than that.

If you need more info then just tell me cos id really like some help.

Cheers
Stoot
stoot98 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-07-2005, 01:26 AM
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
I'm also having this problem. With

PHP Code:
$sql_text "SELECT * FROM ".$this->TABLES['statements']." WHERE company = '".$c."' AND month = '".$m."' AND year = '".$y."' ORDER BY id"
It works fine when $m and $y equal something, but I would like them to be only work with 1 selected.
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 07-07-2005, 09:18 AM
Novice Talker

Posts: 8
Trades: 0
The complexity depends on whether the different boxes access different tables. If they access the same table a good way to tackle this is to load the values into an associative array and then read the array. So an example from a piece of my code that does more or less the same thing would be:

if (!empty($LNAME)) { $textb["LNAME"] = "$LNAME"; }
if (!empty($FNAME)) { $textb["FNAME"] = "$FNAME"; }
if (!empty($ONAME)) { $textb["ONAME"] = "$ONAME"; }
if (!empty($PSEUD)) { $textb["PSEUD"] = "$PSEUD"; }
}

Obviously you would substitute the !empty for '!='. Just make sure the key in the array is the same as the field in the table. This will give you an array with only the set values. Then read the array and build an SQL query like

$CNT = 0;
foreach ($textb as $key => $val) {
$KVAL = "$key = '$val'" ;
if ($CNT == 0 {$CSTAT = "where $KVAL" ;
$CNT++ ;}
else {$CSTAT = "$CSTAT and $KVAL" ;}
}

This builds a conditional statement where on the first iteration you get the where statement and each additional array element gives an 'and' statement. If the select statement depends on the values submitted, it can be built in the same way. If the boxes reference joined tables it becomes more complex, but it can be done by seperating the arrays into one per table that is joined. Also it is fairly easy to substitute other conditions such as != or 'like' if required. Hope this is clear enough.

Bob
bobfarq is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Searching fields
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12018 seconds with 12 queries