PHP Search Results

I’m working with a client who now wants their search to include entire phrases. So, rather than when a user searches for “PHP help” and the results include all documents have have both PHP and help, it would only return those with the entire phrase “PHP help.” Is there anything in the code below that would affect how that happens?

Thanks for any help.

[php]// get order
if (!isset($_GET[‘orderby’]) & $_REQUEST[‘keywords’]!=’’) {$orderby=‘relevancy DESC’;}
else if (!isset($_GET[‘orderby’]) & $_REQUEST[‘keywords’]==’’) {$orderby=‘title’;}
else if ($_GET[‘orderby’]==‘title’) {$orderby=‘title’;}
else if ($_GET[‘orderby’]==‘date’ & $_REQUEST[‘keywords’]!=’’) {$orderby=‘date DESC, relevancy DESC’;}
else if ($_GET[‘orderby’]==‘date’ & $_REQUEST[‘keywords’]==’’) {$orderby=‘date DESC, title’;}
else if ($_GET[‘orderby’]==‘state’ & $_REQUEST[‘keywords’]!=’’) {$orderby=‘state, relevancy DESC’;}
else if ($_GET[‘orderby’]==‘state’ & $_REQUEST[‘keywords’]==’’) {$orderby=‘state, title’;}
else if ($_GET[‘orderby’]==‘designation’ & $_REQUEST[‘keywords’]!=’’) {$orderby=‘designation, relevancy DESC’;}
else if ($_GET[‘orderby’]==‘designation’ & $_REQUEST[‘keywords’]==’’) {$orderby=‘designation, title’;}
else if ($_GET[‘orderby’]==‘downloads’ & $_REQUEST[‘keywords’]!=’’) {$orderby=‘downloads DESC, relevancy DESC’;}
else if ($_GET[‘orderby’]==‘downloads’ & $_REQUEST[‘keywords’]==’’) {$orderby=‘downloads DESC, title’;}
else if ($_GET[‘orderby’]==‘size’ & $_REQUEST[‘keywords’]!=’’) {$orderby=‘size DESC, relevancy DESC’;}
else if ($_GET[‘orderby’]==‘size’ & $_REQUEST[‘keywords’]==’’) {$orderby=‘size DESC, title’;}

