dbsearch-class

Gesponsorde koppelingen

PHP script bestanden

  1. dbsearch-class

« Lees de omschrijving en reacties

Voorbeeldje van gebruik

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?
require('./libs/lib.dbsearch.php');

if(empty($_GET['q'])){
    $_GET['q'] = '[niet gedefinieerd]';
}

?>

<h1><?=Googlelize('zoeken');?></h1>
<form action="" method="get">
    <input type="hidden" name="page" value="searchresults" />
    <input type="text" name="q" value="<?=$_GET['q']?>"/>
    <input type="submit" name="search" value="zoek!" /><br />
    <select name="type">
    <?
        unset($sel);
        switch($_GET['type']){
            case
'rel': $a = 0; break;
            case
'starts': $a = 1; break;
            case
'like': $a = 2; break;
            case
'ends': $a = 3; break;
            default:
$a = 0; break;
        }

        $sel[$a] = "selected";
    ?>

        <option value="rel" <?=$sel[0]?>>Relevantie</option>
        <option value="starts" <?=$sel[1]?>>Beginnend met</option>
        <option value="like" <?=$sel[2]?>>Bevat</option>
        <option value="ends" <?=$sel[3]?>>Eindigd met</option>
    </select>
    Aantal resultaten per pagina <select name="amount">
    <?
        unset($sel);
        switch($_GET['amount']){        
            case
10: $a = 0; break;
            case
20: $a = 1; break;
            case
30: $a = 2; break;
            case
40: $a = 3; break;
            case
50: $a = 4; break;
            default:
$a = 0; break;
        }

        $sel[$a] = "selected";
    ?>

        <option value="10" <?=$sel[0]?>>10</option>
        <option value="20" <?=$sel[1]?>>20</option>
        <option value="30" <?=$sel[2]?>>30</option>
        <option value="40" <?=$sel[3]?>>40</option>
        <option value="50" <?=$sel[4]?>>50</option>
    </select><br />
    <input type="checkbox" name="in_0" <?=($_GET['in_0']=='on'?'checked="on"':'')?> /> Klanten<br />
    <input type="checkbox" name="in_1" <?=($_GET['in_1']=='on'?'checked="on"':'')?> /> Contactpersonen<br />
    <input type="checkbox" name="in_2" <?=($_GET['in_2']=='on'?'checked="on"':'')?> /> Projecten<br />
    <input type="checkbox" name="in_3" <?=($_GET['in_3']=='on'?'checked="on"':'')?> /> Producten<br />
    

</form>
<?

if(isset($_GET['search'])){
    if(!empty($_GET['q'])){    
        $dbsearch = new MyOwnGoogle(mysql_real_escape_string($_GET['q']),$_GET['type']);
        foreach($_GET as $key => $var){
            if(substr($key,0,3) == 'in_'){
                if($var == 'on'){
                    switch(substr($key,3)){
                        case
0: $dbsearch->addColumn("customers", "name"); break;
                        case
1:
                            $dbsearch->addColumn("customer_employees", "firstname");
                            $dbsearch->addColumn("customer_employees", "lastname");
                            break;
                        case
2: $dbsearch->addColumn("projects", "name"); break;
                        case
3: $dbsearch->addColumn("products", "title"); break;
                    }
                }
            }
        }

        switch($_GET['amount']){        
            case
10: $dbsearch->searchAmount = 10; break;
            case
20: $dbsearch->searchAmount = 20; break;
            case
30: $dbsearch->searchAmount = 30; break;
            case
40: $dbsearch->searchAmount = 40; break;
            case
50: $dbsearch->searchAmount = 50; break;
            default:
$dbsearch->searchAmount = 10; break;
        }

        
        print $dbsearch->Search();
                
    }
}

?>


De classe
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?
# gemaakt door P van Linschoten

class MyOwnGoogle{
    
    #@ variabelen setten enzeau
    protected $searchColumns = array();
    protected $searchWords = NULL;
    protected $searchType = NULL;
    protected $StartTime = 0;
    protected $EndTime = 0;
    protected $aResults = array();
    public $searchAmount = 0;
    
    # init
    public function __construct($words,$type){
        $this->StartTime = microtime();
        $this->searchAmount = 10;
        if(preg_match('[a-zA-Z0-9]',$words)){
                
        }

        $this->searchWords = trim($words);
        $this->searchType = $this->getSearchType($type);
    }
    

    #@ Bepaal in welke kollommen gezocht moet worden.
    public function addColumn($table,$column){
        $this->searchColumns[$table][] = $column;
    }

