001.
<html>
002.
<head>
003.
<title>ThaiCreate.Com PHP & MySQL (mysqli)</title>
004.
</head>
005.
<body>
006.
<?php
007.
ini_set
(
'display_errors'
, 1);
008.
error_reporting
(~0);
009.
010.
$strKeyword
= null;
011.
012.
if
(isset(
$_POST
[
"txtKeyword"
]))
013.
{
014.
$strKeyword
=
$_POST
[
"txtKeyword"
];
015.
}
016.
if
(isset(
$_GET
[
"txtKeyword"
]))
017.
{
018.
$strKeyword
=
$_GET
[
"txtKeyword"
];
019.
}
020.
?>
021.
<form name=
"frmSearch"
method=
"post"
action=
"<?php echo $_SERVER['SCRIPT_NAME'];?>"
>
022.
<table width=
"599"
border=
"1"
>
023.
<tr>
024.
<th>Keyword
025.
<input name=
"txtKeyword"
type=
"text"
id=
"txtKeyword"
value=
"<?php echo $strKeyword;?>"
>
026.
<input type=
"submit"
value=
"Search"
></th>
027.
</tr>
028.
</table>
029.
</form>
030.
<?php
031.
$serverName
=
"localhost"
;
032.
$userName
=
"root"
;
033.
$userPassword
=
""
;
034.
$dbName
=
"supap_php"
;
035.
036.
$conn
= mysqli_connect(
$serverName
,
$userName
,
$userPassword
,
$dbName
);
037.
038.
$sql
= "SELECT T1.id,title, T1.created,num_views,num_comments,last_commented FROM chaiya_question AS T1 INNER JOIN chaiya_answer AS T2 ON T1.id = T2.topic_id
039.
WHERE (T1.title LIKE
'%".$strKeyword."%'
OR T1.description LIKE
'%".$strKeyword."%'
OR T2.description LIKE
'%".$strKeyword."%'
) GROUP BY T1.id ";
040.
041.
$query
= mysqli_query(
$conn
,
$sql
);
042.
043.
$num_rows
= mysqli_num_rows(
$query
);
044.
045.
$per_page
= 50;
046.
$page
= 1;
047.
048.
if
(isset(
$_GET
[
"Page"
]))
049.
{
050.
$page
=
$_GET
[
"Page"
];
051.
}
052.
053.
$prev_page
=
$page
-1;
054.
$next_page
=
$page
+1;
055.
056.
$row_start
= ((
$per_page
*
$page
)-
$per_page
);
057.
if
(
$num_rows
<=
$per_page
)
058.
{
059.
$num_pages
=1;
060.
}
061.
else
if
((
$num_rows
%
$per_page
)==0)
062.
{
063.
$num_pages
=(
$num_rows
/
$per_page
) ;
064.
}
065.
else
066.
{
067.
$num_pages
=(
$num_rows
/
$per_page
)+1;
068.
$num_pages
= (int)
$num_pages
;
069.
}
070.
$row_end
=
$per_page
*
$page
;
071.
if
(
$row_end
>
$num_rows
)
072.
{
073.
$row_end
=
$num_rows
;
074.
}
075.
076.
$sql
.=
" ORDER BY T1.last_commented DESC LIMIT $row_start ,$per_page "
;
077.
$query
= mysqli_query(
$conn
,
$sql
);
078.
079.
?>
080.
<table width=
"1200"
border=
"1"
>
081.
<tr>
082.
<th width=
"91"
> <div align=
"center"
>หมายเลข </div></th>
083.
<th width=
"600"
> <div align=
"center"
>หัวข้อ </div></th>
084.
<th width=
"190"
> <div align=
"center"
>โพสต์เมื่อ </div></th>
085.
<th width=
"50"
> <div align=
"center"
>ดู </div></th>
086.
<th width=
"50"
> <div align=
"center"
>ตอบ </div></th>
087.
<th width=
"190"
> <div align=
"center"
>โพสต์ล่าสุด </div></th>
088.
</tr>
089.
<?php
090.
while
(
$result
=mysqli_fetch_array(
$query
,MYSQLI_ASSOC))
091.
{
092.
?>
093.
<tr>
094.
<td><div align=
"center"
><?php
echo
$result
[
"id"
];;?></div></td>
095.
<td><?php
echo
$result
[
"title"
];?></td>
096.
<td><?php
echo
$result
[
"created"
];?></td>
097.
<td><div align=
"center"
><?php
echo
$result
[
"num_views"
];?></div></td>
098.
<td align=
"center"
><?php
echo
$result
[
"num_comments"
];?></td>
099.
<td align=
"right"
><?php
echo
$result
[
"last_commented"
];?></td>
100.
</tr>
101.
<?php
102.
}
103.
?>
104.
</table>
105.
<br>
106.
Total <?php
echo
$num_rows
;?> Record : <?php
echo
$num_pages
;?> Page :
107.
<?php
108.
if
(
$prev_page
)
109.
{
110.
echo
" <a href='$_SERVER[SCRIPT_NAME]?Page=$prev_page&txtKeyword=$strKeyword'><< Back</a> "
;
111.
}
112.
113.
for
(
$i
=1;
$i
<=
$num_pages
;
$i
++){
114.
if
(
$i
!=
$page
)
115.
{
116.
echo
"[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$strKeyword'>$i</a> ]"
;
117.
}
118.
else
119.
{
120.
echo
"<b> $i </b>"
;
121.
}
122.
}
123.
if
(
$page
!=
$num_pages
)
124.
{
125.
echo
" <a href ='$_SERVER[SCRIPT_NAME]?Page=$next_page&txtKeyword=$strKeyword'>Next>></a> "
;
126.
}
127.
$conn
= null;
128.
?>
129.
</body>
130.
</html>