Jump to content

[SOLVED] Need help with simple search function, so close i can taste it.....DESPERATE!!


bossman

Recommended Posts

i built a project management site with a search function, using this tutorial...

 

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

 

my version is here (search bar is upper right)

 

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

 

I'm having some trouble getting it to search all fields. as you can see, the tutorial has it set up so that you have to narrow your search down by checkboxes, but that function is unneccessary for the site we are working on. All we need is a search box that searches anything from all fields and still get results, WITHOUT using checkboxes.

 

Here is the php codes...

 

<?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, Geneva, sans-serif; font-size:10px; color:#000000;'>{$i}:<a href='{$row['link']}' target='_blank'>{$row['title']}</a></div><br />";
            $i++;
         }
      }
   }
}

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

?>

 

and here is my form...

 

<form method="GET" action="search.php" name="searchForm">
<table width="240">
<tr>
<td style="padding-left:15px; width:150px;">
<input type="text" size="19" name="search" value="<?php echo isset($searchTerms)?$searchTerms:''; ?>" /></input>
</td>
<td>
<input name="submit" type="image" id="Submit" src="images/search.jpg" alt="Search" style="padding-left:0px;">
</td>
</tr>
</table>
</form>

 

and here is how the results get displayed....

 

<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>

<?php echo (count($results) > 0)?"<div style='color:#FFFFFF; padding-left:20px; font-family:Verdana, Geneva, sans-serif; font-size:12px;'><strong>Adobe Day Search Results for '{$searchTerms}'</strong></div><br />" . implode("", $results):""; ?>

 

Right now....all it does is search through the 'title' column, but if i type in 'banking' or 'insurance' i get nothing because those fall under the 'industry' column, which i am clearly calling in with my code.

 

Can anybody offer me any help gettnig this to function...i am like 90% there and need help with the final push to get this baby working, any search function experts out there that can offer me some assistance? it would be greatly appreciated...  i have posted this issue yesterday but didnt get much help and im still stuck, ive done as much as i can do on my own and now im lost....

Link to comment
Share on other sites

Why didn't you just reply to your original thread? The fix is as simple as it gets. These lines only add a condition if the checkbox was set

      // 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}%'":'';

 

Just change them so they are set by default

      // grab the search types.
      $types = array();
      $types[] = "`title` LIKE '%{$searchTermDB}%'";
      $types[] = "`date` LIKE '%{$searchTermDB}%'";
      $types[] = "`industry` LIKE '%{$searchTermDB}%'";

 

Personally, I would change a lot more, but I'm not in a mood to rewrite your code.

Link to comment
Share on other sites

not looking for a rewrite, just a thorough explanation as to what i was doing wrong and what i should fix, no need for ignorance, we're all on the same team, im a novice simply looking for help, no where else to turn

I think you were reading something into my post that wasn't there. I asked why you didn't reply in your original post since this new post is technically against the forum rules

13. Duplication of topics is strictly prohibited. Users will not post duplicate topics, if a topic needs moved please use the "Report to Moderator" button and a staff member will move the topic as soon as possible.

 

And, in fact, that post also violated the rules

17. Users should not "bump" topics that are still on the first page of the forums. If you bump, you must provide additional information. If you resort to bumping, chances are your question needs to be re-thought and re-described (see Eric Raymond's "How To Ask Questions The Smart Way").

 

I'm not trying to be an ass, but the rules are there for a reason. They help you to get the answers you need and make it efficient for those providing support.

 

As for the "rewrite" comment, it was simply a factual statement. The code you have now was not meant for how you are using it.  A tutorial is not meant for you to simply copy-paste the code, it is meant for you to learn a concept and then implement the concept, not necessarily the exact code, into your project.

 

As I said, I would not use the code as you have it. There is logic in there that is no longer relevant. I would encourage you to make this a learning experience and to read through the code line-by-line to understand what is happening on each line and to determine what changes, if any, are warranted.

 

If I was trying to be mean I would point out that your usage of the word "ignorance" is incorrect. Perhaps you meant "arrogance". Oh wait, I guess I did point that out. Seriously, though, no ill will intended.

Link to comment
Share on other sites

im new to this website and i hadn't thoroughly read the rules, my apologies for that, i will make sure that doesnt happen again in the future. Im just used to other forums giving me really vague answers and getting nowhere. Dispite this little situation, your recommendation worked and i cant thank you enough for that, i am very pleased. As for the ignorance comment, i guess its hard to tell what tone it is in when its in plane text and not face to face. I appreciate your help, and in the future i will make sure i follow the rules thoroughly. I apologize for the miscommunication, and thanks again  :)

Link to comment
Share on other sites

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.