Jump to content

Search question


An7hony

Recommended Posts

I have this search script. It works great, although i'm trying to put a combo box into it and have got stuck. Can anyone help, its killing me?

<?php



$error = array();
$results = array();

// First check if a form was submitted. 
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.
   
    if (strlen($searchTerms) < 3) {
      $error[] = "Search terms must be longer than 3 characters.";
   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   }
   
   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT id, title, description, location, jobtype FROM jobs WHERE ";
      
      // grab the search types.
      $types = array();
      $types[] = isset($_GET['title'])?"`title` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['description'])?"`description` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['location'])?"`location` LIKE '%{$searchTermDB}%'":'';
      
      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
      
      if (count($types) < 1)
         $types[] = "`description` LIKE '%{$searchTermDB}%'"; // use the description as a default search if none are checked


      
	  $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `title`"; // order by title.

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
      
      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "The search term provided {$searchTerms} yielded no results.";
      }else {
         $results = array(); // the result array
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "{$i}: {$row['title']}<br />{$row['description']}<br />{$row['location']}<br /><br />{$row['jobtype']}<br /><br />";
            $i++;
         }
      }
   }
}



function removeEmpty($var) {
   return (!empty($var)); 
}
?>

 

and here is the html with the combo box named jobtype:

 

<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
      <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
         Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br />
         Search In:<br />
         Title: <input type="checkbox" name="title" value="on" <?php echo isset($_GET['title'])?"checked":'checked'; ?> /> | 
         Desc: <input type="checkbox" name="description" value="on" <?php echo isset($_GET['description'])?"checked":'checked'; ?> /> | 
         location: <input type="checkbox" name="location" value="on" <?php echo isset($_GET['location'])?"checked":'checked'; ?> /> | 
         <br />
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?>><br /><br />


			 <select name="jobtype">
			 <option value="">All</option>
		  	 <option value="Temporary">Temporary</option>
		 	 <option value="Permanent">Permanent</option>
		 	 </select>

         <input type="submit" name="submit" value="Search!" />
      </form>
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>

 

Any help will be a great help

Link to comment
https://forums.phpfreaks.com/topic/202150-search-question/
Share on other sites

$types[] = (isset($_GET['jobtype']) && strlen(trim($_GET['jobtype'])) > 0) ?
    "`jobtype` LIKE '%" . mysql_real_escape_string(trim($_GET['jobtype'])) . "%'" : '';

or

$types[] = (isset($_GET['jobtype']) && strlen(trim($_GET['jobtype'])) > 0) ?
    "`jobtype` = '" . mysql_real_escape_string(trim($_GET['jobtype'])) . "'" : '';

should work

Link to comment
https://forums.phpfreaks.com/topic/202150-search-question/#findComment-1060116
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.