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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

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.