Jump to content

search function + Obscure Database setup = (RE-OPENED issue)


bossman

Recommended Posts

ok, so i got my search feature functioning using this tutorial...

 

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

 

Here is MY version of it...

 

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

 

and here is my code i used/changed...

 

<?php

$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, 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>

 

 

If you run a search of something like "Adobe Days" or "Bank of America", you'll see that i have it working great. But here's the catch...

 

I would also like them to be able to search what products the project fall under, which i have set up in my database. Only thing is, the way i set up the database is well....complicated?  I've attached an image...

 

As you can see in my database, when the user 'adds' a project with the admin i built, it writes a 1 under the column for the designed product. So my question is...how can i have them use this search function to search 'product', if my products values are represented in the database using 1's, instead of names? Is there a way i can transfer the 1 to a different value for the search feature, perhaps using the name of the column?  if you dont understand please reply i can send you the link of the actual project.

 

ANY HELP would be appreciated greatly!!

 

 

 

 

 

[attachment deleted by admin]

ok each project.. has the rows project_id, title, link, date, industry, ....and then all the fields after that are individual products that the project belongs to, if the product belongs to the project, it gets a 1 in the field....so when im calling them in on the site, i use this query...

 

<a name="acrobat9professional"></a>
<div class="left_titles"><strong>Acrobat 9 Professional</strong></div>
<?
$result1 = mysql_query("SELECT * from `projects` where `acro_9_prof`='1' ORDER BY title asc");

while ($row = mysql_fetch_array($result1)){
//store the retrieved info as variables...
$id = $row['project_id'];
$title = $row['title'];
$link = $row['link'];

echo "<a href='$link' target='_blank' style='padding-bottom:5px;'>$title</a><br/>";
}

//close server connection...
//mysql_close($link);
?>

 

 

there are 14 other queries on the page like this, with the product changing for each one.  Heres a link to the site....

 

http://www.adoberegistrations.com/ResourceCenters/OYilmaz/viewby_product.php

 

if you use the navigation on the right, you can view the projects by date, industry, product, or view all. Does it make more sense now?

unfortunately at this point i cant afford to rework the database, after the project is approved n everything i can work on that, but for now i have to try to get the search function working the way it is, any other ideas? theres got to be some way to make transfer a variable from the 1 to the name of the product based off of the name of the column?

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.