bickyz Posted March 18, 2013 Share Posted March 18, 2013 hi, for my school intranet im trying to create a page that will search and list the activities.i am trying to create a search page with the multiple form elements that will filter and list the records from the mysql database. So far i have been using a single field search and I have no idea where to start this multi search project. Here is the demo of how I currently search http://bickyz.byethost13.com/oldsearch1.php (search for the workd Hiking) This is a http://bickyz.byethost13.com/index.php new search page I am trying to create. Currently it can only search by the duration drop down but I would like multiple form elements to be used. Following is my Database. mysql table: teamleadermysql table: activities index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Intranet Activities Search</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css" /> <script> $(function() { $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'}); }); </script> <style type="text/css" media="screen"> table { font-size: 14px; } </style> </head> <body> <form id="form1" name="form1" method="post" action="listactivities.php"> <table width="400" border="0" cellspacing="0" cellpadding="0"> <tr> <tr> <tr> <td>Duration (days)</td> <td><select name="duration" id="duration"> <option selected="selected" value="">Any</option> <option value="7">7</option> <option value="8">8</option> <option value="9">8</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> </select></td> </tr> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td width="45%">Date (YYYY-MM-DD)</td> <td width="55%"><input type="text" id="datepicker" name="datepicker" /> </td> </tr> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Transportation</td> <td><select name="transport" id="transport"> <option selected="selected" value="0">Any</option> <option value="included">included</option> <option value="excluded">excluded</option>> </select></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Fishing</td> <td><input name="chkfishing" type="checkbox" id="chkfishing" value="Fishing" /></td> </tr> <tr> <td>Gliding</td> <td><input name="chkgliding" type="checkbox" id="chkgliding" value="Gliding" /></td> </tr> <tr> <td>Flying</td> <td><input name="chkflying" type="checkbox" id="chkflying" value="Flying" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><input type="submit" name="searchbtn" id="searchbtn" value="Search" /></td> <td> </td> </tr> </table> </form> </body> </html> listactivities.php <?php ini_set('display_errors',1); error_reporting(E_ALL); ?> <?php $dbhost = 'aaa'; $dbuser = 'bbb'; $dbpass = 'ccc'; $dbname = 'ddd'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Intranet Activities Search</title> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <?php $search=$_POST["duration"]; if ($search == "") { echo "<p>You forgot to enter a search term"; exit; } $result = mysql_query("SELECT * FROM activities WHERE acdur LIKE '%$search%'"); if(mysql_num_rows($result)) { $i=1; echo '<table><tr>'; while($r=mysql_fetch_array($result)) { $tleaderid=$r["tlid2"]; $title=$r["actitle"]; $duration=$r["acdur"]; $includes=$r["acincl"]; $exurl=$r["acurl"]; $startdate=$r["acdate"]; $transport=$r["actransp"]; $cost=$r["accost"]; $info=$r["acinfo"]; $acid=$r["acid"]; echo '<td style="padding: 10px; border-right: solid 2px #990000; border-bottom: solid 2px #990000; background-color: #eee;">'." <div><b><u>ACTIVITIES</u></b></div> <div><b>Title: </b>$title</div> <div><b>Duration: </b>$duration <b>days</b></div> <div><b>Activities Includes: </b>$includes</div> <div><b>External Website: </b><a href=$exurl>click here</a></div> <div><b>Start Date: </b>$startdate</div> <div><b>Transportation: </b>$transport</div> <div><b>Cost: £</b>$cost</div> <div><b>Team Leader ID: </b>$tleaderid</div> <div><b>Overview: </b>$info</div><br> ".'</td>'; if($i % 3 == 0) echo '</tr><tr>'; $i++; } echo '</tr></table>'; } else { echo '<div align=center style="margin:20px; font-family:Arial, Helvetica, sans-serif; font-size: 20px; font-weight:bold; color: #ae1919;">Your search did not match any results.</div>'; } ?> </td> </tr> </table> </body> </html> <?php mysql_free_result($result); ?> In the display result I would like: ACTIVITIESTitle:Duration:Activities Includes:External Website: Start Date:Transportation:Cost:Overview:Team Leader Name: (this info to come from teamleader table) Team Leaer Phone: (this info to come from teamleader table)Team Leader Email: (this info to come from teamleader table) I am not a php developer but have good understanding of modifying file for my needs. Any help would be much appreciated, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/ Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 You have a couple of design problems. The acincl field - don't store multiple values in a single field. Normalize you data correctly. Checkboxes in form for activity - give the checkboxes the same name, say, "activity[]". This will post them as an array and facilitate processing. To process the search: <?php $where = array(); $whereclause = ''; if (!empty($_POST['duration'])) { $val = intval($_POST['duration']); $where[] = "(acdur = $val)"; } if (!empty($_POST['transport'])) { $val = mysql_real_escape_string($_POST['transport']); $where[] = "(actransp = '$val')"; } // assuming you change you checkbox names if (isset($_POST['activity'])) { $val = join("','", array_map('mysql_real_escape_string', $_POST['activity'])); $where[] = "(activity IN ('$val'))"; } if (count($where)) $whereclause = "WHERE " join(' AND ', $where); // and you search query is $sql = "SELECT what, you, need FROM activities $whereclause"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419444 Share on other sites More sharing options...
bickyz Posted March 19, 2013 Author Share Posted March 19, 2013 hi barand, thank youI have now changed the table to include separate fields for activities (Fishing, Gliding, Flying). How will be the search code, your help will be much appreciated, thank you. index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Intranet Activities Search</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css" /> <script> $(function() { $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'}); }); </script> <style type="text/css" media="screen"> table { font-size: 14px; } </style> </head> <body> <form id="form1" name="form1" method="post" action="listactivities.php"> <table width="400" border="0" cellspacing="0" cellpadding="0"> <tr> <tr> <tr> <td>Duration (days)</td> <td><select name="duration" id="duration"> <option selected="selected" value="">Any</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> </select></td> </tr> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td width="45%">Date (YYYY-MM-DD)</td> <td width="55%"><input type="text" id="datepicker" name="datepicker" /> </td> </tr> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Transportation</td> <td><select name="transport" id="transport"> <option selected="selected" value="0">Any</option> <option value="included">included</option> <option value="excluded">excluded</option>> </select></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Fishing</td> <td><input name="chkact[]" type="checkbox" id="chkfishing" value="YES" /></td> </tr> <tr> <td>Gliding</td> <td><input name="chkact[]" type="checkbox" id="chkgliding" value="YES" /></td> </tr> <tr> <td>Flying</td> <td><input name="chkact[]" type="checkbox" id="chkflying" value="YES" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><input type="submit" name="searchbtn" id="searchbtn" value="Search" /></td> <td> </td> </tr> </table> </form> </body> </html> listactivities.php <?php ini_set('display_errors',1); error_reporting(E_ALL); ?> <?php $dbhost = 'aa'; $dbuser = 'bb'; $dbpass = 'cc'; $dbname = 'dd'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Intranet Activities Search</title> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <?php $where = array(); $whereclause = ''; if (!empty($_POST['duration'])) { $val = intval($_POST['duration']); $where[] = "(acdur = $val)"; } if (!empty($_POST['datepicker'])) { $val = mysql_real_escape_string($_POST['datepicker']); $where[] = "(acdate = '$val')"; } if (!empty($_POST['transport'])) { $val = mysql_real_escape_string($_POST['transport']); $where[] = "(actransp = '$val')"; } // assuming you change you checkbox names if (isset($_POST['chkact'])) { $val = join("','", array_map('mysql_real_escape_string', $_POST['chkact'])); $where[] = "(chkact IN ('$val'))"; } if (count($where)) $whereclause = "WHERE " join(' AND ', $where); // and you search query is $result = "SELECT * FROM activities $whereclause"; ?> <?php //$search=$_POST["duration"]; //if ($search == "") //{ //echo "<p>You forgot to enter a search term"; //exit; //} //$result = mysql_query("SELECT * FROM activities WHERE acdur LIKE '%$search%'"); if(mysql_num_rows($result)) { $i=1; echo '<table><tr>'; while($r=mysql_fetch_array($result)) { $tleaderid=$r["tlid2"]; $title=$r["actitle"]; $duration=$r["acdur"]; $includes=$r["acincl"]; $exurl=$r["acurl"]; $startdate=$r["acdate"]; $transport=$r["actransp"]; $cost=$r["accost"]; $info=$r["acinfo"]; $acid=$r["acid"]; echo '<td style="padding: 10px; border-right: solid 2px #990000; border-bottom: solid 2px #990000; background-color: #eee;">'." <div><b><u>ACTIVITIES</u></b></div> <div><b>Title: </b>$title</div> <div><b>Duration: </b>$duration <b>days</b></div> <div><b>Activities Includes: </b>$includes</div> <div><b>External Website: </b><a href=$exurl>click here</a></div> <div><b>Start Date: </b>$startdate</div> <div><b>Transportation: </b>$transport</div> <div><b>Cost: £</b>$cost</div> <div><b>Team Leader ID: </b>$tleaderid</div> <div><b>Overview: </b>$info</div><br> ".'</td>'; if($i % 3 == 0) echo '</tr><tr>'; $i++; } echo '</tr></table>'; } else { echo '<div align=center style="margin:20px; font-family:Arial, Helvetica, sans-serif; font-size: 20px; font-weight:bold; color: #ae1919;">Your search did not match any results.</div>'; } ?> </td> </tr> </table> </body> </html> <?php mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419510 Share on other sites More sharing options...
Barand Posted March 19, 2013 Share Posted March 19, 2013 (edited) you have a situation where an activity has many inclusions and an inclusion is in many activities therefor you need +-------------+ +---------------+ +------------+ | activities | | incl_activity | | inclusion | +-------------+ +---------------+ +------------+ | acid | ------< | acid | +-- | inclid | | tlid2 | | inclid |>--+ | description| | actitle | +---------------+ +------------+ | acdur | | acdate | | etc | +-------------+ Edited March 19, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419556 Share on other sites More sharing options...
bickyz Posted March 19, 2013 Author Share Posted March 19, 2013 hi, i have done the tables as is this correct way ? Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419574 Share on other sites More sharing options...
Barand Posted March 19, 2013 Share Posted March 19, 2013 That's good. You can now query the inclusion table to create your list of checkboxes with name activity[] and values will be the incID of each record The code I gave earlier will change slightly now they will have numeric values instead of 'gliding' etc // assuming you change you checkbox names if (isset($_POST['activity'])) { $val = join(",", array_map('intval', $_POST['activity'])); $where[] = "(incid IN ($val))"; } Your search query would now join activites with incl_activity on acid and join to teamleader also Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419583 Share on other sites More sharing options...
bickyz Posted March 19, 2013 Author Share Posted March 19, 2013 hi, i have amended the code but nothing is displayed on the results page http://bickyz.byethost13.com/index.php Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419590 Share on other sites More sharing options...
Barand Posted March 19, 2013 Share Posted March 19, 2013 Can you echo the query string that you are executing? Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419604 Share on other sites More sharing options...
bickyz Posted March 19, 2013 Author Share Posted March 19, 2013 it is not echo-ing anything, page is blank but if i disable all the php code except the code between "test code starts - ends" then the page says Hello Array When all the codes are enabled dreamweaver says there is a syntax error on this line if (count($where)) $whereclause = "WHERE " join(' AND ', $where); <?php //test code starts ini_set('display_errors',1); error_reporting(E_ALL); ?> <?php if (! empty($_POST['activity'])){ echo 'Hello, ' . $_POST['activity']; } //test code ends ?> <?php $dbhost = 'aa'; $dbuser = 'bb'; $dbpass = 'cc'; $dbname = 'dd'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); ?> <?php $where = array(); $whereclause = ''; if (!empty($_POST['duration'])) { $val = intval($_POST['duration']); $where[] = "(acdur = $val)"; } if (!empty($_POST['transport'])) { $val = mysql_real_escape_string($_POST['transport']); $where[] = "(actransp = '$val')"; } // assuming you change you checkbox names if (isset($_POST['activity'])) { $val = join(",", array_map('intval', $_POST['activity'])); $where[] = "(incid IN ($val))"; } if (count($where)) $whereclause = "WHERE " join(' AND ', $where); // and you search query is $query = "SELECT * FROM activities $whereclause"; $test= mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($test)){ echo $row['acid']; echo $row['actitle']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419657 Share on other sites More sharing options...
Zane Posted March 19, 2013 Share Posted March 19, 2013 (edited) You're missing a concatenation period in between WHERE " ... and join( You are seeing Hello Array because $_POST['activity'] is an array... as Barrand suggested you did. Edited March 19, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419664 Share on other sites More sharing options...
Barand Posted March 19, 2013 Share Posted March 19, 2013 I missed out the dot before "join" if (count($where)) $whereclause = "WHERE " . join(' AND ', $where); Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419682 Share on other sites More sharing options...
bickyz Posted March 19, 2013 Author Share Posted March 19, 2013 (edited) thank you to Zane and Barand. If i select one check box and do search then it says Column 'incid' in where clause is ambiguous If i leave all check box unticked and do search then whole pile of data are dsiplayed, demo here http://bickyz.byethost13.com/ Following is the php part of my code. <?php $where = array(); $whereclause = ''; if (!empty($_POST['duration'])) { $val = intval($_POST['duration']); $where[] = "(acdur = $val)"; } if (!empty($_POST['transport'])) { $val = mysql_real_escape_string($_POST['transport']); $where[] = "(actransp = '$val')"; } // assuming you change you checkbox names if (isset($_POST['activity'])) { $val = join(",", array_map('intval', $_POST['activity'])); $where[] = "(incid IN ($val))"; } if (count($where)) $whereclause = "WHERE " . join(' AND ', $where); // and you search query is $query = "SELECT * FROM activities, incl_activity, inclusion $whereclause"; $test= mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($test)){ echo $row['acid']; echo "<br />"; echo $row['actitle']; } ?> In the Form for the check box, do I put 1 in the value ? <tr> <td>Fishing</td> <td><input name="activity[]" type="checkbox" id="chkfishing" value="1" /></td> </tr> <tr> <td>Gliding</td> <td><input name="activity[]" type="checkbox" id="chkgliding" value="1" /></td> </tr> <tr> <td>Flying</td> <td><input name="activity[]" type="checkbox" id="chkflying" value="1" /></td> </tr> Edited March 19, 2013 by bickyz Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419700 Share on other sites More sharing options...
Barand Posted March 19, 2013 Share Posted March 19, 2013 In the Form for the check box, do I put 1 in the value ?<tr><td>Fishing</td> <td><input name="activity[]" type="checkbox" id="chkfishing" value="1" /></td> </tr> <tr> <td>Gliding</td> <td><input name="activity[]" type="checkbox" id="chkgliding" value="1" /></td> </tr> <tr> <td>Flying</td> <td><input name="activity[]" type="checkbox" id="chkflying" value="1" /></td> </tr> no, the id You can now query the inclusion table to create your list of checkboxes with name activity[] and values will be the incID of each record Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419707 Share on other sites More sharing options...
bickyz Posted March 20, 2013 Author Share Posted March 20, 2013 hi barand, thank you. I have changed all the checkbox to have incID in the value, now when doing a search it is listing quite large amount of data. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419762 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 That is because of your query. $query = "SELECT * FROM activities, incl_activity, inclusion $whereclause"; You have not specified any JOIN criteria so every record in each table is joined with every record in the others. If each table has 100 rows there will be 100x100x100 rows returned ie 1,000,000 rows Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419771 Share on other sites More sharing options...
bickyz Posted March 20, 2013 Author Share Posted March 20, 2013 hi barand, i have changed sql query to: $query = "SELECT * FROM activities JOIN incl_activity on activities.acid = incl_activity.acid JOIN inclusion on incl_activity.incid = inclusion.incid JOIN teamleader on activities.tlid2 = teamleader.tlid $whereclause"; If I deselect the checkboxes and search then it will list the results whereas if I select chkbox then results page is blank. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419838 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 As you added the inclusions table you will have to specify which incid to use if (isset($_POST['activity'])) { $val = join(",", array_map('intval', $_POST['activity'])); $where[] = "(incl_activity.incid IN ($val))";} Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419850 Share on other sites More sharing options...
bickyz Posted March 20, 2013 Author Share Posted March 20, 2013 I tried that already but still no results <?php $where = array(); $whereclause = ''; if (!empty($_POST['duration'])) { $val = intval($_POST['duration']); $where[] = "(acdur = $val)"; } if (!empty($_POST['transport'])) { $val = mysql_real_escape_string($_POST['transport']); $where[] = "(actransp = '$val')"; } // assuming you change you checkbox names if (isset($_POST['activity'])) { $val = join(",", array_map('intval', $_POST['activity'])); $where[] = "(incl_activity.incid IN ($val))"; } if (count($where)) $whereclause = "WHERE " . join(' AND ', $where); // and you search query is /*$query = "SELECT * FROM activities, incl_activity, inclusion JOIN incid $whereclause";*/ $query = "SELECT * FROM activities JOIN incl_activity on activities.acid = incl_activity.acid JOIN inclusion on incl_activity.incid = inclusion.incid JOIN teamleader on activities.tlid2 = teamleader.tlid $whereclause"; $test= mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($test)){ echo "<b>Title: </b>"; echo $row['actitle']; echo "<br />"; echo "<b>Team Leader: </b>"; echo $row['tlname']; echo "<br />"; echo "<b>Start Date: </b>"; echo $row['acdate']; echo "<br />"; echo "<b>Cost: </b>"; echo $row['accost']; echo "<br />"; echo "<b>Incl. Activities: </b>"; echo $row['incdesc']; echo "<br />"; echo "------------"; echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419859 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 what does this give echo $query; Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419862 Share on other sites More sharing options...
bickyz Posted March 20, 2013 Author Share Posted March 20, 2013 If I select no checkboxes then it gives: SELECT * FROM activities JOIN incl_activity on activities.acid = incl_activity.acid JOIN inclusion on incl_activity.incid = inclusion.incid JOIN teamleader on activities.tlid2 = teamleader.tlid If I select all three checkboxes then it gives:SELECT * FROM activities JOIN incl_activity on activities.acid = incl_activity.acid JOIN inclusion on incl_activity.incid = inclusion.incid JOIN teamleader on activities.tlid2 = teamleader.tlid WHERE (incl_activity.incid IN (0,0,0)) If I select two checkboxes then it gives: SELECT * FROM activities JOIN incl_activity on activities.acid = incl_activity.acid JOIN inclusion on incl_activity.incid = inclusion.incid JOIN teamleader on activities.tlid2 = teamleader.tlid WHERE (incl_activity.incid IN (0,0)) If I select onecheckboxes then it gives: SELECT * FROM activities JOIN incl_activity on activities.acid = incl_activity.acid JOIN inclusion on incl_activity.incid = inclusion.incid JOIN teamleader on activities.tlid2 = teamleader.tlid WHERE (incl_activity.incid IN (0)) Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419928 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 Then your checkboxes have 0 values instead of the incid of the inclusion Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419932 Share on other sites More sharing options...
bickyz Posted March 20, 2013 Author Share Posted March 20, 2013 (edited) my checkbox has value="incid", just for the test I tried changin value="testing" but still it gives same as before. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Intranet Activities Search</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css" /> <script> $(function() { $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'}); }); </script> <style type="text/css" media="screen"> table { font-size: 14px; } </style> </head> <body> <form id="form1" name="form1" method="post" action="listactivities.php"> <table width="400" border="0" cellspacing="0" cellpadding="0"> <tr> <td>Duration (days)</td> <td><select name="duration" id="duration"> <option selected="selected" value="">Any</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> </select></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td width="45%">Date (YYYY-MM-DD)</td> <td width="55%"><input type="text" id="datepicker" name="datepicker" /> </td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Transportation</td> <td><select name="transport" id="transport"> <option selected="selected" value="0">Any</option> <option value="included">included</option> <option value="excluded">excluded</option>> </select></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Fishing</td> <td><input name="activity[]" type="checkbox" id="chkfishing" value="incid" /></td> </tr> <tr> <td>Gliding</td> <td><input name="activity[]" type="checkbox" id="chkgliding" value="incid" /></td> </tr> <tr> <td>Flying</td> <td><input name="activity[]" type="checkbox" id="chkflying" value="incid" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><input type="submit" name="searchbtn" id="searchbtn" value="Search" /></td> <td> </td> </tr> </table> </form> </body> </html> Edited March 20, 2013 by bickyz Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419939 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 if Fishing, Gliding and Flying have incids of 1 2 3 then those are the values the checkboxes should have. Not the string "incid". As I suggested earlier, query the table to produce the checkboxes Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419942 Share on other sites More sharing options...
bickyz Posted March 20, 2013 Author Share Posted March 20, 2013 thank you very much Barand, now it displays results based on the form element selections. currently results are displayed in one column i.e. RESULT1 RESULT2 RESULT3 RESULT4 RESULT5 RESULT6 how can i display in three columns likeRESULT1 RESULT2 RESULT3 RESULT4 RESULT5 RESULT6 also if there is not results I would like to display a mesage like "no results found", how can i achieve this. <?php $where = array(); $whereclause = ''; if (!empty($_POST['duration'])) { $val = intval($_POST['duration']); $where[] = "(acdur = $val)"; } if (!empty($_POST['transport'])) { $val = mysql_real_escape_string($_POST['transport']); $where[] = "(actransp = '$val')"; } if (!empty($_POST['datepicker'])) { $val = mysql_real_escape_string($_POST['datepicker']); $where[] = "(acdate = '$val')"; } // assuming you change you checkbox names if (isset($_POST['activity'])) { $val = join(",", array_map('intval', $_POST['activity'])); $where[] = "(incl_activity.incid IN ($val))"; } if (count($where)) $whereclause = "WHERE " . join(' AND ', $where); // and you search query is /*$query = "SELECT * FROM activities, incl_activity, inclusion JOIN incid $whereclause";*/ $query = "SELECT * FROM activities JOIN incl_activity on activities.acid = incl_activity.acid JOIN inclusion on incl_activity.incid = inclusion.incid JOIN teamleader on activities.tlid2 = teamleader.tlid $whereclause"; //echo $query; $test= mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($test)){ echo "<b>Title: </b>"; echo $row['actitle']; echo "<br />"; echo "<b>Team Leader: </b>"; echo $row['tlname']; echo "<br />"; echo "<b>Start Date: </b>"; echo $row['acdate']; echo "<br />"; echo "<b>Duration: </b>"; echo $row['acdur']; echo "<br />"; echo "<b>Cost: </b>"; echo $row['accost']; echo "<br />"; echo "<b>Incl. Activities: </b>"; echo $row['incdesc']; echo "<br />"; echo "<b>Transportation: </b>"; echo $row['actransp']; echo "<br />"; echo "------------"; echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419955 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 Easiest way to get results side-by-side is to to put output inside <div>s with style="float:left" Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/#findComment-1419959 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.