PHP Split Search Bar Keywords By Spaces
well, brother, it's a bad approach how you doing it. But for learning, its fine. For +
sign issue, you can use php urldecode
, example:
<?php
$tosearch = 'a+b';
echo urldecode($tosearch);
it has its own pro/con thing but on high-level it will work for you, you can dig more into it if you like.
Creating a MYSQL query from results of splitting a string and using LIKE to match keywords in PHP
The problem is here $new_search = preg_split("/,/", $my_search);
use $new_search = preg_split("/, /", $my_search);
instead.
The items in the string are separated by a comma and a space (", ") so you should split the string with that.
Assigning multiple keywords to PHP/MySQL keyword based search
Either use a fulltext index, or split the keyword list into a compound "where like ... or like ... or like ..." statement:
$keywords = explode(' ', $_GET['q']);
$likes = array()
foreach($keywords as $kw) {
$kw = mysql_real_escape_string($kw);
$likes[] = "keywords LIKE '%{$kw}%'";
}
$sql = "SELECT ... WHERE " . implode(' OR ', $likes);
Storing compound data in a single field in a relational database is rarely a good idea, especially if you're going to need to access sub-portions of the whole field. Keywords should be stored in a separate table, one keyword per record. You can then use a link table to relate multiple keywords to each individual record in your parent table.
How to group search results with multiple keywords in MySQL
You can do this with GROUP BY
and HAVING
:
SELECT gr.postId
FROM word_index wi INNER JOIN
word_rel gr
ON wi.word_id = gr.word_id
WHERE wi.keyword IN (keyword1, keyword2)
GROUP BY gr.postId
HAVING COUNT(*) = 2;
You need to construct the IN
list from the search string provided. You then need to assign the comparison value in the HAVING
based on the number of keywords you want to match.
Note: You may need to do some pre-processing to be sure that keywords are not entered twice.
Related Topics
How to Upload and Parse a CSV File in PHP
How to Add a New Method to a PHP Object on the Fly
MySQL Error: 1364 Field 'Display_Name' Doesn't Have Default Value
Print Newline in PHP in Single Quotes
Regular Expression to Check If the String Has Chinese Chars
PHP Create and Save a Txt File to Root Directory
Differencebetween Sessions and Cookies in PHP
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
What Are Fragment Urls and Why to Use Them
Str_Replace() on Multibyte Strings Dangerous
PHP Code to Test Pdo Is Available
Onbeforeprint() and Onafterprint() Equivalent for Non Ie Browsers