work_it_work Posted May 13, 2008 Share Posted May 13, 2008 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 More sharing options...
benphp Posted May 13, 2008 Share Posted May 13, 2008 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; } ?> Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-540040 Share on other sites More sharing options...
work_it_work Posted May 13, 2008 Author Share Posted May 13, 2008 benphp thanks for your help but i'm still confused :'( i need the whole script so i can understand. i mean the search form too.... tks! Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-540450 Share on other sites More sharing options...
work_it_work Posted May 14, 2008 Author Share Posted May 14, 2008 i'm still waiting for an answer... can anyone help me please? Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-540858 Share on other sites More sharing options...
work_it_work Posted May 14, 2008 Author Share Posted May 14, 2008 waiting... Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-541067 Share on other sites More sharing options...
BlueSkyIS Posted May 14, 2008 Share Posted May 14, 2008 you need the whole script so you can understand? you may be waiting a very long time. Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-541073 Share on other sites More sharing options...
freakus_maximus Posted May 14, 2008 Share Posted May 14, 2008 Google returns a ton of results such as (searching on php mysql full text search): http://devzone.zend.com/node/view/id/1304#Heading12 They even have a section called "Example: Basic Searching Application". Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-541184 Share on other sites More sharing options...
RichardRotterdam Posted May 14, 2008 Share Posted May 14, 2008 Neat!!! great article thanks for the post Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-541213 Share on other sites More sharing options...
work_it_work Posted May 14, 2008 Author Share Posted May 14, 2008 thanks everyone for your help, i'm starting to study now, i hope to understand @ BlueSkyIS yes, i'm waiting for a full script so i can understand... if anyone can share, i'm open Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-541341 Share on other sites More sharing options...
work_it_work Posted May 15, 2008 Author Share Posted May 15, 2008 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! :'( Link to comment https://forums.phpfreaks.com/topic/105443-need-urgent-help-with-search-in-php/#findComment-542188 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.