|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
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:
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'. ![]() |
|
#2
|
||||
|
||||
|
I don't know if this is the best solution, but it doesn't hurt to try =)
Try this: PHP Code:
Notice, I added: if(!empty($Where)) $Where .= 'AND'; and removed the word AND from your $Where variables. |
|
#3
|
|||
|
|||
|
Thanks MadCowDzz
Worked great for what I needed if for.... ![]() |
|
#4
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Building Queries From Url |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|