Jump to content

[SOLVED] Search Function + Obscure Database setup = ??


bossman

Recommended Posts

A few questions on this post...first some information so you understand what's going on.

 

I have successfully gotten my search function working, using this tutorial...

 

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

 

My version can be seen here....

 

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

 

As you can see, you have some check boxes underneath the text box..

 

What i would like to do is change this script, so that they can search through ALL fields, by just typing in ANYTHING into the 1 SINGLE TEXTBOX, rather than typing something in, and then having to use checkboxes to narrow it down. Just want the user to be able to search something from any field and get the same formatted results despite what field the information there searching falls under....does this make sense?

 

here is my code...

 

<?php
//i have the right db info, just not willing to post it on here...
$h="host";
$u="username";
$p="password";
$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, 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 `date`"; // 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:12px; color:#000000;'>{$i}: <br/>{$row['date']}<br />{$row['title']}<br />{$row['industry']}</div><br /><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>
<html>
   <title>Adobe Days</title>
   <style type="text/css">
      #error {
         color: red;
      }
   </style>
   <body>
      <?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)?$searchTerms:''; ?>" /><br />
         Search In:<br /><br/>
         Title: <input type="checkbox" name="title" value="on" <?php echo isset($_GET['title'])?"checked":''; ?> /><br/>
         Date: <input type="checkbox" name="date" value="on" <?php echo isset($_GET['date'])?"checked":''; ?> /> <br/> 
         Industry: <input type="checkbox" name="industry" value="on" <?php echo isset($_GET['industry'])?"checked":''; ?> /><br />
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?><br /><br />
         <input type="submit" name="submit" value="Search!" />
      </form>
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>

   </body>
</html>

 

Does anybody understand what I'm trying to do, if so, can anybody give me a push in the right direction?

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.