[SOLVED] Problem concerning searching - NEED HELP

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

[SOLVED] Problem concerning searching - NEED HELP

Post 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
:)
Last edited by Calimero on Sun Jan 25, 2004 8:53 am, edited 1 time in total.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.

?>
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

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

Post 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
Post Reply