contra10 Posted November 18, 2009 Share Posted November 18, 2009 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? Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 18, 2009 Share Posted November 18, 2009 What do you mean by 'better'? Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 18, 2009 Share Posted November 18, 2009 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? Quote Link to comment Share on other sites More sharing options...
contra10 Posted November 19, 2009 Author Share Posted November 19, 2009 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 Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 19, 2009 Share Posted November 19, 2009 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'; } Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 19, 2009 Share Posted November 19, 2009 You might try FULL TEXT search http://onlamp.com/onlamp/2003/06/26/fulltext.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.