|
5 | 5 | * 2021-01-07 -> Class created
|
6 | 6 | * 2021-03-12 -> Added rel="canonical|prev|next" to <a> pagination tags
|
7 | 7 | * 2021-03-12 -> Added a lot of comments
|
8 |
| - * 2021-06-05 -> Added transformWith private method for insert and update methods using the $additional array. Added formatMonney public method to work with monetary values. Changed getPageNow to getCurrentPage |
| 8 | + * 2021-06-05 -> Added transformWith private method for insert and update methods using the $additional array. Added formatMonney public method to work with monetary values. Changed getPageNow to getCurrentPage. Added search method. |
9 | 9 | *
|
10 |
| - * |
11 |
| - * MUST CREATE: search method, wordlist method, similarwords method, searchSimilarWord method |
12 | 10 | */
|
13 | 11 |
|
14 | 12 | class db
|
@@ -37,6 +35,9 @@ class db
|
37 | 35 | /** This is the default class $language */
|
38 | 36 | protected static $language = "en";
|
39 | 37 |
|
| 38 | + /** This is the max number of words given a list of words on search method */ |
| 39 | + protected static $wordLimit = 25; |
| 40 | + |
40 | 41 | /** These are the words that pagination may have */
|
41 | 42 | protected static $defaultPaginationWords = array(
|
42 | 43 | "en" => array(
|
@@ -643,6 +644,57 @@ public static function delete($table, $rules = array())
|
643 | 644 | }
|
644 | 645 | }
|
645 | 646 |
|
| 647 | + /** Will return an array with all words inside the given input */ |
| 648 | + private static function getAllWords($input) |
| 649 | + { |
| 650 | + $words = array_filter(explode(" ", $input), function ($word) { |
| 651 | + if (strlen($word) > 3) { |
| 652 | + return $word; |
| 653 | + } |
| 654 | + }); |
| 655 | + sort($words); |
| 656 | + $allcombinations = array(); |
| 657 | + $total = count($words); |
| 658 | + foreach ($words as $word) { |
| 659 | + for ($iterate = 0; $iterate < $total; $iterate++) { |
| 660 | + if ($words[$iterate] !== $word) { |
| 661 | + $wordNow = $word . " " . $words[$iterate]; |
| 662 | + if (!in_array($wordNow, $allcombinations)) { |
| 663 | + $allcombinations[] = $wordNow; |
| 664 | + } |
| 665 | + } else { |
| 666 | + $allcombinations[] = $word; |
| 667 | + } |
| 668 | + } |
| 669 | + if (count($allcombinations) == self::$wordLimit) { |
| 670 | + break; |
| 671 | + } |
| 672 | + } |
| 673 | + function csort($a, $b) |
| 674 | + { |
| 675 | + return strlen($b) - strlen($a); |
| 676 | + } |
| 677 | + usort($allcombinations, 'csort'); |
| 678 | + echo "<pre>"; |
| 679 | + print_r($allcombinations); |
| 680 | + echo "</pre>"; |
| 681 | + return $allcombinations; |
| 682 | + } |
| 683 | + |
| 684 | + /** It will search for the given input inside the given table and return all records found */ |
| 685 | + public static function search($what, $where) |
| 686 | + { |
| 687 | + $words = self::getAllWords($what); |
| 688 | + $collumns = array(); |
| 689 | + foreach (self::getTableCollumns($where) as $key => $field) { |
| 690 | + $collumns[] = $field['Field']; |
| 691 | + } |
| 692 | + $concat = "CONCAT(" . implode(", ", $collumns) . ")"; |
| 693 | + $sql = "SELECT * FROM $where WHERE $concat LIKE ('%" . implode("%') OR $concat LIKE ('%", $words) . "%')"; |
| 694 | + $query = self::query($sql); |
| 695 | + return $query;; |
| 696 | + } |
| 697 | + |
646 | 698 | /** It will normalize a string to be accepted on URL addresses */
|
647 | 699 | public function URLNormalize($string)
|
648 | 700 | {
|
|
0 commit comments