Page 1 of 1
Issue with multiple criteria Search
Posted: Sun Nov 01, 2009 11:22 pm
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`";
Re: Issue with multiple criteria Search
Posted: Sun Nov 01, 2009 11:53 pm
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`";
Re: Issue with multiple criteria Search
Posted: Mon Nov 02, 2009 12:12 am
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.
Re: Issue with multiple criteria Search
Posted: Mon Nov 02, 2009 8:25 pm
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.
Re: Issue with multiple criteria Search
Posted: Mon Nov 02, 2009 9:41 pm
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.
Re: Issue with multiple criteria Search
Posted: Wed Nov 04, 2009 9:03 pm
by edawson003
ahhhh! I like it. I'll try that. Thx