| 
  PHP  MySQL Search Data Paging/Pagination (mysqli) บทความนี้จะเป็นตัวอย่างของ mysqli   และการค้นหา (Search) ข้อมูล และการแบ่งหน้า (Paging) การเขียน PHP เพื่อค้นหาข้อมูลจาก Database ของ MySQL มาแสดงผลทางหน้าจอและการแบ่งหน้าแสดงผลออกเป็นหลาย ๆ หน้า ตามปริมาณข้อมูลที่ค้นพบด้วย function ต่าง ๆ ของ mysqli ทำงานร่วมกับการ fetch ข้อมูลในรูปแบบของ array 
    |  
        PHP  MySQL Search Data Paging/Pagination (mysqli)       |  
 MySQL Table
 
 
CREATE TABLE `customer` (
  `CustomerID` varchar(4) NOT NULL,
  `Name` varchar(50) NOT NULL,
  `Email` varchar(50) NOT NULL,
  `CountryCode` varchar(2) NOT NULL,
  `Budget` double NOT NULL,
  `Used` double NOT NULL,
  PRIMARY KEY  (`CustomerID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO `customer` VALUES ('C002', 'John  Smith', '[email protected]', 'UK', 2000000, 800000);
INSERT INTO `customer` VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);
  
 ฐานข้อมูลและตารางของ MySQL Database
 
 Syntax รูปแบบการใช้งาน
 
 $sql = "SELECT * FROM customer WHERE Name LIKE '%".$strKeyword."%' ";
$sql .= " ORDER BY CustomerID ASC LIMIT $row_start ,$row_end ";
while($result=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
	echo $result["CustomerID"];l
}
 Example ตัวอย่างการเขียน PHP กับ MySQL เพื่อทำการค้นหา (Search) ข้อมูล และแบ่งหน้า (Paging) ข้อมูล ด้วย mysqli
 
 list.php
 
 <html>
<head>
<title>ThaiCreate.Com PHP & MySQL (mysqli)</title>
</head>
<body>
<?php
	ini_set('display_errors', 1);
	error_reporting(~0);
	$strKeyword = null;
	if(isset($_POST["txtKeyword"]))
	{
		$strKeyword = $_POST["txtKeyword"];
	}
	if(isset($_GET["txtKeyword"]))
	{
		$strKeyword = $_GET["txtKeyword"];
	}
?>
<form name="frmSearch" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
  <table width="599" border="1">
    <tr>
      <th>Keyword
      <input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $strKeyword;?>">
      <input type="submit" value="Search"></th>
    </tr>
  </table>
</form>
<?php
   $serverName = "localhost";
   $userName = "root";
   $userPassword = "root";
   $dbName = "mydatabase";
   $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
	$sql = "SELECT * FROM customer WHERE Name LIKE '%".$strKeyword."%' ";
	$query = mysqli_query($conn,$sql);
	$num_rows = mysqli_num_rows($query);
	$per_page = 2;   // Per Page
	$page  = 1;
	
	if(isset($_GET["Page"]))
	{
		$page = $_GET["Page"];
	}
	$prev_page = $page-1;
	$next_page = $page+1;
	$row_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;
	}
	$row_end = $per_page * $page;
	if($row_end > $num_rows)
	{
		$row_end = $num_rows;
	}
	$sql .= " ORDER BY CustomerID ASC LIMIT $row_start ,$row_end ";
	$query = mysqli_query($conn,$sql);
?>
<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($result=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
?>
  <tr>
    <td><div align="center"><?php echo $result["CustomerID"];?></div></td>
    <td><?php echo $result["Name"];?></td>
    <td><?php echo $result["Email"];?></td>
    <td><div align="center"><?php echo $result["CountryCode"];?></div></td>
    <td align="right"><?php echo $result["Budget"];?></td>
    <td align="right"><?php echo $result["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&txtKeyword=$strKeyword'><< Back</a> ";
}
for($i=1; $i<=$num_pages; $i++){
	if($i != $page)
	{
		echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$strKeyword'>$i</a> ]";
	}
	else
	{
		echo "<b> $i </b>";
	}
}
if($page!=$num_pages)
{
	echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$next_page&txtKeyword=$strKeyword'>Next>></a> ";
}
$conn = null;
?>
</body>
</html>
 
 
 Screenshot
 
 
  
 แสดงข้อมูลจาก MySQL ด้วย function ของ mysqli
 
 
  
 กรณีที่มีการค้นหาข้อมูล และแบ่งหน้า
 
 
  
 ในกรณีที่ไปหน้าอื่น ๆ ก็จะต้องแสดงเฉพาะรายการที่ค้นหาเท่านั้น
 
 
 
 
 |