Jump to content

Recommended Posts

Hello to all people on forum.

I built web site with large database of some texts and for easier finding what you need on it, I wanted to include search engine!

I built it, but it only works accurate with one word. Problem is when I want to search few word!

If they are separated or in different order, search is failure.

Here's my code:

 

 

function trazi($table,$search) {

$sve = mysql_query("SELECT id,headline,whole FROM $table ORDER BY id DESC");

$data = "";

while($tmp = mysql_fetch_array($sve)) {

if(stristr($tmp['headline'], $search) || stristr($tmp['whole'], $search)) {

$data .= '<a href="www.mysite.com/' . $table . '_cur.php?id=' . $tmp[0] . '" target="_blank" style=" text-decoration:none; color:#1A5C1F; font-size: 13px;">';

$data .= '<p>' . $tmp[1] . '</p>';

$data .= '</a>';

}

}

return $data;

}

 

So, I want search to be success only if ALL words can be found in headline OR in whole (text)!!

 

Can you help me modify my code?! Thx anyway!

Link to comment
https://forums.phpfreaks.com/topic/241438-help-in-search-engine/
Share on other sites

I wanted that searched words are included in headline or within whole (text)...

 

Im not sure, but your code is not what I need, friend!!!

It will work only if searched word and tables (headline or whole) are equal!

 

I need to upgrade my function so I can find result if I seek for 2 or more words..Its working perfect as it is now, but only for 1 word!!!

  • I have not tested it but it should work.
  • This is just what i got out of my head right not :P.
  • it's very dirty and i would not suggest using it if you have a lot of users.
  • REMEMBER to check the terms for evil activity be for passing it to a function like this!!!! (sql-injection).
     

 

function search ($term)
{
$idArray = array();
$i = 0;
$info;

$searchTerms = explode(" ",$term);	

foreach($searchTerms as $word)
{
	$currentIdArray = array();
  $wordsSql = mysql_query("SELECT id FROM table WHERE headline LIKE '%$word%'");
  while($headline = mysql_fetch_array($wordsSql))
  {
	 $currentIdArray[$headline['id']] =  $headline['id'];  

   }
   if($i == 0)
	   {
			  $idArray[$headline['id']] = $headline['id'];
		}else{
			  if(!in_array($headline['id'],$idArray)){
				  unset($idArray[$headline['id']]);
			  }
		}
   $i++;
}
$j = 0;
foreach($idArray as $id)
{
	$sql = mysql_query("SELECT something FROM table WHERE id='$id'");
	$info[$j] = mysql_fetch_assoc($sql);
	$j++;
}
return $info;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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