bottom
wait
Valid XHTML 1.0!
Get Firefox!
Programming tutorials - Function for joining words (PHP)
In this tutorial we'll make a function that could be used for a simple search engine. The resul will be an SQL command that you can use further to examine the MySQL data base. The function will be called joining_sql() and will have as resource the key words that can be proceed from an form filled in by an user. An user could enter as many words as he wants and thats why we need to analyse them all. The function joining_sql() don't use an for sintax because this solution wasn't so good as this chosen by me in this tutorial. As I said the result will be an SQL command, something like: "SELECT * FROM _table_ WHERE _column_ LIKE "%Those%" OR _column_ LIKE "%are%" OR _column_ LIKE "%a%" OR _column_ LIKE "%few%" OR _column_ LIKE "%key%" OR _column_ LIKE "%words%" OR _column_ LIKE "%to%" OR _column_ LIKE "%search%"; ", resulted from joining_sql function with resource "Those are a few key words to search".
Let see next how we do this thing. For the first time we take all key words as a string and we apply explode function to it with string separator " " (space) to enter each word in an array. Then, the array's elements we'll bring in them back to string form with %" after each element (key word). This string will introduce in an variable called $where. Beforehand we set the variable $sql with value SELECT * FROM _table_ WHERE _column_ LIKE "%. The variable $where have now the value Those%" are%" a%" few%" key%" words%" to%" search.
$pieces = explode(" ", $string);
$sql = 'SELECT * FROM _table_ WHERE _column_ LIKE "%';
$where = join('%" ', $pieces);
But we need the "% chars after each key word. This thing we'll make it as we did since now but with the string reversed by appling the function strrev() then we bring in back to initial form by appling the same function strrev(). Now the SQL command is something like: Those%" "%are%" "%a%" "%few%" "%key%" "%words%" "%to%" "%search.
$where = strrev($where);
$pieces = explode(" ", $where);
$where = join('%" ', $pieces);
$where = strrev($where);
We need after each key word to introduce the SQL operator OR _column_ LIKE and wee do this thing as we did since now but we use the sintax below for this time as resource "string glue" of function join(). All we have to do is to join the variables $sql and $where and in the end the string %". Now we have a function that we can use it to extract what we need from data base. This is just an example for a simple SQL command and you need to connect beforehand to data base and modify the name of table and column.
  1. <?php
  2. function joining_sql($string){
  3. $pieces = explode(" ", $string);
  4. $sql = 'SELECT * FROM _table_ WHERE _column_ LIKE "%';
  5. $where = join('%" ', $pieces);
  6. $where = strrev($where);
  7. $pieces = explode(" ", $where);
  8. $where = join('%" ', $pieces);
  9. $where = strrev($where);
  10. $pieces = explode(" ", $where);
  11. $where = join(' OR _column_ LIKE ', $pieces);
  12. $sql = $sql.$where.'%"';
  13. return $sql;
  14. }
  15. joining_sql('Those are a few key words to search');
  16. ?>
Print this page
bottom