我有一个分页脚本,它运行正常。我想为我的分页添加一个范围。 BUt它应该采用以下格式;
<< < 1 | 2 | 3 | 4 | 5> >>
当我点击“5”时,它应该是;
<< < 4 | 5 | 6 | 7 | 8> >>
同样地,当点击“8”时,它应该是;
<< < 7 | 8 | 9 | 10 | 11> >>
假设“11”是最后一页,如果我们点击11,它应该是;
<< < 7 | 8 | 9 | 10 | 11> >>
我的代码看起来像这样;
$recordLimit = 4;
$totalNumberofRecords = 100;
$currentpage = !empty( $startPage ) ? (int) $startPage : 1 ;
$startFrom = ( $currentpage -1 ) * $recordLimit;
$limit = $startFrom.','.$recordLimit;
$totalPages = ceil( $totalNumberOfRecords / $recordLimit );
$j = 0;
for ($i = max(1, $currentpage - 1); $i<= min($currentpage + 4, $totalPages); $i++) {
$fairLists['pages'][$j] = $i;
if ( $startPage == $fairLists['pages'][$j] || ( $j == 0 && empty( $startPage ) )
{
$fairLists['pages'][$j] = array( $i ,"font-weight:bold;");
}else{
$fairLists['pages'][$j] = array( $i , 'font-weight:normal;' );
}
$j++;
}
我想将范围限制为5,当我们达到范围时,它应显示1个先前记录和3个下一个记录;
如果有人知道剧本,请帮助我,
谢谢,
阿伦
答案 0 :(得分:0)
我的问题已经解决了..我的代码如下,但是在使用此代码后,如果它适用于你,那么请点击此评论作为答案..... 代码是
<php
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$recordsPerPage = 10;
$fromRecordNum = ($recordsPerPage * $page) - $recordsPerPage;
$query = "SELECT * FROM
table_mstr_department
ORDER BY DeptId
LIMIT
{$fromRecordNum}, {$recordsPerPage}";
$stmt = $con->prepare( $query );
$stmt->execute();
$num = $stmt->rowCount();
echo "<div id='search_area'>";
echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
//********Add New Department Button
echo "<td height='auto' width='auto'>";
echo "<input type='submit' name='newemp' value='Add New Department'>";
echo "</td>";
echo "<td width='auto'></td>";
//******Search Button
echo "<td width='auto'>";
echo "<div id='search_options'>";
echo "<input type='search' name='search_text' width='auto' style='height:20px;'/>";
echo "<input type='button' name='search_btn' value='Search' />";
echo "</div>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td height='auto' width='auto'>";
echo "</td>";
echo "<td width='auto'></td>";
echo "<td width='auto'>";
echo "<div id='search_options'>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
if($num>0)
{
echo '<table width="100%" id="dep_table" style="margin-top:10px;" cellspacing="1" cellpadding="2" border="0">';
echo '<tr bgcolor="#4682B4">';
echo '<th>Editor</th>';
echo '<th>Department Id</th>';
echo '<th>Department Name</th>';
echo '</tr>';
$i=0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$i++;
if($i % 2 == 0)
{
$bgcolor= "#6AA2C3";
}
else
{
$bgcolor= "#A2B5CD";
}
//extract row, this will make $row['firstname'] to just $firstname only
extract($row);
//creating new table row per record
echo "<tr bgcolor='$bgcolor' id='$DeptId' name='edit_tr'>";
echo '<td id="edit"><input id="edit" type="radio" name="deptid" value="DeptId" ?></td>';
echo "<td class='format'>{$row['DeptId']}</td>";
echo "<td class='format'>{$row['DeptName']}</td>";
echo "</tr>";
}
echo "</table>";
}
echo "</div>";
// <强> * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** 强>
echo "<div id='nav_position'>";
if($page>1)
{
// ********** show the first page
echo "<a href='" . $_SERVER['PHP_SELF'] . "' title='Go to the first page.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> << </span>";
echo "</a>";
// ********** show the previous page
$prev_page = $page - 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$prev_page}' title='Previous page is {$prev_page}.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> < </span>";
echo "</a>";
}
// ********** show the number paging
// find out total pages
$query = "SELECT COUNT(*) as total_rows FROM table_mstr_department";
$stmt = $con->prepare( $query );
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_rows = $row['total_rows'];
$total_pages = ceil($total_rows / $recordsPerPage);
// range of num links to show
$range = 2;
// display links to 'range of pages' around 'current page'
$initial_num = $page - $range;
$condition_limit_num = ($page + $range) + 1;
for ($x=$initial_num; $x<$condition_limit_num; $x++)
{
// be sure '$x is greater than 0' AND 'less than or equal to the $total_pages'
if (($x > 0) && ($x <= $total_pages))
{
// current page
if ($x == $page)
{
echo "<span class='customBtn' style='background:#0D6598;'>$x</span>";
}
// not current page
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?page=$x' class='customBtn'>$x</a> ";
}
}
}
// ***** for 'next' and 'last' pages
if($page<$total_pages)
{
// ********** show the next page
$next_page = $page + 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$next_page}' title='Next page is {$next_page}.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> > </span>";
echo "</a>";
// ********** show the last page
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$total_pages}' title='Last page is {$total_pages}.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> >> </span>";
echo "</a>";
}
/*
echo "<input type='text' name='page' size='1' />";
echo "<input type='submit' name='Go_btn' value='Go' class='customBtn' />";<?php */
echo "</div>";
?>
您可以相应地修改此代码。此代码显示三列...... 祝您好运.....并且不要忘记将此视为asnwer
答案 1 :(得分:0)
echo "<div id='nav_position'>";
if($page>1)
{
// ********** show the first page
echo "<a href='" . $_SERVER['PHP_SELF'] . "' title='Go to the first page.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> << </span>";
echo "</a>";
// ********** show the previous page
$prev_page = $page - 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$prev_page}' title='Previous page is {$prev_page}.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> < </span>";
echo "</a>";
}
// ********** show the number paging
// find out total pages
$query = "SELECT COUNT(*) as total_rows FROM table_mstr_department";
$stmt = $con->prepare( $query );
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_rows = $row['total_rows'];
$total_pages = ceil($total_rows / $recordsPerPage);
// range of num links to show
$range = 2;
// display links to 'range of pages' around 'current page'
$initial_num = $page - $range;
$condition_limit_num = ($page + $range) + 1;
for ($x=$initial_num; $x<$condition_limit_num; $x++)
{
// be sure '$x is greater than 0' AND 'less than or equal to the $total_pages'
if (($x > 0) && ($x <= $total_pages))
{
// current page
if ($x == $page)
{
echo "<span class='customBtn' style='background:#0D6598;'>$x</span>";
}
// not current page
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?page=$x' class='customBtn'>$x</a> ";
}
}
}
// ***** for 'next' and 'last' pages
if($page<$total_pages)
{
// ********** show the next page
$next_page = $page + 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$next_page}' title='Next page is {$next_page}.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> > </span>";
echo "</a>";
// ********** show the last page
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$total_pages}' title='Last page is {$total_pages}.' class='customBtn'>";
echo "<span style='margin:0 .5em;'> >> </span>";
echo "</a>";
}
/*
echo "<input type='text' name='page' size='1' />";
echo "<input type='submit' name='Go_btn' value='Go' class='customBtn' />";<?php */
echo "</div>";