Jump to content

multiple keyword search


elduce

Recommended Posts

Hello, I have been working with this searching script and have it working exactly how I need it to but only with one keyword.

 

<?
// Get the search variable from URL

$var = @$_GET['q'];
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=20;

// check for an empty string and display a message.
if ($trimmed == "")
	{
  		echo "<p>Please enter a search...</p>";
  		exit;
  	}

// check for a search parameter
if (!isset($var))
	{
  		echo "<p>We dont seem to have a search parameter!</p>";
  		exit;
  	}

//connect to database
mysql_connect("host", "username", "password"); //(host, username, password)

//specify database
mysql_select_db("database") or die("Unable to select database");

// Build SQL Query  
$query = "select help_page_id, title from help_pages where content like '%$trimmed%' "
."and help_page_id in ( "
."SELECT help_page_id "
."FROM help_pages p "
."INNER JOIN help_topics t "
."ON p.help_topic_id=t.help_topic_id "
."INNER JOIN help_versions v "
."ON t.help_version_id=v.help_version_id "
."WHERE v.help_interface_id=".$help_interface->getPrimaryKey().")";

//echo $query;

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
	{
  		echo "<h4>Results</h4>";
  		echo "<h1>Sorry, your search: "" . $trimmed . "" returned zero results</h1>";
  	}

  else {

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
	$s=0;
  }
  
// get results
	$query .= " limit $s,$limit";
	$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
	echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
	echo "Results<br />";
	$count = 1 + $s ;

// now you can display the results returned
	while ($row= mysql_fetch_array($result)) 
	{
		$title = $row["title"];
		$help_page_id = $row["help_page_id"];
		echo " $count. <a href='/help/index.php?help_id=$help_page_id'>".$title."</a><br />";
		$count++ ;
  	}

	$currPage = (($s/$limit) + 1);

//break before paging
	echo "<br />";

// next we need to do the links to other results
	if ($s>=1) { // bypass PREV link if s is 0
	$prevs=($s-$limit);
	print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
	Prev 20</a>&nbsp ";
	}

// calculate number of pages needing links
	$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

	if ($numrows%$limit) { // has remainder so add one page
	$pages++;
  	}

// check to see if last page
	if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
	$news=$s+$limit;

	echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 20 >></a>";
  	}

$a = $s + ($limit) ;
  	if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
}
?>

 

Here is the part that works but for some reason when I try to add the array

 

// I can't seem to get these parts to work in the above script. I extracted them to keep a working copy which accepts one word.

$trimmed = html_entities($_POST['q'], ENT_QUOTES);
// (replace quotes, etc. with HTML entities)

if(strtok($trimmed, " ") !== FALSE)
  {
  // No spaces found (only 1 word), so the query should be...
$query = "select help_page_id, title from help_pages where content like '%$trimmed%' "
."and help_page_id in ( "
."SELECT help_page_id "
."FROM help_pages p "
."INNER JOIN help_topics t "
."ON p.help_topic_id=t.help_topic_id "
."INNER JOIN help_versions v "
."ON t.help_version_id=v.help_version_id "
."WHERE v.help_interface_id=".$help_interface->getPrimaryKey().")";
  }
else
  {
  // Found a space, so now you can safely use explode()
  // I GET LOST HERE 
  $query = "select help_page_id, title from help_pages where content like ";

  // Create an array of keywords...
  $words = explode(" ", $trimmed);

  // Add each keyword in the array to your query...
  foreach($words as $keyword)
    $query .= "'$keyword' OR LIKE ";

  // Remove the last "OR LIKE "...
  $query = substr($query, 0, -;
  } 

 

If anyone has some time to check it out and give me some suggestions it would be greatly appreciated. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/169260-multiple-keyword-search/
Share on other sites

You are on the right track with the foreach() loop and splitting the $trimmed string on " ".

 

Since I can't sit down and analyze all your code at the moment, I will tell you to use print_r($words) to check the string it splitting the data as you expect, and then work from there.

 

I think you are better off processing your query string at the start of the script to build an array that contains search terms you can easily loop through and perform a SELECT statement(s) on.

 

Simplified example:

 

<?php

$queryString = 'this is a test';
$queryArray = explode(" ", $queryString);
// show me the contents of queryArray for debugging purposes, and so i know what data i'm working with.
print_r($queryArray);
$dbQuery = "SELECT fieldName1,fieldName2 FROM tableName WHERE ";
foreach ($queryArray => $key as $searchTerm)
{
    $dbQuery .= "(searchFieldName LIKE '".$searchTerm."')";
    if (isset($queryArray[$key + 1]))
        $dbQuery .= " OR ";
}
?>

 

Above code is not checked and doesn't do any data control or verification for simplicity sake plus I HAVE TO GO TO WORK!

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.