Issue with multiple criteria Search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Issue with multiple criteria Search

Post by edawson003 »

I have a php search form with a couple fields. I want the user to be able to enter search criteria in a minimum of one field and then display the entire record from the mysql database in a HTML table. I found some code on the web but I botched something up. I get a Parse error: syntax error, unexpected T_IF in /home/claudett/public_html/membersarea/foodnexercise.php on line 349 error. Anyone care to assist?

Code: Select all

$exsearchquery = "SELECT * FROM `ExerciseLib` WHERE " 
if($_POST['extype'] == 379) $exsearchquery .= " `static` = '1' AND ";
if($_POST['extype'] == 380) $exsearchquery .= " `sport` = '1' AND ";
if($_POST['extype'] == 381) $exsearchquery .= " `outdoor` = '1' AND ";
if($_POST['extype'] == 382) $exsearchquery .= " `outset` = '1' AND ";
if($_POST['extype'] == 383) $exsearchquery .= " `other` = '1' AND ";
if($_POST['movementtype'] == 375) $exsearchquery .= " `w8_isometric` = '1' AND ";
if($_POST['movementtype'] == 376) $exsearchquery .= " `w8_eccentric` = '1' AND ";
if($_POST['movementtype'] == 377) $exsearchquery .= " `w8_concentric` = '1'";
$exsearchquery .= " order by `entereddt`";
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Issue with multiple criteria Search

Post by pbs »

Try this

Code: Select all

 
$exsearchquery = "SELECT * FROM `ExerciseLib` WHERE  1=1 "
if($_POST['extype'] == 379) $exsearchquery .= " AND `static` = '1' ";
if($_POST['extype'] == 380) $exsearchquery .= " AND  `sport` = '1' ";
if($_POST['extype'] == 381) $exsearchquery .= " AND  `outdoor` = '1' AND ";
if($_POST['extype'] == 382) $exsearchquery .= " AND  `outset` = '1' AND ";
if($_POST['extype'] == 383) $exsearchquery .= " AND  `other` = '1' ";
if($_POST['movementtype'] == 375) $exsearchquery .= " AND `w8_isometric` = '1' ";
if($_POST['movementtype'] == 376) $exsearchquery .= " AND  `w8_eccentric` = '1' ";
if($_POST['movementtype'] == 377) $exsearchquery .= " AND  `w8_concentric` = '1' ";
$exsearchquery .= " order by `entereddt`";
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Issue with multiple criteria Search

Post by McInfo »

Parse error: syntax error, unexpected T_IF in [...] on line 349
A bit of trivia: the "T" in "T_IF" stands for "Token". In the error, PHP is telling you that while it was parsing the tokens that make up your script, it found an "if" token when it was expecting something else. If you look before the unexpected "if", you will see that there is a semicolon missing from the statement on line 348.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 1:15 pm, edited 1 time in total.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Issue with multiple criteria Search

Post by edawson003 »

Thanks McInfo. I was wondering why I was still getting the error. That semicolon did the trick, though now I am getting a new error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`static` = '1' AND' at line 1. That's okay, at least now I know what to fix.

Thx for the assist attempt as well pbs.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Issue with multiple criteria Search

Post by McInfo »

With an added semicolon and without the extra "AND"s for extype 381 and 382, pbs's solution would work.

However, I suggest using switch.

Code: Select all

$query = 'SELECT * FROM `ExerciseLib` WHERE 1 = 1 ';
if (isset($_POST['extype'])) {
    switch ($_POST['extype']) {
        case '379' : $query .= 'AND `static`  = 1 '; break;
        case '380' : $query .= 'AND `sport`   = 1 '; break;
        case '381' : $query .= 'AND `outdoor` = 1 '; break;
        case '382' : $query .= 'AND `outset`  = 1 '; break;
        case '383' : $query .= 'AND `other`   = 1 '; break;
    }
}
if (isset($_POST['movementtype'])) {
    switch ($_POST['movementtype']) {
        case '375' : $query .= 'AND `w8_isometric`  = 1 '; break;
        case '376' : $query .= 'AND `w8_eccentric`  = 1 '; break;
        case '377' : $query .= 'AND `w8_concentric` = 1 '; break;
    }
}
$query .= 'ORDER BY `entereddt`';
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 1:15 pm, edited 1 time in total.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Issue with multiple criteria Search

Post by edawson003 »

ahhhh! I like it. I'll try that. Thx
Post Reply