$keywords=mysql_real_escape_string($_REQUEST[‘keywords’]);

	// find out if we should pull up and show state and/or designation
	$state_query="";
	$designation_query="";
	
			if ($_REQUEST['project']!='') {
			// state array
			$state_array=array();
			$query_states = "SELECT ID FROM projects WHERE state=1"; 
			$r_states = mysql_query ($query_states);
			while ($row_states = mysql_fetch_array($r_states)) {
			$state_array[]=$row_states['ID'];
			}
			if (in_array($_REQUEST['project'],$state_array)) {
			$state="yes";
			$state_query=", documents.state";
			}
			
			// designation array
			$designation_array=array();
			$query_designation = "SELECT ID FROM projects WHERE designation=1"; 
			$r_designation = mysql_query ($query_designation);
			while ($row_designation = mysql_fetch_array($r_designation)) {
			$designation_array[]=$row_designation['ID'];
			}
			if (in_array($_REQUEST['project'],$designation_array)) {
			$designation="yes";
			$designation_query=", documents.designation";
			}
			}

	
	// project and keywords
	if ($_REQUEST['keywords']!='' & $_REQUEST['project']!='' & $_REQUEST['category']=='') {
	$query_label="SELECT title FROM projects WHERE ID = " . (int)$_REQUEST['project'] . " LIMIT 1";
	$r_label = mysql_query ($query_label);
	$row_label = mysql_fetch_array ($r_label);
	$expression = "<b>{$_REQUEST['keywords']}</b> in <i>{$row_label['title']}</i>";		
	
	// get total searched
	$query = "SELECT document_ID FROM documents_projects WHERE project_ID=" . (int)$_REQUEST['project'];
	$r = mysql_query ($query);
	$total = mysql_num_rows($r);
	
	// max query
	$query = "SELECT documents.ID, documents.date{$state_query}{$designation_query}, (3*MATCH(documents.title) AGAINST ('{$keywords}')) + (1.2*MATCH(documents.author) AGAINST ('{$keywords}')) + (MATCH(documents.text) AGAINST ('{$keywords}')) AS relevancy FROM documents_projects INNER JOIN documents ON documents.ID = documents_projects.document_ID WHERE documents_projects.project_ID = " . (int)$_REQUEST['project'] . " HAVING relevancy > 0 ORDER BY RELEVANCY DESC LIMIT 1";
	$r = mysql_query ($query);
	$row = mysql_fetch_array($r);
	$max = $row['relevancy'];		
	
	// query
	$query = "SELECT documents.title, documents.ID, documents.website, documents.author, documents.size, documents.downloads, documents.date{$state_query}{$designation_query}, (3*MATCH(documents.title) AGAINST ('{$keywords}')) + (1.2*MATCH(documents.author) AGAINST ('{$keywords}')) + (MATCH(documents.text) AGAINST ('{$keywords}')) AS relevancy FROM documents_projects INNER JOIN documents ON documents.ID = documents_projects.document_ID WHERE documents_projects.project_ID = " . (int)$_REQUEST['project'] . " HAVING relevancy > 0 ORDER BY {$orderby}";
	$r = mysql_query ($query);
	
	// category and keywords
	} else if ($_REQUEST['keywords']!='' & $_REQUEST['category']!='') {
	$query_label="SELECT title FROM projects WHERE ID = " . (int)$_REQUEST['project'] . " LIMIT 1";
	$r_label = mysql_query ($query_label);
	$row_label = mysql_fetch_array ($r_label);
	$query_label2="SELECT category FROM new_categories WHERE ID = " . (int)$_REQUEST['category'] . " LIMIT 1";
	$r_label2 = mysql_query ($query_label2);
	$row_label2 = mysql_fetch_array ($r_label2);		
	$expression = "<b>{$_REQUEST['keywords']}</b> in <i>{$row_label['title']}</i> &gt; <i>{$row_label2['category']}</i>";		
	
	// get total searched
	$query = "SELECT ID FROM documents_categories WHERE category_ID=" . (int)$_REQUEST['category'];
	$r = mysql_query ($query);
	$total = mysql_num_rows($r);
	
	// max query
	$query = "SELECT documents.ID, (3*MATCH(documents.title) AGAINST ('{$keywords}')) + (MATCH(documents.text) AGAINST ('{$keywords}')) AS relevancy FROM documents_categories INNER JOIN documents ON documents.ID = documents_categories.document_ID WHERE documents_categories.category_ID = " . (int)$_REQUEST['category'] . " HAVING relevancy > 0 ORDER BY relevancy DESC LIMIT 1"; 

	$r = mysql_query ($query);
	$row = mysql_fetch_array($r);
	$max = $row['relevancy'];	
	
	// query
	$query = "SELECT documents.title, documents.ID, documents.website, documents.author, documents.size, documents.downloads, documents.date{$state_query}{$designation_query}, (3*MATCH(documents.title) AGAINST ('{$keywords}')) + (1.2*MATCH(documents.author) AGAINST ('{$keywords}')) + (MATCH(documents.text) AGAINST ('{$keywords}')) AS relevancy FROM documents_categories INNER JOIN documents ON documents.ID = documents_categories.document_ID WHERE documents_categories.category_ID = " . (int)$_REQUEST['category'] . " HAVING relevancy > 0 ORDER BY {$orderby}"; 
	$r = mysql_query ($query);
	
	// keyword only
	} else if ($_REQUEST['keywords']!='' & $_REQUEST['project']=='') {
	$expression = "Keyword <b>{$_REQUEST['keywords']}</b>";
	
	// get total searched
	$query = "SELECT ID FROM documents";
	$r = mysql_query ($query);
	$total = mysql_num_rows($r);
	
	// max query
	$query = "SELECT ID,(1.2*MATCH(title) AGAINST ('{$keywords}')) + (1.2*MATCH(author) AGAINST ('{$keywords}')) + (MATCH(text) AGAINST ('{$keywords}')) AS relevancy FROM documents HAVING relevancy > 0 ORDER BY relevancy DESC LIMIT 1";
	$r = mysql_query ($query);
	$row = mysql_fetch_array($r);
	$max = $row['relevancy'];		
	
	// query
	$query = "SELECT ID, title, author, website, downloads, size, date, (1.2*MATCH(title) AGAINST ('{$keywords}')) + (1.2*MATCH(author) AGAINST ('{$keywords}')) + (MATCH(text) AGAINST ('{$keywords}')) AS relevancy FROM documents HAVING relevancy > 0 ORDER BY {$orderby}";
	$r = mysql_query ($query);
	
	// project only
	} else if ($_REQUEST['keywords']=='' & $_REQUEST['project']!='' & $_REQUEST['category']=='') {
	$query_label="SELECT title FROM projects WHERE ID = " . (int)$_REQUEST['project'] . " LIMIT 1";
	$r_label = mysql_query ($query_label);
	$row_label = mysql_fetch_array ($r_label);
	$expression = "<b>All documents</b> in <i>{$row_label['title']}</i>";
	$expression_cats = "<i>{$row_label['title']}</i>";		
	
	// query
	$query = "SELECT documents.title, documents.ID, documents.website, documents.author, documents.size, documents.downloads, documents.date{$state_query}{$designation_query} FROM documents_projects INNER JOIN documents ON documents.ID = documents_projects.document_ID WHERE documents_projects.project_ID = " . (int)$_REQUEST['project'] . " ORDER BY {$orderby}"; 
	$r = mysql_query ($query);
	$total = mysql_num_rows($r);
	
	// category only
	} else if ($_REQUEST['keywords']=='' & $_REQUEST['category']!='') {
	$query_label="SELECT title FROM projects WHERE ID = " . (int)$_REQUEST['project'] . " LIMIT 1";
	$r_label = mysql_query ($query_label);
	$row_label = mysql_fetch_array ($r_label);
	$query_label2="SELECT category FROM new_categories WHERE ID = " . (int)$_REQUEST['category'] . " LIMIT 1";
	$r_label2 = mysql_query ($query_label2);
	$row_label2 = mysql_fetch_array ($r_label2);				
	$expression = "<b>All documents</b> in <i>{$row_label['title']}</i> &gt; <i>{$row_label2['category']}</i>";
	$expression_cats = "<i>{$row_label['title']}</i> &gt; <i>{$row_label2['category']}</i>";

	// query
	$query = "SELECT documents.title, documents.ID, documents.website, documents.author, documents.size, documents.downloads, documents.date{$state_query}{$designation_query} FROM documents_categories INNER JOIN documents ON documents.ID = documents_categories.document_ID WHERE documents_categories.category_ID = " . (int)$_REQUEST['category'] . " ORDER BY {$orderby}"; 
	$r = mysql_query ($query);
	$total = mysql_num_rows($r);
	
	} else { echo "You must select either a keyword or a category to continue";
	
	}
	?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service