任何人都知道如何设置我正在寻找的这个分页的好地方或好的教程?
在你生气我在这里发布数百行之前,让我快速解释一下我想要实现的目标,我用来更好地解释我的情况的代码。
如果您只想分页或只想搜索,下面的代码可以工作:
1. 进入主页,分页工作。点击第 1 页、第 2 页等,你会得到你期望的结果。
2.使用搜索,它可以工作,但这会在一页内提供所有记录而不受限制。我可以设置一个限制,但分页仍然不起作用。
我只是不知道如何获取搜索变量并将其添加到分页中。
是否有可能有一个像这样的网址:search=Hello+World?orderby=custforename+custsurname?page=3..我认为它是......这样的东西,'?nested get variables?叫?有人知道高级分页教程的好地方吗?
我在我的主页上使用此代码:
<?php
if(isset($_POST['search']))
{
$search = $_POST['search'];
$search = ltrim($search, " ");
$terms = explode(" ", $search);
$customerlistquery = "
SELECT *
FROM customer
LEFT JOIN company
ON customer.compid=company.compid
WHERE
";
$i = 0;
foreach ($terms as $each)
{
if ($i++ > 0)
$customerlistquery .= ' OR ';
$customerlistquery .= "concat(custsurname, custforename, custmidname, custpostal, custphone1, custphone2, custfax, custnamecode, datemodified, modifiedby) LIKE '%$each%' ";
}
$customerlistquery .= " ORDER BY $orderby ASC";
}
else
{
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$startpoint = ($page * $limit) - $limit;
$customerlistquery = "
SELECT *
FROM customer
LEFT JOIN company
ON customer.compid=company.compid
ORDER BY $orderby ASC
LIMIT {$startpoint}, {$limit}
";
echo pagination("customer
LEFT JOIN company
ON customer.compid=company.compid
ORDER BY $orderby ASC",$limit,$page);
}
?>
$result=mysql_query($itemslistquery) or die("query fout " . mysql_error() );
最后是 pagination.php 代码:
<?php
function pagination($query, $per_page = 10,$page = 1, $url = '?'){
$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysql_fetch_array(mysql_query($query));
$total = $row['num'];
$adjacents = "2";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 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 ($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}page=$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}page=$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=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}page=$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";
}
else
{
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=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}page=$counter'>$counter</a></li>";
}
}
}
if ($page < $counter - 1){
$pagination.= "<li><a href='{$url}page=$next'>Next</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
}else{
$pagination.= "<li><a class='current'>Next</a></li>";
$pagination.= "<li><a class='current'>Last</a></li>";
}
$pagination.= "</ul>\n";
}
return $pagination;
}
?>