Jump to content

creating a better search


contra10

Recommended Posts

hey I'm trying to create a better search bar because I find LIKE to be very simple

 

<?php
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM `musiclinks` WHERE `title` LIKE '%$sr%' $max") or die(mysql_error()); 
if ($data_p){
//This is where you display your query results
while ($postede = mysql_fetch_assoc($data_p))
{
$name = "{$postede['title']}";
$genre = "{$postede['genre']}";
$filename = "{$postede['filename']}";
$embed = "{$postede['embed']}";
$false = 'False';
$true = 'True';
}

?>

 

how can I make the search a lot better I know I have to work on variable $sr, should i break it down into strings. how should i go about doing that?

Link to comment
https://forums.phpfreaks.com/topic/182071-creating-a-better-search/
Share on other sites

is $sr a $_POST variable..? with a term like "the beatles"?

you can do something like

$sr = explode(" ", $sr);
$sr = implode("%", $sr);

or

$sr = str_replace(" ", "%", $sr);

would do the same...

then you will get $sr = 'the%beatles'

 

is that what you were after?

sry by better I meant allows more flexibility with the search

 

I used implode seems to be working well

 

but for example because my site is a music site when someone searches for

 

black eyed peas I gotta feeling

 

the song comes up in search results as

 

black eyed peas - i gotta feeling

 

which is what is in the db as the title

but when someone searches

 

i gotta feeling black eyed peas

 

no results comes up

you could have it split the search terms up so its "WHERE title LIKE '%i%' AND title LIKE '%gotta%' AND title LIKE '%feeling%' AND title LIKE '%black%' AND title LIKE '%eyed%' AND title LIKE '%pees%'"... i'm assuming there'd be an easier way tho

i.e.

$search = "i gotta feeling black eyed pees";
$search = ltrim(rtrim($search)); //get rid of whitespace at start n end
$search = explode(" ", $search);

$sqlSearch = array();

foreach ($search as $s)
{
$sqlSearch[] = "title LIKE '%$s%'";
}

$sqlSearch = implode(" AND ", $sqlSearch);

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM `musiclinks` WHERE $sqlSearch $max") or die(mysql_error()); 
if ($data_p){
//This is where you display your query results
while ($postede = mysql_fetch_assoc($data_p))
{
   $name = "{$postede['title']}";
$genre = "{$postede['genre']}";
$filename = "{$postede['filename']}";
$embed = "{$postede['embed']}";
$false = 'False';
$true = 'True';
}

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.