需要使用 PDO 获取 MSSQL table 在 PHP 中一次显示 25 条记录
Need to get a MSSQL table to show 25 records at a time in PHP using PDO
我正在尝试让 MSSQL table 限制显示 25 条记录,然后其余的在底部显示一个数字,表示 1 到它结束的任何数字,并且能够点击每个数字显示 100 条记录。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="favicon.ico">
<title>Completed Request Status</title>
<!-- Bootstrap core CSS -->
<link href="../../css/bootstrap.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="../../css/table.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="../../javascript/html5shiv.min.js"></script>
<script src="../../javascript/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container-fluid">
<div class="main-wrap">
<div class="main-content">
<?php
//conects to the database
require_once("../../db_connect.php");
//prepared statement with PDO to query the database
$stmt = $db->prepare("SELECT * FROM receivingrequests WHERE status='Completed' Limit 100");
$stmt->execute();
?>
<?php //start of the while loop ?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">
<tr>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Request#</strong></th>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Status</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Comments</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Date Requested</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Name</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Department</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>VasLblDate</strong></th>
</tr>
<tr>
<?php $id = $row['RequestNumber'];?>
<?php echo "<td> <a href='../../update.php?id=$id'>$id</a></td>"?>
<td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
<td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VasLblDate']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Work Requested</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases Missing</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Stray Cases Found/To Move</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases To Consume</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>PO #</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>IS #
</strong> </th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Vendor Name
</strong> </th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Effortrequest']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Missing']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Located']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Dissassociated']; ?>
</strong></td>
<td class="style2"><strong><?php echo $row['PONumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['IBS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Vendor']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Case 1</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 2</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 3</strong></th>
<th style="background-color: #F4F4F4;"class="style2"></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Description</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Comments</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Carrier</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['CaseOne']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseTwo']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseThree']; ?></strong></td>
<td class="style2"><strong><?php echo $row['']; ?></strong></td>
<td class="style2"><strong><?php echo $row['ReqDescription']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Carrier']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Trip</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Update by</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Updated</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Actual Cases Consumes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>EE Performing Consume Cases</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Store Number</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Reason</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Trip']; ?></strong></td>
<td class="style2"><strong><?php echo $row['RemarksBy']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CompDT']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseCount']; ?></strong></td>
<td class="style2"><strong><?php echo $row['WHODIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['StoreNumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Reason']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>New IS#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>New Carrier Shipment#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Deleted Case#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Notes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Inbound User ID</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Was Shipment</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Verified BY</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['NewIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['NewCSN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DCN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['AreaID']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Verified']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VerifiedID']; ?></strong></td>
</tr>
</table>
<?php } //end of the while loop?>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../../javascript/jquery.js"></script>
<script src="../../javascript/bootstrap.js"></script>
</body>
</html>
使用 LIMIT
函数将您的查询限制为一个子集。来自 MySQL 文档 (https://dev.mysql.com/doc/refman/5.1/en/select.html):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
您需要做的就是确定偏移量和分页大小:
$num_per_page = 25;
// Get this from the user (and default to 1 as the first page)
$page = (int)$_GET['page'] ? (int)$_GET['page'] : 1;
$stmt = $db->prepare("SELECT * FROM receivingrequests WHERE status='Completed' Limit :offset,:limit");
$stmt->execute(array(':offset' => int($num_per_page * ($page-1)), ':limit' => $num_per_page));
使用 $_get(或 $_post)获取当前页面并限制查询
例如限制 (($id-1)*25,($id-1)*25+25)
$id=1 会显示 1,25
我正在尝试让 MSSQL table 限制显示 25 条记录,然后其余的在底部显示一个数字,表示 1 到它结束的任何数字,并且能够点击每个数字显示 100 条记录。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="favicon.ico">
<title>Completed Request Status</title>
<!-- Bootstrap core CSS -->
<link href="../../css/bootstrap.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="../../css/table.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="../../javascript/html5shiv.min.js"></script>
<script src="../../javascript/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container-fluid">
<div class="main-wrap">
<div class="main-content">
<?php
//conects to the database
require_once("../../db_connect.php");
//prepared statement with PDO to query the database
$stmt = $db->prepare("SELECT * FROM receivingrequests WHERE status='Completed' Limit 100");
$stmt->execute();
?>
<?php //start of the while loop ?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">
<tr>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Request#</strong></th>
<th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
<strong>Status</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Comments</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Date Requested</strong></th>
<th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Name</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>Department</strong></th>
<th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
<strong>VasLblDate</strong></th>
</tr>
<tr>
<?php $id = $row['RequestNumber'];?>
<?php echo "<td> <a href='../../update.php?id=$id'>$id</a></td>"?>
<td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
<td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VasLblDate']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Work Requested</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases Missing</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Stray Cases Found/To Move</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Cases To Consume</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>PO #</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>IS #
</strong> </th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Vendor Name
</strong> </th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Effortrequest']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Missing']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Located']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Dissassociated']; ?>
</strong></td>
<td class="style2"><strong><?php echo $row['PONumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['IBS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Vendor']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Case 1</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 2</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Case 3</strong></th>
<th style="background-color: #F4F4F4;"class="style2"></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Description</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Comments</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Carrier</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['CaseOne']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseTwo']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseThree']; ?></strong></td>
<td class="style2"><strong><?php echo $row['']; ?></strong></td>
<td class="style2"><strong><?php echo $row['ReqDescription']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Carrier']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>Trip</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Update by</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>DC Remarks Updated</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong># Of Actual Cases Consumes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>EE Performing Consume Cases</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Store Number</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Reason</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['Trip']; ?></strong></td>
<td class="style2"><strong><?php echo $row['RemarksBy']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CompDT']; ?></strong></td>
<td class="style2"><strong><?php echo $row['CaseCount']; ?></strong></td>
<td class="style2"><strong><?php echo $row['WHODIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['StoreNumber']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Reason']; ?></strong></td>
</tr>
<tr>
<th style="background-color: #F4F4F4; width: 62px;"class="style3"><strong>New IS#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>New Carrier Shipment#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Deleted Case#</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Notes</strong></th>
<th style="background-color: #F4F4F4;"class="style3"><strong>Inbound User ID</strong></th>
<th style="background-color: #F4F4F4;"class="style3" class="style2">
<strong>Was Shipment</strong></th>
<th style="background-color: #F4F4F4; width:60px; height: 25px;" class="style3">
<strong>Verified BY</strong></th>
</tr>
<tr>
<td class="style2" style="width: 62px"><strong><?php echo $row['NewIS']; ?></strong></td>
<td class="style2"><strong><?php echo $row['NewCSN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['DCN']; ?></strong></td>
<td class="style2"><strong><?php echo $row['MoreComments']; ?></strong></td>
<td class="style2"><strong><?php echo $row['AreaID']; ?></strong></td>
<td class="style2"><strong><?php echo $row['Verified']; ?></strong></td>
<td class="style2"><strong><?php echo $row['VerifiedID']; ?></strong></td>
</tr>
</table>
<?php } //end of the while loop?>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../../javascript/jquery.js"></script>
<script src="../../javascript/bootstrap.js"></script>
</body>
</html>
使用 LIMIT
函数将您的查询限制为一个子集。来自 MySQL 文档 (https://dev.mysql.com/doc/refman/5.1/en/select.html):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
您需要做的就是确定偏移量和分页大小:
$num_per_page = 25;
// Get this from the user (and default to 1 as the first page)
$page = (int)$_GET['page'] ? (int)$_GET['page'] : 1;
$stmt = $db->prepare("SELECT * FROM receivingrequests WHERE status='Completed' Limit :offset,:limit");
$stmt->execute(array(':offset' => int($num_per_page * ($page-1)), ':limit' => $num_per_page));
使用 $_get(或 $_post)获取当前页面并限制查询 例如限制 (($id-1)*25,($id-1)*25+25) $id=1 会显示 1,25