Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Wyszukiwanie pełnotekstowe mysql/php, Wyszikiwanie kilku słów
maarss
post 2.07.2017, 12:15:57
Post #1





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 29.06.2017

Ostrzeżenie: (0%)
-----


Mam problem z wyszukiwarką. Może ktoś pomoże z WAS.

Problem polega na tym iż wpisuje jedno słowo to wyszukuje bez problemu, lecz jak już wpisze dwa słowa to już nic nie pokazuje. Mój kod wygląda tak :

  1. function getRecords($params) {
  2. $rp = isset($params['rowCount']) ? $params['rowCount'] : 10;
  3.  
  4. if (isset($params['current'])) { $page = $params['current']; } else { $page=1; };
  5. $start_from = ($page-1) * $rp;
  6.  
  7. $sql = $sqlRec = $sqlTot = $where = '';
  8.  
  9. if( !empty($params['searchPhrase']) ) {
  10. $where .=" WHERE ";
  11. $where .=" ( employee_name LIKE '%".$params['searchPhrase']."%' ";
  12. $where .=" OR employee_salary LIKE '".$params['searchPhrase']."%' ";
  13.  
  14. $where .=" OR employee_age LIKE '".$params['searchPhrase']."%' ";
  15. $where .=" OR employee_magazyn LIKE '".$params['searchPhrase']."%' )";
  16. }
  17. if( !empty($params['sort']) ) {
  18. $where .=" ORDER By ".key($params['sort']) .' '.current($params['sort'])." ";
  19. }
  20. // getting total number records without any search
  21. $sql = "SELECT * FROM `employee` ";
  22. $sqlTot .= $sql;
  23. $sqlRec .= $sql;
  24.  
  25. //concatenate search sql if value exist
  26. if(isset($where) && $where != '') {
  27.  
  28. $sqlTot .= $where;
  29. $sqlRec .= $where;
  30. }
  31. if ($rp!=-1)
  32. $sqlRec .= " LIMIT ". $start_from .",".$rp;
  33.  
  34.  
  35. $qtot = mysqli_query($this->conn, $sqlTot) or die("error to fetch tot employees data");
  36. $queryRecords = mysqli_query($this->conn, $sqlRec) or die("error to fetch employees data");
  37.  
  38. while( $row = mysqli_fetch_assoc($queryRecords) ) {
  39. $data[] = $row;
  40. }
  41.  
  42. $json_data = array(
  43. "current" => intval($params['current']),
  44. "rowCount" => 10,
  45. "total" => intval($qtot->num_rows),
  46. "rows" => $data // total data array
  47. );
  48.  
  49. return $json_data;
  50. }






Próbowałem już tez :
  1. function getRecords($params) {
  2. $rp = isset($params['rowCount']) ? $params['rowCount'] : 10;
  3.  
  4. if (isset($params['current'])) { $page = $params['current']; } else { $page=1; };
  5. $start_from = ($page-1) * $rp;
  6.  
  7. $sql = $sqlRec = $sqlTot = $where = '';
  8.  
  9. if( !empty($params['searchPhrase']) ) {
  10. $where .=" WHERE ";
  11. $where .="MATCH ( employee_name, employee_salary) AGAINST ('%".$params['searchPhrase']."%') ";
  12. }
  13. if( !empty($params['sort']) ) {
  14. $where .=" ORDER By ".key($params['sort']) .' '.current($params['sort'])." ";
  15. }
  16. // getting total number records without any search
  17. $sql = "SELECT * FROM `employee` ";
  18. $sqlTot .= $sql;
  19. $sqlRec .= $sql;
  20.  
  21. //concatenate search sql if value exist
  22. if(isset($where) && $where != '') {
  23.  
  24. $sqlTot .= $where;
  25. $sqlRec .= $where;
  26. }
  27. if ($rp!=-1)
  28. $sqlRec .= " LIMIT ". $start_from .",".$rp;
  29.  
  30.  
  31. $qtot = mysqli_query($this->conn, $sqlTot) or die("error to fetch tot employees data");
  32. $queryRecords = mysqli_query($this->conn, $sqlRec) or die("error to fetch employees data");
  33.  
  34. while( $row = mysqli_fetch_assoc($queryRecords) ) {
  35. $data[] = $row;
  36. }
  37.  
  38. $json_data = array(
  39. "current" => intval($params['current']),
  40. "rowCount" => 10,
  41. "total" => intval($qtot->num_rows),
  42. "rows" => $data // total data array
  43. );
  44.  
  45. return $json_data;
  46. }
  47.  



emplayee_name i employee_salary mam ustawione jako full text.


Pomożcie proszę.

Widze ze forum bardzo pomocne :(ehh....
Go to the top of the page
+Quote Post
tenloginjestzaje...
post 2.07.2017, 15:39:50
Post #2





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


Ja rozbijam zapytanie ( explode ) i stosuje AND w zapytaniu SQL.

Przykład:
  1. $forumlimit = 10;
  2. $szukaj_ = str_replace(array(' a ',' z ',' i ',' na ',' w ',' do ',' od ',' czy '), ' ', $szukaj);
  3. $xrozbicie_ = explode(' ', $szukaj_);
  4. foreach($xrozbicie_ as $t)
  5. $xrozbicie2[] = "`content` LIKE '%$t%'";
  6. $xrozbicie = implode(" AND ", $xrozbicie2);
  7. $limit = ($page-1)*$forumlimit;
  8. $sql = "SELECT DISTINCT `tag` FROM `rewievs` WHERE $xrozbicie ORDER BY `id` DESC LIMIT $limit,$forumlimit";


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 26.04.2024 - 04:58