Advertisement

05.18.2008 at 08:09AM PDT, ID: 23411926
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Building php search engine, with relevant results

Asked by socross in PHP and Databases, MySQL Server, SQL Query Syntax

Tags:

HI

I have been having real trouble getting my head around displaying relevant results, i have asked various questions with regads the mysql SELECT statement but have real issues working out the next bit.

I will attach untested code, which is using some tutorials and some of my own functions.

The bit i am struggling with is the ordering of results, at the moment i take the search string and splt it into an array, with each value being one of the words.

I then SELECT results from my database which match these words and store the row ids in a new array.

I use a case statement to order the SELECT results.

The bit im stuck on is ordering the results depending on how many of the original string keywords are found in a row!!

Here is my code

Any suggestions would be much appriciated!

--s--Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
//
//--//--//  PROCESS SEARCH  //--//	
 
 
// Specify how many results to display per page
	$limit = 8;
// Get the search variable from URL
	$var = @$_POST['search-input'];
// Trim whitespace from the stored variable
	$trimmed = trim($var);  
// Separate key-phrases into keywords
	$trimmed_array = explode(" ",$trimmed);
// Check for an empty string and redirect || Check for no search var and redirect
	if ($trimmed == "" || !isset($var))
  		redirect('/search-empty/');
		
		
// Build SQL Query for each keyword entered 
foreach ($trimmed_array as $trimm){
 
	// SET QUERY VARS
	$db_table = 'for_search';
	$db_values = array('mach_id');
	$db_where = 'WHERE manu_name LIKE "%'.$trimm.'%" OR name LIKE "%'.$trimm.'%" OR details LIKE "%'.$trimm.'%"';
	$db_order = 'ORDER BY (CASE WHEN manu_name LIKE "%'.$trimm.'%" THEN 1 ELSE 0 END + CASE WHEN name LIKE "%'.$trimm.'%" THEN 1 ELSE 0 END + CASE WHEN details LIKE "%'.$trimm.'%" THEN 1 ELSE 0 END) DESC';
	$db_return = 'm_arr';
	
	//select_record($db_table, $db_values, $db_where, $db_order, $db_return);
	
      
 
     // Execute the query to  get number of rows that contain search kewords
     $row_num_links_main = get_rows($db_table, $db_values, $db_where, $db_order, $db_return);
 
     // next determine if 's' has been passed to script, if not use 0.
     // 's' is a variable that gets set as we navigate the search result pages.
     if (empty($s)) {
         $s=0;
     }
 
      // now let's get results.
     $db_order .= " LIMIT $s,$limit" ;
     $row_num_links_main = select_record($db_table, $db_values, $db_where, $db_order, $db_return);
 
      //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
	  foreach($row_num_links_main as $r)
          $adid_array[] = $r['mach_id'];
	  }
 } //end foreach
 
if($row_num_links_main == 0 && $row_set_num == 0){
	
	
	// Put no search results redirects here - send get var with search
	redirect('/search-empty/');
 
}
   //delete duplicate record id's from the array. To do this we will use array_unique function
   $tmparr = array_unique($adid_array); 
   $i=0; 
   foreach ($tmparr as $v) { 
       $newarr[$i] = $v; 
       $i++; 
   }
 
// now you can display the results returned.
[+][-]05.18.2008 at 08:47AM PDT, ID: 21593076

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP and Databases, MySQL Server, SQL Query Syntax
Tags: PHP MYSQL SQL
Sign Up Now!
Solution Provided By: DarkoLord
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.18.2008 at 02:57PM PDT, ID: 21594203

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.18.2008 at 04:27PM PDT, ID: 21594447

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.18.2008 at 04:35PM PDT, ID: 21594472

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.19.2008 at 06:19AM PDT, ID: 21597483

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.25.2008 at 03:14PM PDT, ID: 21643799

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628