    #@ Voer de zoekopdracht uit.
    public function Search(){
        switch($this->searchType){
            case
'rel':
                $this->searchFulltext();
            break;
            case
'like':
                $this->searchLike();
            break;
        }

        if(count($this->aResults) == 0){
            return 'Er zijn geen zoekresultaten voor <strong>'.htmlentities($this->searchWords).'</strong>';
        }
else{
            return $this->tableFormat();
        }
    }

    #@ Fulltext zoeken!
    protected function searchFulltext(){
        foreach($this->searchColumns as $table => $columns){
            $i = 0;
            $sColumns = NULL;
            foreach($columns as $column){
                $i++;
                $sColumns .= ($i==1?'':',').$column;
            }

            $query = "SELECT * FROM ".$table." WHERE MATCH(".$sColumns.") AGAINST('".$this->searchWords."') LIMIT ".$this->searchAmount;
            $query = mysql_query($query) or print('<p>'.mysql_error().'</p>');
            while($result = mysql_fetch_assoc($query)){
                 $this->aResults[] = $result;
            }
        }
    }

    #@ zoeken dmv %like%
    protected function searchLike(){
        foreach($this->searchColumns as $table => $columns){
            
            foreach($columns as $column){
                
                $query = "SELECT * FROM ".$table." WHERE ".$column." LIKE '%".$this->searchWords."%' LIMIT ".$this->searchAmount;
                print $query.'<br />';
                $query = mysql_query($query);
                
                 while($result = mysql_fetch_assoc($query)){
                     $this->aResults[] = $result;
                 }
            }
        }
    }

    #@ zoeken dmv %like
    protected function searchStartWith(){
        foreach($this->searchColumns as $table => $columns){
            foreach($columns as $column){
                
                $query = "SELECT * FROM ".$table." WHERE ".$column." LIKE '%".$this->searchWords."' LIMIT ".$this->searchAmount;
                print $query.'<br />';
                $query = mysql_query($query);
                
                 while($result = mysql_fetch_assoc($query)){
                     $this->aResults[] = $result;
                 }
            }
        }
    }

    #@ zoeken dmv like%.
    protected function searchEndWith(){
        foreach($this->searchColumns as $table => $columns){        
            foreach($columns as $column){
                
                $query = "SELECT * FROM ".$table." WHERE ".$column." LIKE '".$this->searchWords."%' LIMIT ".$this->searchAmount;
                print $query.'<br />';
                $query = mysql_query($query);
                
                 while($result = mysql_fetch_assoc($query)){
                     $this->aResults[] = $result;
                 }
            }
        }
    }


    #@ Bepaal zoek methode
    protected function getSearchType($type){
        switch($type){
            case
'rel': return $type;break;
            case
'starts': return $type;break;
            case
'ends': return $type;break;
            case
'like': return $type;break;
            case
'gamble': return $type; break;
            default:
return 'rel';break;
        }
    }

    
    # Zet de zoek resultaten in een tabelletje.
    protected function tableFormat(){
        $content .= '<table cellspacing="0" cellpadding="0" border="0" width="100%">';
        foreach($this->aResults as $result){
            $content.='<tr><td>'."\n";
            $i++;
            foreach($result as $key => $var){
                $content.= $key .' => '. $var.'<br />'."\n";
            }

            $content.='</td></tr>'."\n";
            if($i==10){
                break;
            }
        }

        $content.= "</table>";
        return $content;
    }


    # Highlighten van de zoekwoorden.
    protected function HighLight($text,$string){
        return preg_replace("/(?!<!--)(?!<)(^|[\s\.,>])($string)($|[\s,\.])(?!>)(?!-->)/i",
"\\1<span class=\"highlight\">\\2</span>\\3", $text);
    }
}



# googlelize your name :)
function Googlelize($var){
    $var = strtoupper($var);
    if(strlen($var) >= 6){
        for($i=0;$i<(strlen($var)-3);$i++){
            $digit = substr($var,$i,1);
            switch($i){
                case
0: $color = "#0000FF";break;
                case
1: $color = "#FF0000";break;
                default:
$color = "#FFCC00";break;
                
            }

            
            $str .= "<span style='color:" . $color . "'>" . $digit . "</span>";    
        }
    
        $str .= "<span style='color:#0000FF'>" . substr($var,-3,1) . "</span>";
        $str .= "<span style='color:#00FF00'>" . substr($var,-2,1) . "</span>";
        $str .= "<span style='color:#FF0000'>" . substr($var,-1,1) . "</span>";
        #$str = "<strong>" . $str . "</strong>";
    }
    return $str;
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.