Page 1 of 1

[SOLVED] Problem concerning searching - NEED HELP

Posted: Sat Jan 24, 2004 2:46 pm
by Calimero
When you enter words in a search engine (ex. google) each one of them is checked separately, and all of them toogether.

What I want to know is>
1) If someone writes 3 words (for ex. Computer Hardware Periphetals) in a textfield and submits the form - what is the code in php - so those 3 words can be checked against the database separately - each as different variable

2) If I have two text fields like
enter keywords:___________________
enter location:_____________________
in a form, whats the code in PHP to process them both, but as separate values (or variables-whatever the name is), and to implement the above problem solution, if possible :?

For those who decide to work on this problem, Thanks Ahead
:)

Posted: Sat Jan 24, 2004 8:51 pm
by microthick
Assume there's a search text box called "criteria" on the previous page, and the user entered "computer peripherals" in the search criteria box.

Then, when processing the code, you could start with this:

Code: Select all

<?php

if (!empty($_POST["criteria"]) {
     $criteria = $_POST["criteria"];
     // Create an array storing all the separate words, using space as a delimiter.
     $searchwords = explode(" ", $criteria);
     
     // Now, let's create the SQL statement.
     // 0 = 0 is a base statement that will always be true.
     $sql = "select * from mysearchtable where 0 = 0 ";
     foreach ($searchwords as $word) {
          // For each word, include any records that have that word in the field anywhere.
          $sql .= "and somefield like '%".$word."%' ";
     }
     
     // Now, let's run the query.
     $result = mysql_query($sql, $conn);
}

// Results are now stored in $result to use as you please.

?>

Thanks For The Reply :)))), But I need a little bit more hlp

Posted: Sun Jan 25, 2004 2:55 am
by Calimero
Thanks microthick,
I assume that this code can be just duplicated for each textfield (of this type)

PROBLEM
3) If I declare variables in 1.php - that's the script to which I submit the form, and start another 2.php script trough Header - command are the variables entered in the 1.php still functional in the 2.php or do I need to declare them again, and if possible the code for that (3 Variables, no more)

NOTE I worked in mysql much longer and i'm using the expressions from there, just if some words are not PHP-like