EthaiDesign Community  

Go Back   EthaiDesign Community > Developer Forums > PHP Developers

Reply
 
LinkBack Thread Tools
Old 27-01-2009, 10:49 PM   #1
HotDog
 
admin's Avatar
 
เป็นสมาชิกเมื่อ: Jan 2009
ที่อยู่: Bangkok
โพส: 167
Default ค้นหาข้อมูลจาก Table มาแสดงแล้วแบ่งการค้นหาออกเป็นหน้า

ขอบคุณบทความดี ๆ จาก
http://www.thaicreate.com

PHP MySQL Search Record Paging ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อค้นหาข้อมูลจาก Table มาแสดงและมีการแบ่งการแสดงผลเป็นหน้า

ตัวอย่าง

phpMySQLSearchRecordPaging.php

PHP Code:
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
if($_GET["txtKeyword"] != "")
{
$objConnect mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB mysql_select_db("mydatabase");
// Search By Name or Email
$strSQL "SELECT * FROM customer WHERE (Name LIKE '%".$_GET["txtKeyword"]."%' or Email LIKE '%".$_GET["txtKeyword"]."%')";
$objQuery mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows mysql_num_rows($objQuery);


$Per_Page 2// Per Page

$Page $_GET["Page"];
if(!
$_GET["Page"])
{
$Page=1;
}

$Prev_Page $Page-1;
$Next_Page $Page+1;

$Page_Start = (($Per_Page*$Page)-$Per_Page);
if(
$Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if((
$Num_Rows $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}


$strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";
$objQuery mysql_query($strSQL);

?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
</tr>
<?
}
?>
</table>
<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
echo 
" <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";
}

for(
$i=1$i<=$Num_Pages$i++){
if(
$i != $Page)
{
echo 
"[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";
}
else
{
echo 
"<b> $i </b>";
}
}
if(
$Page!=$Num_Pages)
{
echo 
" <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";
}

mysql_close($objConnect);

}
?>
</body>
</html>

จากตัวอย่างโค้ดด้านบนผลการแสดงออกมาจะได้ดังภาพ




ลองเล่นกันดูนะครับ
__________________
Ethaidesign.com
The Communities Graphic & Developer free forums.

admin is offline   Reply With Quote
Old 18-02-2009, 02:11 PM   #2
สมาชิกบอร์ด
 
เป็นสมาชิกเมื่อ: Feb 2009
โพส: 1
Default

รบกวนสอบถามครับ

จากวิธีที่แนะนำมา
ผมมาเปลี่ยนในส่วนช่องสำหรับใส่ keyword เป็น 2 ช่อง
ตามโค้ดนี้

<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">และ<br>
<input name="txtKeyword1" type="text1" id="txtKeyword1" value="<?=$_GET["txtKeyword1"];?>">
<input type="submit" value="Search"></font></th>

</tr>
</table>
</form>
<?
if($_GET["txtKeyword"]["txtKeyword1"] != "")
{
$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
$objDB = mysql_select_db("xxxx");
// Search By Name or Email
$strSQL = "SELECT * FROM ratchakitcha WHERE (title LIKE '%".$_GET["txtKeyword"]."%' or volume LIKE '%".$_GET["txtKeyword"]."%' or part LIKE '%".$_GET["txtKeyword"]."%' or year LIKE '%".$_GET["txtKeyword"]."%')

and (title LIKE '%".$_GET["txtKeyword1"]."%' or volume LIKE '%".$_GET["txtKeyword1"]."%' or part LIKE '%".$_GET["txtKeyword1"]."%' or year LIKE '%".$_GET["txtKeyword1"]."%')Order by title";

$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);

ใช้ and ในการเชื่อม keyword ทั้ง 2 คำแล้วลอง search ดูปรากฎว่า
ได้ผลครับ ผลการสืบค้นต้องมี keyword ทั้ง2 คำปรากฎ
ทำให้ข้อมูลแคบลงครับ

พอกด next เพื่อดูหน้าถัดไป มันกลับแสดงผลเป็นเช่นเดียวกับการค้น
โดยใช้ keyword แรกคำเดียวครับ

ผมจะต้องแก้ยังไงครับ

ปล.ผมอธิบายงงป่ะ
อีเมลผม hunter-xx@windowslive.com
hunterx is offline   Reply With Quote
Reply

คำค้น (Tags)
การดึงข้อมูล mysql, ค้นหา แสดงผลเป็นหน้า, ดึงข้อมูล แสดงผลเป็นหน้า, ดึงข้อมูลจากฐานข้อมูล

Thread Tools

ระดับการโพส
คุณ ไม่สามารถ โพสหัวข้อใหม่ได้
คุณ ไม่สามารถ โพสตอบกลับได้
คุณ ไม่สามารถ โพสไฟล์แนบได้
คุณ ไม่สามารถ แก้ไขโพสของคุณได้

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On

ไปยังฟอรั่ม


การตั้งการเวลาแบบ GMT +7. และเวลาในขณะนี้คือ 12:01 PM.


Forums design by: EthaiDesign.com. Copyright © 2004-2009. All right reserved.
เว็บไซต์ Ethaidesign.Com เป็นเพียงศูนย์กลางการเรียนรู้ของเหล่า Designers และ Programmers เป็นเว็บบอร์ดแลกเปลี่ยนความรู้ทั่วไป
ทางทีมงานไม่สามารถที่จะตรวจสอบเนื้อหาทุกอย่างโดยละเอียดได้ อันเกิดจากการโพสโดยสมาชิก หากพบเนื้อหาไม่เหมาะสมโปรดแจ้งทีมงานด่วน..


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24