请指导我如何在每个页面中继续进行结果编号,直到最后一个数据。目前在第一页中发生的编号为1到10,但是一旦在下一页上移动,则该编号从1到10再次重复,应该是11到20。
我收到了这段代码并且只是对其进行了自定义Paginate result set having written the query with prepared statements,
<?php
// Script and tutorial written by Adam Khoury @ developphp.com
// Line by line explanation : youtube.com/watch?v=T2QFNu_mivw
$myConnection = new mysqli ($servername, $username, $password, $dbname);
// This first query is just to get the total count of rows
$stmt=$myConnection->prepare('SELECT COUNT(id) FROM questions');
// Don't use bind_result()...
// execute your statement
$stmt->execute();
// Get result set into a MySQLi result resource
$result = $stmt->bind_result($id);
// array to hold all rows
$rows = array();
// All results bound to output vars
while ($stmt->fetch()) {
// Append an array containing your result vars onto the rowset array
$rows[] = array(
'id' => $id
);
}
$rows=$id;
// This is the number of results we want displayed per page
$page_rows = 10;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$dynamicList = "";
$stmt = $myConnection->prepare('SELECT id, question, ans1, ans2, ans3, ans4 FROM questions ORDER BY id LIMIT ?,? ');
$begin= ($pagenum - 1) * $page_rows;
$end= $page_rows;
$stmt->bind_param('ii',$begin,$end);
$stmt->execute();
/* store result */
$stmt->store_result();
/* get the row count */
$count = $stmt->num_rows;
if ($count >= 1) {
$stmt->bind_result($id, $question, $ans1, $ans2, $ans3, $ans4);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Questions (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
}
}
$number = 0;
while ($stmt->fetch()) {
"$id, $question, $ans1, $ans2, $ans3, $ans4";
$number = $number+1;
$dynamicList .= "
<div class='questions'>
<span class='numbering'>$number</span>
<span class='question' id='$id'>$question</span></br>
<input type='radio' name='choice1' value='$ans1' /></br>
<input type='radio' name='choice2' value='$ans2' /></br>
<input type='radio' name='choice3' value='$ans3' /></br>
<input type='radio' name='choice4' value='$ans4' />
</div>
";
}
}
// Close your database connection
mysqli_close($myConnection);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
div#pagination_controls{font-size:21px;}
div#pagination_controls > a{ color:#06F; }
div#pagination_controls > a:visited{ color:#06F; }
</style>
</head>
<body>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $dynamicList; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>
答案 0 :(得分:0)
对于你的编号,你可能想玩以下来看看它是如何运作的。
$pn=isset( $_GET['pn'] ) ? $_GET['pn'] : 0;
$startNumber=1;
$endNumber=$page_rows;
$i=0;
while ( $stmt->fetch() ) {
if( $pn > 0 ) {
if( $i % $page_rows == 0 ) {
$startNumber=( $pn * $page_rows + 1 );
$endNumber=( ( $startNumber + $page_rows ) - 1 );
}
}
$displayedRecordNumber=abs( $startNumber + $i );
$i++;
/* The name of the radio group should be the same for each question */
$dynamicList .= "
<div class='questions'>
<span class='numbering'>".$displayedRecordNumber."</span>
<span class='question' id='".$id."'>$question</span><br />
<input type='radio' name='choice".$i."' value='".$ans1."' /><br />
<input type='radio' name='choice".$i."' value='".$ans2."' /><br />
<input type='radio' name='choice".$i."' value='".$ans3."' /><br />
<input type='radio' name='choice".$i."' value='".$ans4."' />
</div>";
}