PHP 5.5 เชื่อมฐานข้อมูล SQL Server ทำหน้าค้นหาแบบจำกันจำนวนแถว
เวลาใส่คำค้นหา แสดงผลออกมาไม่ตรง ชาวยแนะนำทีครับ มือใหม่ หัดทำ
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css" />
<style type="text/css">
body {
margin-left: 20px;
margin-top: 20px;
}
h1 {
font-size: 2.5px;
}
h2 {
font-size: 2px;
}
h3 {
font-size: 1.75px;
}
h4 {
font-size: 1.5px;
}
h5 {
font-size: 1.25px;
}
h6 {
font-size: 1px;
}
</style>
<title>ThaiCreate.Com PHP & SQL Server (sqlsrv)</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>
<script src="js/jquery-3.5.1.slim.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$strKeyword = null;
if(isset($_POST["txtKeyword"]))
{
$strKeyword = $_POST["txtKeyword"];
}
?>
<form name="frmSearch" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>ป้อนคำที่ต้องหารค้นหา
<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $strKeyword;?>">
<input type="submit" value="ค้นหา"></th>
</tr>
</table>
</form>
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "sa";
$userPassword = 'qaz1wsx2';
$dbName = "CLCRM2";
$connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
//$sql = "SELECT * FROM Shops";
$sql = "SELECT * FROM Shops WHERE Name LIKE '%".$strKeyword."%' ";
$query = sqlsrv_query($conn, $sql);
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query = sqlsrv_query( $conn, $sql , $params, $options );
$num_rows = sqlsrv_num_rows($query);
$per_page = 20; // 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 = " SELECT c.* FROM (
SELECT ROW_NUMBER() OVER(ORDER BY Code) AS RowID,* FROM Shops
) AS c
WHERE c.RowID > $row_start AND c.RowID <= $row_end
";
$query = sqlsrv_query( $conn, $sql );
?>
<table width="900" border="1">
<tr>
<th width="125"> <div align="center">Code </div></th>
<th width="531"> <div align="center">Name </div></th>
<th width="222"> <div align="center">Phone </div></th>
</tr>
<?php
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
?>
<tr>
<td><div align="center"><?php echo $result["Code"];?></div></td>
<td><?php echo $result["Name"];?></td>
<td><?php echo $result["Phone"];?></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> ";
}
sqlsrv_close($conn);
?>
</body>
</html>
Tag : PHP, Ms SQL Server 2008, Google Chrome, XAMPP
ประวัติการแก้ไข 2021-07-11 22:15:48
Date :
2021-07-11 22:11:48
By :
vegasza
View :
501
Reply :
1
ทำได้แล้วครับ มั่วๆเอา
$sql = " SELECT c.* FROM (SELECT ROW_NUMBER() OVER(ORDER BY Code) AS RowID,* FROM Shops WHERE Name LIKE '%".$strKeyword."%' ) AS c WHERE c.RowID > $row_start AND c.RowID <= $row_end
Date :
2021-07-12 01:22:58
By :
vegasza
Load balance : Server 02