PDA

View Full Version : Include a Search facility on my website.


tux
08-28-2009, 11:06 AM
Hello All,

I am building a website to display my photographs. As they will eventually run into several hundred or more I want to include a search facility.

The images are all displayed dynamically from the MYSQL DB. What would be the best way to go about adding this functionality. I was thinking of maybe adding a field to the DB to include keywords that a query can search through but am open to suggestions and best methods.

I have a reasonable knowledge of PHP.

Thanks all.

Regards, Paul

edbr
08-28-2009, 11:27 AM
keyword tags sounds like a good idea

tux
08-28-2009, 03:51 PM
Thanks edbr,

So if I had a field in my DB and filled it with keywords for each image, I could then use a query based on what the user entered into the search box. ie...

$search = $_POST['search'];

$query = sprintf("SELECT * FROM images WHERE search = $search");
$result = @mysql_query($query);
$images = @mysql_fetch_array($result);

but, how would the syntax be to search the DB field for a single word or phrase from an entry that contained lots of words.

Regards Paul

Corrosive
08-28-2009, 06:46 PM
Found this a few weeks back. Must admit I've not attempted it yet...

http://www.roscripts.com/PHP_search_engine-119.html

tux
08-29-2009, 02:23 PM
Thanks for that Corrosive.

What I have done so far is to have this code.....

$search = 'tree';

$query = sprintf("SELECT * FROM images WHERE search LIKE $search ORDER BY imageID DESC");
$result = @mysql_query($query);
$images = @mysql_fetch_array($result);

This kinda works but only picks the rows that have exactly "tree" in the DB keyword field. If the DB keyword field has for example... "tree leaf bark".... then it wont select that row. How can I get my query to select all the fields that contain "tree" somewhere in the string.

Any ideas would be great.

Regards, Paul

bee80
08-29-2009, 02:55 PM
add percentage signs


$search = 'tree';

$query = sprintf("SELECT * FROM images WHERE search LIKE %$search% ORDER BY imageID DESC");
$result = @mysql_query($query);
$images = @mysql_fetch_array($result);


i think

tux
08-29-2009, 05:30 PM
Thanks Bee80,

I'm slowly getting this to work but have come accross an error that I cant suss. I have this query (lines 39, 40 & 41)......

$query5 = sprintf("SELECT * FROM images WHERE imageCat = '$galleryID' AND upper(search) LIKE '%$search%' OR upper(title) LIKE '%$search%' OR upper(description) LIKE '%$search%' ORDER BY imageID DESC LIMIT $offset, $rowsPerPage ");
$result5 = mysql_query($query5);
$images1 = mysql_fetch_array($result5);

..... which keeps throwing these errors....

Warning: sprintf() [function.sprintf]: Too few arguments in D:\wamp\www\gallery_search.php on line 39

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\wamp\www\gallery_search.php on line 41

Both $galleryID & $search have values. Can anyone see anything wrong with my code.

Thanks.

bee80
08-29-2009, 05:56 PM
not sure about the first one but use an @ symbol to supress the error on line 41,
the query should work without sprintf so :



$query5 = "SELECT * FROM images WHERE imageCat = '$galleryID' AND upper(search) LIKE '%$search%' OR upper(title) LIKE '%$search%' OR upper(description) LIKE '%$search%' ORDER BY imageID DESC LIMIT $offset, $rowsPerPage ";
$result5 = @mysql_query($query5);
$images1 = @mysql_fetch_array($result5);



actually what does the sprintf function do?? i know it formats the string in someway but how?