Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 756231f

Browse files
feature: Added search method and improved many other methods within the class
1 parent 4b4a8e4 commit 756231f

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,7 @@ Will delete whole table if $rules is omitted
109109
## URLNormalize
110110

111111
Makes given string url friendly
112+
113+
## search
114+
115+
Simple search engine. It uses the content inside database (so, if database doesn't have much records...)

‎db.class.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
* 2021年01月07日 -> Class created
66
* 2021年03月12日 -> Added rel="canonical|prev|next" to <a> pagination tags
77
* 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.
99
*
10-
*
11-
* MUST CREATE: search method, wordlist method, similarwords method, searchSimilarWord method
1210
*/
1311

1412
class db
@@ -37,6 +35,9 @@ class db
3735
/** This is the default class $language */
3836
protected static $language = "en";
3937

38+
/** This is the max number of words given a list of words on search method */
39+
protected static $wordLimit = 25;
40+
4041
/** These are the words that pagination may have */
4142
protected static $defaultPaginationWords = array(
4243
"en" => array(
@@ -643,6 +644,57 @@ public static function delete($table, $rules = array())
643644
}
644645
}
645646

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+
646698
/** It will normalize a string to be accepted on URL addresses */
647699
public function URLNormalize($string)
648700
{

‎index.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44

55
db::addConnection('default', array("HOST" => "localhost", "USER" => "root", "PASSWORD" => "", "NAME" => "test"));
66

7-
db::insert(array("monney" => "1,00.50"), "monney");
7+
$resultado = db::search("teste comum verdadeiro falso com amor", "emails");
8+
9+
while ($dado = db::fetch($resultado)) {
10+
echo "<pre>";
11+
print_r($dado);
12+
echo "</pre>";
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /