Jump to content

Need urgent help with search in php


work_it_work

Recommended Posts

I need to create search script which search into mysql... i have no idea how to do it... i never seen an example.

what i'm trying to do is something like this: http://motors.ebay.co.uk/

User will select the make then the model and other features for car search

I've used google but didn't find an answer yet.

Please paste some examples or some links where I can learn to do it. ::)

 

Link to comment
https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/
Share on other sites

This works but could be refined - as I look at it today. I wrote it years ago.

 

<?php
//Loops search terms into a SQL SELECT statement that allows you to search for a term or terms
//in multiple fields (Googley). Uses the AND operator between terms. For example, a search for "music elvis"
//returns a SELECT statement where any field contains "music" AND any field contains "elvis". 
function fnSearch($term, $orderBy, $orderType, $limit) { //$term=search field; $orderBy=order by field; $orderType=ASC or DESC; $limit is LIMIT.
$term = trim($term); //trim the spaces fore and aft
$term = strtolower($term);	//set string to lowercase
//set up noise words array
$noise = array(	"the ",  
				" an ", 
				" a ", 
				" of ", 
				" and ", 
				" with ", 
				" on ", 
				" in ", 
				" is ", 
				" but ", 
				" like ", 
				" without "
				);
$term = str_replace($noise, " ", $term); //replace noise words	
$term = str_replace("  ", " ", $term);	//replace any double spaces
$term = preg_replace('/\s+/', ' ', $term); //replace multiple spaces
//KEEP THESE THREE TOGETHER - this allows you to set the operator to "OR"
$termLen1 = strlen($term); //count the string length
$term = str_replace(" or ", " ", $term);	//replace any ORs
$termLen2 = strlen($term); //see if the string length changed (ORs were found)
//KEEP THESE THREE TOGETHER ^
$term = str_replace(" and ", " ", $term); //get rid of ANDs
$arrTerms = split(' ', $term); //split the terms on space
$termCount = count($arrTerms); //count your terms
if($termLen1 != $termLen2 ) { //if ORs were found, then set operator to OR
	$operator = "OR";
} else {
	$operator = "AND";
}
$searchSql = ""; //declare the sql string 

$i=0; //set the counter
//Define your WHERE clause
while ($i < $termCount) { 
	if ($i == ($termCount - 1)) { //minus 1 because the array count starts at zero
		//this is the last search phrase - it excludes the operator
		$searchy = " (ptitle LIKE '%$arrTerms[$i]%' OR pstyle LIKE '%$arrTerms[$i]%'  ) ";
	} else {
		//as long as this is not the last search phrase, it includes the operator - either AND or OR
		$searchy = " (ptitle LIKE '%$arrTerms[$i]%' OR pstyle LIKE '%$arrTerms[$i]%'  ) $operator ";
	}
	$searchSql = "$searchSql".$searchy; //tack on the string to the end of the old string
	$i++; //count the loop
} //end while
//Define your main SQL here
$sSearch = "SELECT pid, ptitle, pstyle FROM iproducts WHERE $searchSql $limit"; 
return $sSearch;
}
?>

no body wants to share...

i'll explain again what i want to do:

 

one search page with form where the visitors can search into the database. the form must have drop down columns for the first 2 options:

eg: the visitor wants to search for a vechile, so he has to select from first drop down "make" then the second drop down is activated and contains the models corresponding to the first choice. example:: make: acura model: MDX or make: bmw model: 330.

the other search options are also on the page, but i assume i can put them together by my self.

 

so, please help me to solve this problem! :'(

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.