Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > PHP > PHP MySQL สอน PHP กับ MySQL > PHP MySQL List Record Paging/Pagination



Clound SSD Virtual Server

PHP MySQL List Record Paging/Pagination

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

ตัวอย่าง

phpMySQLListRecordPaging.php

<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?php
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer ";
$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>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <tr>
    <td><div align="center"><?php echo $objResult["CustomerID"];?></div></td>
    <td><?php echo $objResult["Name"];?></td>
    <td><?php echo $objResult["Email"];?></td>
    <td><div align="center"><?php echo $objResult["CountryCode"];?></div></td>
    <td align="right"><?php echo $objResult["Budget"];?></td>
    <td align="right"><?php echo $objResult["Used"];?></td>
  </tr>
<?php
}
?>
</table>

<br>
Total <?php echo $Num_Rows;?> Record : <?php echo $Num_Pages;?> Page :
<?php
if($Prev_Page)
{
	echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
}

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

Screenshot

PHP & SQL Server








Note! สำหรับ function ของ mysql
เนื่องจาก function ของ mysql จะถูก deprecated ใน version ใหม่ ๆ ฉะนั้นแนะนำให้เปลี่ยนไปใช้ function ของ mysqli
อ่านเพิ่มเติม : PHP MySQL Database (mysqli)


บทความที่แนะนำเพิ่มเติม

Go to : PHP MySQL Pagination ทำแบ่งหน้าด้วย PHP กับ MySQL ย่อจำนวนหน้า ในกรณีที่มีจำนวนหน้าเยอะ ๆ
Go to : คือผมต้องการแบ่งหน้าเว็บครับผมมีข้อมูล อยู่หลายข้อมูล แต่ผมต้องการที่จะโชว์หน้าละ 10 ข้อมูล ผมใช้ตัวแปร $count++ ในการเรียงข้อมูล แต่ผมมีปัญหาตรงที่ว่าคลิกไปหน้าที่สอง
Go to : ถ้าผมต้องการทำลิงค์หน้าใน listbox ควรเขียนอย่างไรดีคับ เวลาเลือกหน้าใน listbox ต้องกดปุ่ม summit เช่นเลือกหน้า 5 แล้วกดปุ่ม summit ก็จะลิงค์ไปหน้าที่ 5


Reference : https://www.thaicreate.com/php-manual/ref.mysql.html


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-08-23 23:44:47 / 2017-03-14 15:48:28
  Download : Download  PHP MySQL List Record Paging/Pagination
 Sponsored Links / Related

 
PHP MySQL Connect to Database
Rating :

 
PHP MySQL List Table Properties
Rating :

 
PHP MySQL List Record
Rating :

 
PHP MySQL Random Record
Rating :

 
PHP MySQL Search Record
Rating :

 
PHP MySQL Search Record Paging/Pagination
Rating :

 
PHP MySQL Multiple Column
Rating :

 
PHP MySQL Multiple Column and Paging/Pagination
Rating :

 
PHP MySQL Add/Insert Record
Rating :

 
PHP MySQL Add/Insert Multiple Record
Rating :

 
PHP MySQL Check Already Exists Add/Insert Record
Rating :

 
PHP MySQL Edit/Update Data Record
Rating :

 
PHP MySQL Multiple Rows Edit/Update Record
Rating :

 
PHP MySQL Delete Record/Confirm Delete
Rating :

 
PHP MySQL Multiple Checkbox Delete Record
Rating :

 
PHP MySQL Add/Insert/Edit/Delete to MySQL On Same Form
Rating :

 
PHP MySQL Transaction (Begin,Commit,RollBack)
Rating :

 
PHP MySQL Used Require/Include
Rating :

 
PHP MySQL Used Function Database Query
Rating :

 
PHP MySQL Database Class
Rating :

 
PHP MySQL (PHP กับ MySQL ภาษาไทย Thai Support)
Rating :

 
PHP MySQL : Upload file to MySQL
Rating :

 
PHP MySQL : Upload PDF Files to MySQL
Rating :

 
PHP MySQL Multiple Upload File to MySQL Database - CreateElement Input File Upload
Rating :

 
PHP MySQL DropDownList / Listmenu / ListBox
Rating :

 
PHP MySQL : Row No and Line Number
Rating :

 
PHP MySQL Upload File to MySQL Database - Multiple Dynamic CreateElement Input File Upload
Rating :

 
PHP MySQL : Move Option List/ListMenu
Rating :

 
PHP MySQL : Dependent List Menu
Rating :

 
PHP MySQL : Counter
Rating :

 
PHP MySQL : Sort Data
Rating :

 
PHP MySQL : Login and Registration
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่