PDA

View Full Version : search box



Ferellie
03-03-2006, 12:35 PM
Hello and thank you for this forum.

Part of my code for searching my mysql table is:

// read data from database
$result = mysql_query("select * from $table WHERE categories LIKE '%club%' order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

What I want to do is get the value of an input box into the string search in place of %club%, so instead of searching for club it searches for what the user put into the input box.

My html code is this:

<form id="frmSearch" name="frmSearch" method="post" action="">
<label>
<input name="inputSearch" type="text" id="inputSearch" size="20" maxlength="12" />
</label>
<label>
<input type="submit" name="Submit" value="Search" />
</label>
</form>

Everything works fine but I'd be over the moon if you could help me to use the users input and search for that!

Many thanks,

Peter

djr33
03-04-2006, 12:45 AM
The first thing I notice is that your form is a bit more complex than the ones I've used. (I'm just getting into this too.)
you have id and name fields for each... I don't think you need both... just name. Maybe it varies for older browsers.

Anyway... just name the input text whatever you want... seems like you've got it as 'inputSearch'.

(I'd advise not using caps... just makes it more complex)

In your php code, BEFORE the mysql command, maybe just at the top to make things easier, put:
...
$term = $_POST['inputSearch'];
...

then,
...
$result = mysql_query("select * from $table WHERE categories LIKE '%$term%' order by id desc limit $rows", $link)
...

If that doesn't work, might be because the syntax of the variable in the line of text doesn't work, so you could use this (i'm not positive I coded it correctly... I think it's right, though)
...
$result = mysql_query("select * from $table WHERE categories LIKE '%".$term."%' order by id desc limit $rows", $link)
...


Anyway, that should do just fine.

I think that older versions of php use '$HTTP_POST_VARS' or something of the like instead of '$_POST', so you might need to use that if you're on php version 3. I'm guessing it won't be an issue.

Also, some advice:
I coded a search form... might be worth letting your users see what they put in the input field by making the action 'get', not 'post'.

That would make the url: 'http://..url../page.php?search=TERM' (If you did this, I could rename inputSearch to just search.)

Hope this helps.

Ferellie
03-04-2006, 03:55 PM
Thank you, that's great.
In the end I used this:

foreach($_POST as $key => $val) { // This loops through the form variables
$$key = $val; // Assigns variables with the same names as the input names
} // i.e. the text box inputSearch would have a variable name of $inputSearch

// read data from database
$result = mysql_query("select * from $table WHERE keywords LIKE '%$inputSearch%' order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());


Problem is, the query results are not quite what I had in mind! If the field contained the words 'one two three' and the user searched for 'three two' the query would return nothing. Is there a way of modifying the search to accomplish this? I want my search to find any matching words.

Thanks again,

Peter

djr33
03-05-2006, 12:03 AM
I wrote a search engine recently.... and you have to think not about coding it, but about logic.
HOW does it get compared?
Think about google. You know they're not just doing it word for word. They find words, then compare for relevance, narrow down the lists, then compare for context and words in more important contexts would be more relevant for searching... it also ignores words like the, of, etc.

Not looking at your code, exactly, I'm not too sure how it works, but I can explain what I did, and that it works:
--For every result, I made a name, description, link, set of keywords, catergory, etc.
--If the input matched the keywords, the name, description and link would appear. (The name was also used as a variable name...

$keywords = $name$keywords
$$keywords = "keyword other stuff blah blah blah"
so that you could have relatively named keywords, descriptions, etc.)
--NOTE: I DIDN'T use a database, just a php file with the above code in it. I plan to make a database now, to make it easier to modify. The same logic should apply, though.

Ok... so now that you get how this works... I can explain how I did keywords:
As you saw above, my keywords were seperated by spaces. That's it.
When checking, it got the keywords as a variable, then went through character by character, until it hit a space. It saved the letters before the space in another variable, $keyword.
Then it checked if any of the $term variables (seperated EXACTLY the same way) matched it.
Then it did so for the next keyword, finding the next space, etc.

I also added things like ignoring words like the, of, etc. (in the input... the keywords wouldn't have these... I wouldn't input them.)
and sorting results by relevance. I added another variable named variable, $$matches (see above for how) and added 1 to that each time an input term matched a keyword. At the end, it would compare the number of words (remember to store this as a variable at the beginning) to the number of matches for each possible result:
0 would mean no result... it wouldn't do anything.
if $numofwords = $$matches, then it would be a complete result. If not,
it would be a partial result, and displayed under that heading. Also, it goes through and ranks them for how MUCH less they are than full... giving your results that all but 1 of your terms match, then all but 2, etc. so that the more relevant ones are on top.

Now, if you want to see this in action, visit:
http://thebrb.com/stockpile/search.php
Try searching for stuff related to filmmaking, as that's what the site is about. You'll see how it works.

If you want, talk to me on AIM.
I'm d33jr.
I'm working on this too, so maybe we could help each other out a bit.