Jump to content

Search function functionality off a bit, quick fix, need a push


Recommended Posts

ok, i made a search function using this tutorial...

 

http://www.phpfreaks.com/tutorial/simple-sql-search

 

here is my version....

 

http://www.adoberegistrations.com/ResourceCenters/OYilmaz/search_test/search.php

 

and here is my code....

 

<?php

$h="host";
$u="username";
$p="pass";
$d="database";

$link = mysql_connect ($h, $u, $p) or die ("Could not connect to database, try again later");
mysql_select_db($d, $link);


// Set up our error check and result check array
$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 project_id, title, link, date, industry FROM projects WHERE ";
      
      // grab the search types.
      $types = array();
      $types[] = isset($_GET['ptitle'])?"`title` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['pdate'])?"`date` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['pindustry'])?"`industry` LIKE '%{$searchTermDB}%'":'';
      
      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
      
      if (count($types) < 1)
         $types[] = "`title` LIKE '%{$searchTermDB}%'"; // use the body 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 die("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[] = "<div style='font-family:Verdana; font-size:10px; color:#000000;'>{$i}:<a href='{$row['link']}' target='_blank'>{$row['title']}</a></div><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>
<html>
   <title>Adobe Days</title>
   <style type="text/css">
      #error {
         color: red;
      }
   </style>
   <body bgcolor="#000000">
      <?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">
      <input type="text" name="search" value="<?php echo isset($searchTerms)?$searchTerms:''; ?>" />
      <input name="submit" value="Search!"  type="image" src="../images/search.jpg" />
      </form>
      <?php echo (count($results) > 0)?"<div style='color:#FFFFFF'>Adobe Day Search Results for '{$searchTerms}'</div><br />" . implode("", $results):""; ?>

   </body>
</html>

 

 

What im having trouble with, is i deleted the 'filter search' checkboxes from underneath the search box, and now it wont search through any fields except title, but i have it set up to search through date and industry columns as well. So when i type in 2009 and hit search, it returns no results when it should be returning any project that has 2009 in the column. Can anybody help me get this thing working right?!? if you try searching using my search tool posted above, type in "adobe" or "bank of america" or "prudential" to return some results. It SHOULD be able search with the word "banking" or "2009" but its not and this is what i need help with, anyone understand, or have something they can tell me to help me get it working?

 

:shrug:

The answer is right there in the comments of your script

 

      // grab the search types.
      $types = array();
      $types[] = isset($_GET['ptitle'])?"`title` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['pdate'])?"`date` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['pindustry'])?"`industry` LIKE '%{$searchTermDB}%'":'';
      
      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
      
      if (count($types) < 1)
         $types[] = "`title` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

 

It states "use the body as a default search if none are checked". I don't see any way to "check" those three options in your form - therefore those fields are never used in the search criteria. I can make yoru page search by the year or by industry if I manually modify the URL. Either add checkboxes for the user to select those options or just include them in the search criteria by default.

 

Search records with year value of 2009:

http://www.adoberegistrations.com/ResourceCenters/OYilmaz/search_test/search.php?search=2009&pdate=true

 

well what can i do to make it so that using just the ONE single text box will search all three fields? that would be much more convenient than the user having to use checkboxes its pointless that way, we want them to be able to search the criteria for any field with just the one text box, can you assist me in these changes?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.