Не сте регистриран!
Регистрирайте се БЕЗПЛАТНО, за да използвате услугите на сайта!
Програмиране на собствена търсачка с MySQL и PHP
Автор
Mutatos (14.04.2005 22:30)
съобщение до автора
Погледнат
5898 пъти
добави към любими
Оценка
добави коментар
Гласове
4
изпрати на приятел
Коментари
(2)
абонирай се за PHP
Страница 4 / 4
Пример за тестване
CODE 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
<? PHP
function getmicrotime ()
{
list ( $usec , $sec ) = explode ( " " , microtime ()) ;
return (( float ) $usec + ( float ) $sec ) ;
}
mysql_pconnect ( " xxx " , " xxx " , " xxx " )
or die ( ' Unable to connect to server. ' ) ;
mysql_select_db ( " test " )
or die ( ' Unable to select database. ' ) ;
$RESULTS_LIMIT = 10 ;
if ( isset ( $search_query ))
{
if ( ! isset ( $first_pos ))
{
$first_pos = " 0 " ;
}
$start_search = getmicrotime () ;
$sql_query = mysql_query ( " SELECT * FROM news WHERE MATCH(title,article) AGAINST(' $search_query ') " ) ;
if ( $results = mysql_num_rows ( $sql_query ) != 0 )
{
$sql_result_query = mysql_query ( " SELECT * FROM news WHERE MATCH(title,article) AGAINST(' $search_query ') LIMIT $first_pos , $RESULTS_LIMIT " ) ;
}
else
{
$sql_query = mysql_query ( " SELECT * FROM news WHERE (title LIKE '% " . $search_query . " %' OR article LIKE '% " . $search_query . " %') " ) ;
$results = mysql_num_rows ( $sql_query ) ;
$sql_result_query = mysql_query ( " SELECT * FROM news WHERE (title LIKE '% " . $search_query . " %' OR article LIKE '% " . $search_query . " %') LIMIT $first_pos , $RESULTS_LIMIT " ) ;
}
$stop_search = getmicrotime () ;
$time_search = ( $stop_search - $start_search ) ;
}
?>
<?PHP
if ( $results != 0 )
{
?>
<table border = " 0 " cellspacing = " 2 " cellpadding = " 2 " >
< tr >
< td width = " 47% " >Резултати за <? PHP echo " <i><b><font color=#000000> " . $search_query . " </font></b></i> " ; ?></ td >
< td width = " 53% " align = " right " height = " 22 " >Резултати < b ><? PHP echo ( $first_pos + 1 ) . " - " ; if (( $RESULTS_LIMIT + $first_pos ) < $results ) echo ( $RESULTS_LIMIT + $first_pos ) ; else echo $results ; ?></ b >
от общо < b ><? PHP echo $results ; ?></ b > за ( < b ><? PHP echo sprintf ( " %01.2f " , $time_search ) ; ?></ b > )
секунди </ td >
</ tr >
< tr >
< form action = "" method = " GET " >
< td colspan = " 2 " align = " center " > < input name = " search_query " type = " text " value = " <?PHP echo $search_query ; ?> " size = " 40 " >
< input name = " search_button " type = " submit " value = " Search " > </ td >
</ form >
</ tr >
<? PHP
while ( $row = mysql_fetch_array ( $sql_result_query ))
{
?>
<tr align = " left " >
< td colspan = " 2 " ><? PHP echo $row [ ' title ' ] ; ?></ td >
</ tr >
<? PHP
}
?>
</table >
<?
}
else
{
?>
<table border = " 0 " cellspacing = " 2 " cellpadding = " 0 " >
< tr >
< td align = " center " >Няма намерени резултати за <? PHP echo " <i><b><font color=#000000> " . $search_query . " </font></b></i> " ; ?></ td >
</ tr >
< tr >
< form action = "" method = " GET " >
< td colspan = " 2 " align = " center " >
< input name = " search_query " type = " text " value = " <?PHP echo $search_query ; ?> " >
< input name = " search_button " type = " submit " value = " Search " >
</ td >
</ form >
</ tr >
</ table >
<? PHP
}
?>
<table width = " 300 " border = " 0 " cellspacing = " 0 " cellpadding = " 0 " >
< tr >
< td align = " right " >
<? PHP
if ( $first_pos > 0 )
{
$back = $first_pos - $RESULTS_LIMIT ;
if ( $back < 0 )
{
$back = 0 ;
}
echo " <a href= " search . php ? search_query = " .stripslashes( $search_query ). " & first_pos = $back " ><< </a> " ;
}
if ( $results > $RESULTS_LIMIT )
{
$sites = intval ( $results / $RESULTS_LIMIT ) ;
if ( $results % $RESULTS_LIMIT )
{
$sites ++;
}
}
for ( $i = 1 ; $i <= $sites ; $i ++ )
{
$fwd = ( $i - 1 ) * $RESULTS_LIMIT ;
if ( $fwd == $first_pos )
{
echo " <a href= " search . php ? search_query = " .stripslashes( $search_query ). " & first_pos = $fwd " style='color:#000000'><b> $i </b></a> | " ;
}
else
{
echo " <a href= " search . php ? search_query = " .stripslashes( $search_query ). " & first_pos = $fwd " > $i </a> | " ;
}
}
if ( $first_pos < $results - $RESULTS_LIMIT )
{
$fwd = $first_pos + $RESULTS_LIMIT ;
echo " <a href= " search . php ? search_query = " .stripslashes( $search_query ). " & first_pos = $fwd " > >></a> " ;
$fwd = $results - $RESULTS_LIMIT ;
}
?>
</td >
</ tr >
</ table >
Още уроци от тази рубрика
За автора: Mutatos
Николай Николов се занимава с програмиране на PHP/MySQL повече от 6 години. Заедно с разработката на уеб приложения на PHP, се занимава с Java, XML и Webservices.
1 посетител чете този урок (0 потребители и 1 гост)
Активни потребители:
---