Please help me on paging display (>4.000 pages) in php?
I have a code to fetch data, but my data is too many to> 4.00 pages.
Please help me on paging display.
Thank you!
My Code php:
Quote: <html>
<head>
<title>Trâm Anh Danh sách nhân viên</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="0">
<tr>
<th>Nhập tên nhân viên:
<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $strKeyword;?>">
<input type="submit" value="Tìm kiếm"></th>
</tr>
</table>
</form>
<?php
$serverName = "QGSLUOU6NK9BNBY\SQLEXPRESS";
$userName = "sa";
$userPassword = "12345";
$dbName = "PHP_TEST";
$connectionInfo = array("Database"=>$dbName,"characterSet"=>"UTF-8", "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$stmt = "SELECT Studen_Id,Name,ClassName FROM Studen
INNER JOIN Class
ON Studen.Class_Id=Class.Class_Id
WHERE Name LIKE '%".$strKeyword."%' ";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query = sqlsrv_query( $conn, $stmt , $params, $options );
$num_rows = sqlsrv_num_rows($query);
$per_page = 25; // 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;
}
$stmt = " SELECT Name,Studen_ID
FROM(
SELECT ROW_NUMBER() OVER(ORDER BY Name) AS RowID,Name,Studen_ID
FROM Studen WHERE Name LIKE '%".$strKeyword."%'
) AS tmp
WHERE tmp.RowID > $row_start AND tmp.RowID <= $row_end
";
$query = sqlsrv_query( $conn, $stmt );
if( $query === false ) {
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
echo "code: ".$error[ 'code']."<br />";
echo "message: ".$error[ 'message']."<br />";
}
}
}
?>
<table width="600" border="1">
<tr>
<th width="50"> <div align="center">Student ID </div></th>
<th width="98"> <div align="center">Name Student </div></th>
<th width="98"> <div align="center">Class Name </div></th>
</tr>
<?php
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
?>
<tr>
<td><div align="center"><?php echo $result["Studen_ID"];?></div></td>
<td><?php echo $result["Name"];?></td>
</tr>
<?php
}
?>
</table>
<br>
Tổng số: <?php echo $num_rows;?> nhân viên : <?php echo $num_pages;?> trang :
<?php
if($prev_page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$prev_page&txtKeyword=$strKeyword'><< Trang trước</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'>Trang kế tiếp>></a> ";
}
sqlsrv_close($conn);
?>
</body>
</html>
Tag : PHP
ประวัติการแก้ไข 2019-09-10 15:25:18
Date :
2019-09-10 15:23:23
By :
rainkv
View :
620
Reply :
3
Try this *Show 8 pages at a time*
Code (PHP)
<div class="pagination" align="center">
<?php
if($Prev_Page)
{
echo " <a class='' href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><<</a> ";
}
$intRankPage = 8;
$LastShowPage = $Page + $intRankPage;
if($LastShowPage > $Num_Pages)
{
$LastShowPage = $Num_Pages;
}
$FirstShowPage = $Page - $intRankPage;
if($FirstShowPage < 1)
{
$FirstShowPage = 1;
}
for($i=$FirstShowPage; $i<=$LastShowPage; $i++){
if($i != $Page)
{
echo "<a class='' href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ";
}
else
{
echo "<b class='active12'> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a class='' href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>>></a> ";
}
?>
</div>
Date :
2019-09-10 15:42:03
By :
samada
Load balance : Server 02