在Php中,将查询字符串从页面传递到另一个页面时如何进行分页。以下代码我使用它可以很好地进行没有查询字符串的分页但是在将值从页面传递到另一个页面(查询字符串)时它可以正常工作。 请给我任何建议。
<?php
include("connect.php");
$adjacents = 3;
$id = $_REQUEST["id"];
$query = "SELECT COUNT(*) as num FROM data";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$targetpage = "pagetest.php";
$limit = 10;
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
$sql = "SELECT name,email,dateins FROM data WHERE name='$id' LIMIT $start, $limit";
$result = mysql_query($sql);
$cnt=0;
if($result)
while($rs=mysql_fetch_assoc($result))
{
$cnt=$cnt+1;
$cssRow="class='gridRow'";
if(($cnt%2)==1)
{
$cssRow="class='gridRowAlternate'";
}
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
if ($page > 1)
$pagination.= "<a href=\"$targetpage?id=$id?page=$prev\">Previous</a>";
else
$pagination.= "<span class=\"disabled\">Previous</span>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?id=$id?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?id=$id?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?id=$id?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?id=$id?page=$lastpage\">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?id=$id?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?id=$id?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?id=$id?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?id=$id?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?id=$id?page=$lastpage\">$lastpage</a>";
}
else
{
$pagination.= "<a href=\"$targetpage?id=$id?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?id=$id?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?id=$id?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?id=$id?page=$next\">Next</a>";
else
$pagination.= "<span class=\"disabled\">Next</span>";
$pagination.= "</div>\n";
}
?>
<tr>
<td><?php echo $rs['name']; ?></td>
<td><?php echo $rs['email']; ?></td>
<td><?php echo $rs['dateins']; ?></td>
</tr>
<?php
}
?>
<td><input type="hidden" name="id" id="id" value="<?php echo $id; ?>"></td>
</table>
<?php echo $pagination; ?>
?>
感谢先生。
答案 0 :(得分:2)
您需要将您想要查看的$ _GET变量添加到您的分页链接中,否则在第一次请求后您将无法使用它们。
例如:
if ($page > 1)
$pagination.= "<a href=\"$targetpage?id=$id&page=$prev&something=$_GET['something']\">Previous</a>";
else
$pagination.= "<span class=\"disabled\">Previous</span>";
我还注意到您的$ _GET变量在链接中设置错误。
此
<a href=\"$targetpage?id=$id?page=$lastpage\">$lastpage</a>
应该是
<a href=\"$targetpage?id=$id&page=$lastpage\">$lastpage</a>
向会话中添加任何其他变量可能更容易,因此如果有多个变量,您不必担心将它们动态地添加到查询字符串中。
答案 1 :(得分:0)
页面: - pagination.func.inc.php
<style type="text/css">
ul.pagination {
font-family: "Arial", "Helvetica", sans-serif;
font-size: 13px;
height:auto;
list-style-type: none;
margin: 20px 0;
overflow: hidden;
padding: 0;
}
ul.pagination li.details {
background-color: white;
border-color: #C8D5E0;
border-image: none;
border-style: solid;
border-width: 1px 1px 2px;
color: #1E598E;
font-weight: bold;
padding: 6px 10px;
text-decoration: none;
}
ul.pagination li.dot {
padding: 3px 0;
}
ul.pagination li {
float: left;
list-style-type: none;
margin: 0 3px 0 0;
}
ul.pagination li:first-child {
margin-left: 0;
}
ul.pagination li a {
color: black;
display: block;
line-height:normal;
padding: 7px 10px;
text-decoration: none;
}
ul.pagination li a img {
border: medium none;
}
ul.pagination li a.current {
background-color:#08569D;
color:#fff;
}
ul.pagination li a.current:hover {
/*background-color: white;
color: #333333; */
}
ul.pagination li a:hover {
/*background-color: #C8D5E0; */
background-color:#08569D;
color:#fff;
}
ul.pagination li a {
background-color: #F6F6F6;
border-color: #C8D5E0;
border-image: none;
border-style: solid;
border-width: 1px ;
color: #1E598E;
display: block;
font-weight: bold;
padding: 8px 10px;
text-decoration: none;
}
ul.pagination li.details {
border: 1px solid #DDDDDD;
color: #999999;
}
</style>
<?php
function pagination_function($per_page = 10, $page = 1, $url = '', $total)
{
$adjacents = "2";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$firstpage = 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class='pagination'>";
//$pagination .= "<li class='details'>Page $page of $lastpage</li>";
if($page == '1')
{
$pagination .= "<li class='details'>First</li>";
$pagination .= "<li class='details'>Previous</li>";
}
else
{
$pagination.= "<li><a href='{$url}$firstpage'>First</a></li>";
$pagination .= "<li><a href='{$url}$prev'>Previous</a></li>";
}
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href='{$url}1'>1</a></li>";
$pagination.= "<li><a href='{$url}2'>2</a></li>";
$pagination.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>";
}
else
{
$pagination.= "<li><a href='{$url}1'>1</a></li>";
$pagination.= "<li><a href='{$url}2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}$counter'>$counter</a></li>";
}
}
}
if ($page < $counter - 1){
$pagination.= "<li><a href='{$url}$next'>Next</a></li>";
$pagination.= "<li><a href='{$url}$lastpage'>Last</a></li>";
}else{
$pagination .= "<li class='details'>Next</li>";
$pagination .= "<li class='details'>Last</li>";
//$pagination.= "<li><a class='current'>Next</a></li>";
// $pagination.= "<li><a class='current'>Last</a></li>";
}
$pagination.= "</ul>\n";
}
return $pagination;
}
?>
您的信息页
<?php
include("connect.php");
require_once('pagination.func.inc.php');
$id = $_REQUEST["id"];
$pageno = addslashes(trim($_REQUEST['page']));
if($pageno !=''){$pageno = $pageno;}else{$pageno = '1';}
//Your Query
$query = "SELECT COUNT(*) as num FROM data";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num]; // Total Record
$targetpage = "pagetest.php";
$limit = 10; // Par page limit
//Here your While/Foorlop/foreach condition
$get_pageurl = $targetpage ."?page=";
echo $pagination_function = pagination_function($limit,$pageno,$get_pageurl,$total_pages);
?>