bickyz Posted March 21, 2013 Author Share Posted March 21, 2013 I mean results to be like this i.e each records displays side by side 1 2 34 5 67 8 9 Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420077 Share on other sites More sharing options...
Barand Posted March 21, 2013 Share Posted March 21, 2013 Yes, I know  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/page/2/#findComment-1420133 Share on other sites More sharing options...
bickyz Posted March 21, 2013 Author Share Posted March 21, 2013 Hi barand, thank you very much. I have managed to get results side by side by using follwing changes, could you please check if I my code looks good. <?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";*/ $query = "SELECT activities.*, teamleader.tlname, GROUP_CONCAT(incdesc SEPARATOR ', ') as Includes 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 GROUP BY activities.acid ORDER BY teamleader.tlname ASC"; //echo $query; $test= mysql_query($query) or die(mysql_error()); if(mysql_num_rows($test)) { $i=1; echo '<table><tr>'; while($row = mysql_fetch_assoc($test)){ $actitle=$row["actitle"]; $tlname=$row["tlname"]; $acdate=$row["acdate"]; $acdur=$row["acdur"]; $accost=$row["accost"]; $incdesc=$row["Includes"]; $actransp=$row["actransp"]; echo '<td>'."$actitle <br> $tlname <br> $acdate <br> $acdur <br> $accost <br> $incdesc <br> $actransp <br><br>".'</td>'; if($i % 4 == 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420134 Share on other sites More sharing options...
Barand Posted March 21, 2013 Share Posted March 21, 2013 try  if(mysql_num_rows($test)) { $i=0; while($row = mysql_fetch_assoc($test)){ $actitle=$row["actitle"]; $tlname=$row["tlname"]; $acdate=$row["acdate"]; $acdur=$row["acdur"]; $accost=$row["accost"]; $incdesc=$row["Includes"]; $actransp=$row["actransp"]; echo "<div style='width:250px; padding:10px; float:left;'> $actitle <br> $tlname <br> $acdate <br> $acdur <br> $accost <br> $incdesc <br> $actransp </div>"; $i++; if($i % 3 == 0) echo "<div style='clear:both'></div>\n"; } if ($i%3) echo "<div style='clear:both'></div>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420148 Share on other sites More sharing options...
bickyz Posted March 21, 2013 Author Share Posted March 21, 2013 Hi Barand, thank you very much for your help. Can you please give me an idea on how the code will be if i want to implement a drop down list to sort the results. I have created a form on the top of this results page with following code <form name="form1" method="post" action=""> <select name="sort" id="sort"> <option selected="selected" value="0">Sorty by</option> <option value="costlow">Price Ascending</option> <option value="costhigh">Price Descending</option> <option value="durlow">Duration Ascending</option> <option value="durhigh">Duration Descending</option> <option value="nameasc">Teamleader Name ASC</option> <option value="namedesc">Teamleader Name DESC</option> <option value="acdate">Date Ascending</option> <option value="acdate">Date Descending</option> </select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420165 Share on other sites More sharing options...
Barand Posted March 21, 2013 Share Posted March 21, 2013 (edited) Something like this, maybe  switch ($_POST['sort']) { case '0': $orderby = ''; break; case 'costlow': $orderby = " ORDER BY acprice"; break; case 'costhigh': $orderby = " ORDER BY acprice DESC"; break; // etc } $query .= $orderby; // add order by clause to the query Note: your date sort options both have same value! Edited March 21, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420167 Share on other sites More sharing options...
bickyz Posted March 21, 2013 Author Share Posted March 21, 2013 Hi barand, thank you very much for your help. When I search, it does give me a results but it also throws me following error: Â Notice: Undefined index: sortby in /htdocs/listactivities.php on line 28Notice: Undefined variable: orderby in /htdocs/listactivities.php on line 78 Â Line 28 is switch ($_POST['sortby']) { Line 78 is $orderby"; Â While on the results page if I select anything from the sort by drop down list, the results page displays everything clearing out the search results, sort by does works though. Â demo http://bickyz.byethost13.com/ <form name="form1" method="post" action=""> <select name="sortby" id="sortby" onChange="this.form.submit()"> <option selected="selected" value="0">Sorty by</option> <option value="costasc">Price Ascending</option> <option value="costdesc">Price Descending</option> <option value="durasc">Duration Ascending</option> <option value="durdesc">Duration Descending</option> <option value="nameasc">Teamleader Name ASC</option> <option value="namedesc">Teamleader Name DESC</option> <option value="dateasc">Date Ascending</option> <option value="datedesc">Date Descending</option> </select> </form> <?php switch ($_POST['sortby']) { case '0': $orderby = ''; break; case 'costasc': $orderby = " ORDER BY accost ASC"; break; case 'costdesc': $orderby = " ORDER BY accost DESC"; break; case 'durasc': $orderby = " ORDER BY acdur ASC"; break; case 'durdesc': $orderby = " ORDER BY acdur DESC"; break; case 'nameasc': $orderby = " ORDER BY tlname ASC"; break; case 'namedesc': $orderby = " ORDER BY tlname DESC"; break; case 'dateasc': $orderby = " ORDER BY acdate ASC"; break; case 'datedesc': $orderby = " ORDER BY acdate DESC"; break; // etc } //$query .= $orderby; // add order by clause to the query ?> <p></p><p></p> <?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); $query = "SELECT activities.*, teamleader.tlname, GROUP_CONCAT(incdesc SEPARATOR ', ') as Includes 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 GROUP BY activities.acid $orderby"; //ORDER BY teamleader.tlname ASC"; //echo $query; $test= mysql_query($query) or die(mysql_error()); if(mysql_num_rows($test)) { $i=0; while($row = mysql_fetch_assoc($test)){ $actitle=$row["actitle"]; $tlname=$row["tlname"]; $acdate=$row["acdate"]; $acdur=$row["acdur"]; $accost=$row["accost"]; $incdesc=$row["Includes"]; $actransp=$row["actransp"]; echo "<div style='width:250px; padding:10px; float:left;'> $actitle <br> $tlname <br> $acdate <br> $acdur <br> $accost <br> $incdesc <br> $actransp </div>"; $i++; if($i % 3 == 0) echo "<div style='clear:both'></div>\n"; } if ($i%3) echo "<div style='clear:both'></div>\n"; } 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>'; header('Refresh: 5; URL=index.php'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420188 Share on other sites More sharing options...
Barand Posted March 21, 2013 Share Posted March 21, 2013 try  $orderby = ''; if (isset($_POST['sortby'])) { switch ($_POST['sortby']) { case '0': $orderby = ''; break; case 'costasc': $orderby = " ORDER BY accost ASC"; break; case 'costdesc': $orderby = " ORDER BY accost DESC"; break; case 'durasc': $orderby = " ORDER BY acdur ASC"; break; case 'durdesc': $orderby = " ORDER BY acdur DESC"; break; case 'nameasc': $orderby = " ORDER BY tlname ASC"; break; case 'namedesc': $orderby = " ORDER BY tlname DESC"; break; case 'dateasc': $orderby = " ORDER BY acdate ASC"; break; case 'datedesc': $orderby = " ORDER BY acdate DESC"; break; // etc } } Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420202 Share on other sites More sharing options...
bickyz Posted March 21, 2013 Author Share Posted March 21, 2013 hi barand, thank you. Both errors are gone, while on the search results page if I select anything from the sortby drop down list, the results page displays everything. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420212 Share on other sites More sharing options...
Barand Posted March 21, 2013 Share Posted March 21, 2013 So long as it displays them in the selected order. If you don't make any other selections such as duration, date etc to filter the results then you will get all records. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420213 Share on other sites More sharing options...
bickyz Posted March 21, 2013 Author Share Posted March 21, 2013 Hi Barand, sorry i didnt get u. I have total 16 records in the database. For example if i select Duration 8 days and do a search, it will give me 4 records which are not sorted in any order. Now while on the results page with the 4 records if i select Sort by "Price Ascending" then the page clears the 4 records and displays all the 16 records sorted by Price Asc. Â So basically this dropdown is clearing the search results. What i want is, while on the results page with 4 records I should be able to flick between the sort by dropdown list for these 4 records in either Price ASC/DESC or Duration ASC/DESC or Date ASC/DESC or Teamleader Name ASC/DESC. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420221 Share on other sites More sharing options...
Barand Posted March 21, 2013 Share Posted March 21, 2013 Then you need to save the previous selection and, when the page reloads, reset the choices to those saved options. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420224 Share on other sites More sharing options...
bickyz Posted March 22, 2013 Author Share Posted March 22, 2013 hi barand, when you say "save the previous selection", how do i save; do i use session or save it to database. thank you Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420274 Share on other sites More sharing options...
Barand Posted March 22, 2013 Share Posted March 22, 2013 Use session. Â When you create the selection options, set the selected one to the one saved in the session. Â It's easier to put the options and values in an array and loop through them to create the options and test which should be selected. In the case of included activities loop through the table data. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420285 Share on other sites More sharing options...
bickyz Posted March 22, 2013 Author Share Posted March 22, 2013 hi barand, thank you.On the results page i have added following code: <?php session_start(); foreach(array('duration', 'datepicker', 'transport', 'activity') as $name) { $_SESSION[$name] = (isset($_POST[$name])) ? $_POST[$name] : ""; } echo "<pre>"; print_r($_SESSION); echo "</pre>\n"; ?> when i do the search it shows the saved session contents. Am I on a right track ?i have been trying to get my head over how to do this session part in my code; but I have no idea, I will be really grateful if you can help me please. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420334 Share on other sites More sharing options...
Barand Posted March 22, 2013 Share Posted March 22, 2013 A couple of samples  <?php $duration_options = ''; for ($d=7; $d<=14; $d++) { $sel = $d==$_SESSION['duration'] ? "selected='selected" : ''; $duration_options .= "<option $sel value='$d'> $d</option>"; } $transport_options = ''; $trans = array('included', 'excluded'); foreach ($trans as $o) { $sel = $o == $_SESSION['transport'] ? "selected='selected" : ''; $transport_options .= "<option $sel value='$o'> $o</option>"; } ?> <select name="duration" id="duration"> <option value="">Any</option> <?php echo $duration_options ?> </select> <select name="transport" id="transport"> <option selected="selected" value="0">Any</option> <?php echo $transport_options ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420340 Share on other sites More sharing options...
bickyz Posted March 22, 2013 Author Share Posted March 22, 2013 hi barand, thank you for your help, much appreciated. it throws Undefined variable error on red marked lines. do i use FOREACH to those check boxes. <?php ini_set('display_errors',1); error_reporting(E_ALL); ?> <?php $duration_options = ''; for ($d=7; $d<=14; $d++) { $sel = $d == $_SESSION['duration'] ? "selected='selected" : ''; $duration_options .= "<option $sel value='$d'> $d</option>"; } $transport_options = ''; $trans = array('included', 'excluded'); foreach ($trans as $o) { $sel = $o == $_SESSION['transport'] ? "selected='selected" : ''; $transport_options .= "<option $sel value='$o'> $o</option>"; } $_SESSION["datepicker"] = $_POST["datepicker"]; $_SESSION['activity'] = $_POST['activity']; ?> <!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 value="">Any</option> <?php echo $duration_options ?> </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> <?php echo $transport_options ?> </select></td> </tr> <tr> <td>Â </td> <td>Â </td> </tr> <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="2" /></td> </tr> <tr> <td>Flying</td> <td><input name="activity[]" type="checkbox" id="chkflying" value="3" /></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> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420366 Share on other sites More sharing options...
Barand Posted March 22, 2013 Share Posted March 22, 2013 (edited) You have checkbox descriptions and values (incid) in your db. Query the table to create checkboxes. (This must be about the third time I've said this) Â edit. Â You are using sessions so you need session_start() at the top of the script. Â You are using $_SESSION values before you have set them Edited March 22, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420368 Share on other sites More sharing options...
bickyz Posted March 23, 2013 Author Share Posted March 23, 2013 (edited) Hi barand, thank you for your help. I have now created a checkbox from db query My session code for textbox (datepicker) and checkboxex (activities) seems to be not right, could you please have a look. Â index.php <?php ini_set('display_errors',1); error_reporting(E_ALL); ?> <?php include 'db.inc.php'; ?> <?php session_start(); $duration_options = ''; for ($d=7; $d<=14; $d++) { $sel = $d == $_SESSION['duration'] ? "selected='selected" : ''; $duration_options .= "<option $sel value='$d'> $d</option>"; } $transport_options = ''; $trans = array('included', 'excluded'); foreach ($trans as $o) { $sel = $o == $_SESSION['transport'] ? "selected='selected" : ''; $transport_options .= "<option $sel value='$o'> $o</option>"; } $activity_options = ''; for ($d=1; $d<=3; $d++) { $sel = $d == $_SESSION['activity'] ? "checked='checked" : ''; $activity_options .= "<input $sel value='$d'> $d</option>"; } $datepicker_options['datepicker'] = 'datepicker'; $_SESSION['datepicker'] = $datepicker_options['datepicker']; ?> <!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 value="">Any</option> <?php echo $duration_options ?> </select></td> </tr> <tr> <td>Â </td> <td>Â </td> </tr> <tr> <td width="45%">Date (YYYY-MM-DD)</td> <td width="55%"> <input name="datepicker" type="text" id="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> <?php echo $transport_options ?> </select></td> </tr> <tr> <td>Â </td> <td>Â </td> </tr> <tr> <td colspan="2"> <?php $query="SELECT * FROM inclusion ORDER BY incid"; $test= mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($test)){ $activityname = $row["incdesc"]; $activityid = $row["incid"]; "<br/>"; "<br/>"; echo "<input type=\"checkbox\" name=\"activity[]\" value=\"$activityid\"> $activityname"; echo "<br>"; } ?> </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> <?php mysql_free_result($test); ?> listactivities.php <?php ini_set('display_errors',1); error_reporting(E_ALL); ?> <?php include 'db.inc.php'; ?> <?php session_start(); foreach(array('duration', 'datepicker', 'transport', 'activity') as $name) { $_SESSION[$name] = (isset($_POST[$name])) ? $_POST[$name] : ""; } echo "<pre>"; print_r($_SESSION); echo "</pre>\n"; ?> <form name="form1" method="post" action=""> <select name="sortby" id="sortby" onChange="this.form.submit()"> <option selected="selected" value="0">Sorty by</option> <option value="costasc">Price Ascending</option> <option value="costdesc">Price Descending</option> <option value="durasc">Duration Ascending</option> <option value="durdesc">Duration Descending</option> <option value="nameasc">Teamleader Name ASC</option> <option value="namedesc">Teamleader Name DESC</option> <option value="dateasc">Date Ascending</option> <option value="datedesc">Date Descending</option> </select> </form> <?php $orderby = ''; if (isset($_POST['sortby'])) { switch ($_POST['sortby']) { case '0': $orderby = ''; break; case 'costasc': $orderby = " ORDER BY accost ASC"; break; case 'costdesc': $orderby = " ORDER BY accost DESC"; break; case 'durasc': $orderby = " ORDER BY acdur ASC"; break; case 'durdesc': $orderby = " ORDER BY acdur DESC"; break; case 'nameasc': $orderby = " ORDER BY tlname ASC"; break; case 'namedesc': $orderby = " ORDER BY tlname DESC"; break; case 'dateasc': $orderby = " ORDER BY acdate ASC"; break; case 'datedesc': $orderby = " ORDER BY acdate DESC"; break; // etc } } ?> <p></p><p></p> <?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); $query = "SELECT activities.*, teamleader.tlname, GROUP_CONCAT(incdesc SEPARATOR ', ') as Includes 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 GROUP BY activities.acid $orderby"; //ORDER BY teamleader.tlname ASC"; //echo $query; $test= mysql_query($query) or die(mysql_error()); if(mysql_num_rows($test)) { $i=0; while($row = mysql_fetch_assoc($test)){ $actitle=$row["actitle"]; $tlname=$row["tlname"]; $acdate=$row["acdate"]; $acdur=$row["acdur"]; $accost=$row["accost"]; $incdesc=$row["Includes"]; $actransp=$row["actransp"]; echo "<div style='width:250px; padding:10px; float:left;'> $actitle <br> $tlname <br> $acdate <br> $acdur <br> $accost <br> $incdesc <br> $actransp </div>"; $i++; if($i % 3 == 0) echo "<div style='clear:both'></div>\n"; } if ($i%3) echo "<div style='clear:both'></div>\n"; } 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>'; header('Refresh: 5; URL=index.php'); } ?> <?php mysql_free_result($test); ?> Edited March 23, 2013 by bickyz Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420501 Share on other sites More sharing options...
mac_gyver Posted March 23, 2013 Share Posted March 23, 2013 or you can pass the selected search filters in the url so that someone can create a bookmark or shortcut to the page and return to the same point later or share the search result with someone else via a link. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420505 Share on other sites More sharing options...
Barand Posted March 23, 2013 Share Posted March 23, 2013 what is this supposed to do? Â $activity_options = ''; for ($d=1; $d<=3; $d++) { $sel = $d == $_SESSION['activity'] ? "checked='checked" : ''; $activity_options .= "<input $sel value='$d'> $d</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420590 Share on other sites More sharing options...
bickyz Posted March 23, 2013 Author Share Posted March 23, 2013 hi barand, i didn know how to put the checkboxes values in the session so i end up trying to copy the codes from the dropdown list. Could u plz help with the code, i will be really grateful,thank you. Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420606 Share on other sites More sharing options...
Barand Posted March 24, 2013 Share Posted March 24, 2013 example  <form action="" method="post"> <?php $_SESSION['activity'] = (isset($_POST['activity'])) ? $_POST['activity'] : array(); $query="SELECT * FROM inclusion ORDER BY incid"; $test= mysql_query($query) or die(mysql_error()); while(list($id, $desc) = mysql_fetch_row($test)) { $chk = in_array($id, $_SESSION['activity']) ? "checked='checked'" : ''; echo "<input type=\"checkbox\" name=\"activity[]\" value=\"$id\" $chk /> $desc <br>"; } ?> <input type="submit" name="btnSubmit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420629 Share on other sites More sharing options...
bickyz Posted March 24, 2013 Author Share Posted March 24, 2013 hi barand, thank you for the help. it throws following error now, Notice: Undefined index: duration in index.php on line 40Notice: Undefined index: transport in index.php on line 74 <?php ini_set('display_errors',1); error_reporting(E_ALL); ?> <?php include 'db.inc.php'; ?> <?php session_start(); ?> <!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="600" border="0" cellspacing="0" cellpadding="0"> <tr> <td>Duration (days)</td> <td> <?php $duration_options = ''; for ($d=7; $d<=14; $d++) { $sel = $d == $_SESSION['duration'] ? "selected='selected" : ''; $duration_options .= "<option $sel value='$d'> $d</option>"; } ?> <select name="duration" id="duration"> <option value="">Any</option> <?php echo $duration_options ?> </select></td> </tr> <tr> <td>Â </td> <td>Â </td> </tr> <tr> <td width="45%">Date (YYYY-MM-DD)</td> <td width="55%"> <?php $datepicker_options['datepicker'] = 'datepicker'; $_SESSION['datepicker'] = $datepicker_options['datepicker']; ?> <input name="datepicker" type="text" id="datepicker" /> </td> </tr> <tr> <td>Â </td> <td>Â </td> </tr> <tr> <td>Transportation</td> <td> <?php $transport_options = ''; $trans = array('included', 'excluded'); foreach ($trans as $o) { $sel = $o == $_SESSION['transport'] ? "selected='selected" : ''; $transport_options .= "<option $sel value='$o'> $o</option>"; } ?> <select name="transport" id="transport"> <option selected="selected" value="0">Any</option> <?php echo $transport_options ?> </select> </td> </tr> <tr> <td>Â </td> <td>Â </td> </tr> <tr> <td colspan="2"> <?php $_SESSION['activity'] = (isset($_POST['activity'])) ? $_POST['activity'] : array(); $query="SELECT * FROM inclusion ORDER BY incid"; $test= mysql_query($query) or die(mysql_error()); while(list($incid, $incdesc) = mysql_fetch_row($test)) { $chk = in_array($incid, $_SESSION['activity']) ? "checked='checked'" : ''; echo "<input type=\"checkbox\" name=\"activity[]\" value=\"$incid\" $chk /> $incdesc <br>"; } ?> </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> <?php mysql_free_result($test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420704 Share on other sites More sharing options...
Barand Posted March 24, 2013 Share Posted March 24, 2013 You are using $_SESSION values before you have set them Quote Link to comment https://forums.phpfreaks.com/topic/275829-search-multiple-fields-using-multiple-form-elements/page/2/#findComment-1420715 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.