PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP 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:
  #1  
Old March 4th, 2005, 10:25 AM
zigote zigote is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Atlanta GA
Posts: 73 zigote User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 11 sec
Reputation Power: 7
Building Queries From Url

The form below is an example is what I need to do, The user has the option to filter data based off a select menu.
If the user doesn't "select" from each section of the menu, the "AND" of the statement gets placed where it causes errors.

Html Form Example
Code:
 
<form action="browse.php" method="POST" class="forms">
    <select name="CatId" class="formSelect125" size="1">
        <option value="All">All Categories</option>
        <option value="1">Example One</option>
        <option value="2">Example Two</option>
    </select>
    <select name="AuthorId" class="formSelect125" size="1">
        <option value="All">All Authors</option>
        <option value="1">Example One</option>
        <option value="2">Example Two</option>
    </select>
    <select name="Difficulty" class="formSelect125" size="1">
        <option value="All">All Difficulties</option>
        <option value="1">Example One</option>
        <option value="2">Example Two</option>
    </select>
    <input type="submit" class="formButtons" value="Filter">
</form>


As you can see from the above form the Url's would be sent as
browse.php?CatId=$CatId&AuthorId=$AuthorId&Difficulty=$Difficulty
If no menu was selected then the variable would be set to 'All'.
Example filter from CatId 1 only
browse.php?CatId=1&AuthorId=All&Difficulty=All

Now for the PHP...

PHP Code:
// Get the varibles
$CatId = (isset($_REQUEST['CatId'])) ? $_REQUEST['CatId'] : $CatId;
$AuthorId = (isset($_REQUEST['AuthorId'])) ? $_REQUEST['AuthorId'] : $AuthorId;
$Difficulty = (isset($_REQUEST['Difficulty'])) ? $_REQUEST['Difficulty'] : $Difficulty;

// If any variable is empty, then change to 'All',
if ($CatId == "")
    
$CatId "All";
if (
$AuthorId == "")
    
$AuthorId "All";
if (
$Difficulty == "")
    
$Difficulty "All"


Ok, Here's where I' am getting confused, I need this to only set 'AND' if two or more selects have been made.
Build the query
PHP Code:
 $Condition "SELECT * FROM MyTable";

    if (empty(
$CatId) OR $_GET["CatId"] == 'All')
    {
    } else {
        
$Where .= " CatId = '$CatId' AND ";
    }

    if (empty(
$AuthorId) OR $_GET["AuthorId"] == 'All')
    {
    } else {
        
$Where .= " AuthorId = '$AuthorId' AND ";
    }

    if (empty(
$Difficulty) OR $_GET["Difficulty"] == 'All')
    {
    } else {
        
$Where .= "Difficulty = $Difficulty ";
    }
    
$Condition .= " WHERE " $Where;

    
// Print the query
    
Print "$Condition"


Now, This is all fine, If the user selects all the options.
Example of all options selected
SELECT * FROM MyTable WHERE CatId = '1' AND AuthorId = '1' AND Level = 1

When the user only selects 2 options, it would then remove one, but the last option would leave the 'AND' which would cause an error. Like so...

SELECT * FROM MyTable WHERE CatId = '1' AND AuthorId = '1' AND

What can I do, so the user could select any option in any order and the 'Statement' would know where to place or remove the 'AND'.

Reply With Quote
  #2  
Old March 7th, 2005, 09:06 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I don't know if this is the best solution, but it doesn't hurt to try =)

Try this:
PHP Code:
 $Condition "SELECT * FROM MyTable";

    if (empty(
$CatId) OR $_GET["CatId"] == 'All')
    {
    } else {
        if(!empty(
$Where)) $Where .= 'AND';
        
$Where .= " CatId = '$CatId'";
    }

    if (empty(
$AuthorId) OR $_GET["AuthorId"] == 'All')
    {
    } else {
        if(!empty(
$Where)) $Where .= 'AND';
        
$Where .= " AuthorId = '$AuthorId'";
    }

    if (empty(
$Difficulty) OR $_GET["Difficulty"] == 'All')
    {
    } else {
        if(!empty(
$Where)) $Where .= 'AND';
        
$Where .= "Difficulty = $Difficulty ";
    }
    
$Condition .= " WHERE " $Where;

    
// Print the query
    
Print "$Condition"


Notice, I added: if(!empty($Where)) $Where .= 'AND';
and removed the word AND from your $Where variables.

Reply With Quote
  #3  
Old March 8th, 2005, 07:53 AM
zigote zigote is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Atlanta GA
Posts: 73 zigote User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 11 sec
Reputation Power: 7
Thanks MadCowDzz
Worked great for what I needed if for....

Reply With Quote
  #4  
Old March 8th, 2005, 09:44 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Technically that first if(!empty) statement in my code doesn't need to be there... $Where should always be empty at that point. I put it there in case you added new WHERE clause stuff before the CatId stuff...

Glad I could help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Building Queries From Url


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 6 hosted by Hostway
Stay green...Green IT