ZT Maju - шаблон joomla Продвижение

การเขียน PHP MySQL List Record Paging/Pagination

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

ตัวอย่าง

phpMySQLListRecordPaging.php
 

01.<html>
02.<head>
03.<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
04.</head>
05.<body>
06.<?
07.$objConnect = mysql_connect("localhost","root","root"or die("Error Connect to Database");
08.$objDB = mysql_select_db("mydatabase");
09.$strSQL "SELECT * FROM customer ";
10.$objQuery = mysql_query($strSQLor die ("Error Query [".$strSQL."]");
11.$Num_Rows = mysql_num_rows($objQuery);
12.
13.$Per_Page = 2;   // Per Page
14.
15.$Page $_GET["Page"];
16.if(!$_GET["Page"])
17.{
18.$Page=1;
19.}
20.
21.$Prev_Page $Page-1;
22.$Next_Page $Page+1;
23.
24.$Page_Start = (($Per_Page*$Page)-$Per_Page);
25.if($Num_Rows<=$Per_Page)
26.{
27.$Num_Pages =1;
28.}
29.else if(($Num_Rows $Per_Page)==0)
30.{
31.$Num_Pages =($Num_Rows/$Per_Page) ;
32.}
33.else
34.{
35.$Num_Pages =($Num_Rows/$Per_Page)+1;
36.$Num_Pages = (int)$Num_Pages;
37.}
38.
39.$strSQL .=" order  by CustomerID ASC LIMIT $Page_Start , $Per_Page";
40.$objQuery  = mysql_query($strSQL);
41.?>
42.<table width="600" border="1">
43.<tr>
44.<th width="91"> <div align="center">CustomerID </div></th>
45.<th width="98"> <div align="center">Name </div></th>
46.<th width="198"> <div align="center">Email </div></th>
47.<th width="97"> <div align="center">CountryCode </div></th>
48.<th width="59"> <div align="center">Budget </div></th>
49.<th width="71"> <div align="center">Used </div></th>
50.</tr>
51.<?
52.while($objResult = mysql_fetch_array($objQuery))
53.{
54.?>
55.<tr>
56.<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
57.<td><?=$objResult["Name"];?></td>
58.<td><?=$objResult["Email"];?></td>
59.<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
60.<td align="right"><?=$objResult["Budget"];?></td>
61.<td align="right"><?=$objResult["Used"];?></td>
62.</tr>
63.<?
64.}
65.?>
66.</table>
67.
68.<br>
69.Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
70.<?
71.if($Prev_Page)
72.{
73.echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
74.}
75.
76.for($i=1; $i<=$Num_Pages$i++){
77.if($i != $Page)
78.{
79.echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
80.}
81.else
82.{
83.echo "<b> $i </b>";
84.}
85.}
86.if($Page!=$Num_Pages)
87.{
88.echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
89.}
90.mysql_close($objConnect);
91.?>
92.</body>
93.</html>


Screenshot

php mysql list record paging

Reference : http://www.thaicreate.com

Top of Page