Jump to content

Need help with code to search database


gerble1000

Recommended Posts

hi guys im new here and new to php

 

i have this search code in my website that returns the matching search searches.

my problem is the script is shit!

if the title i am looking for is "Captain America"

and i search "captain america" it returns with no issue,

but if my title is "Captain.America"

then i get no results.

i figured i need to tell the script to take these characters in the title "_-." and look at them as spaces.

here is the code i got

 

 

 

$search = $this->getState('filter.search');


if (!empty($search)) {

if (stripos($search, 'id:') === 0) { 
$query->where('t.fid = '.(int) substr($search, 3)); 
} else {

$search = $db->Quote('%'.$db->getEscaped($search, true).'%'); 

$query->where('( t.name LIKE '.$search.' )');
}

}


return $query;

could somebody explain as to what i should do please :)

been racking my brain for hours on this

 

i was coming up with ideas like this

$search = $this->getState('filter.search');
$search2 = str_replace(' ', '.', $search);

if (!empty($search)) {

if (stripos($search, 'id:') === 0) { 
$query->where('t.fid = '.(int) substr($search, 3)); 
} else {

$search = $db->Quote('%'.$db->getEscaped($search, true).'%'); 
$search2 = $db->Quote('%'.$db->getEscaped($search2, true).'%'); 
$query->where('( t.name LIKE '.$search.$search2.' )');
}

}


return $query;

but this just gave errors :(

Link to comment
https://forums.phpfreaks.com/topic/283353-need-help-with-code-to-search-database/
Share on other sites

There is -- But, it's not the most efficient way to handle this! Every search is having to do a 3 REPLACE()'s on every title to replace those 3 characters into spaces to find any matches.

$query->where("( REPLACE(REPLACE(REPLACE(t.name,'.',' '),'_',' '),'-',' ') LIKE {$search} )");

Don't do this. Not only is it a waste of resources, it also prevents the database from using indexes, so the query becomes as slow as it can possibly get.

 

A simple alternative is to create a separate table that holds just the searchable data. Then you can prepare that data once, store it in the new table, and search the new table using a simple "=" or even a LIKE or FULLTEXT. A small trigger can keep the searchtable uptodate when the original table is inserted/deleted or updated upon.

An added bonus of this method is that you can also separately store each word of the text and find texts that do not have an exact match, by counting the number of words that do match.

 

And although some people on this forum will hate me for it, I'm still going to say that in other databases you'd solve this problem using a functional index; an index built on an expression such as the triple-replace mentioned earlier, saving you the trouble of creating the separate table.

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.