Anyone know a good place or a good tutorial how to set up this pagination I’m looking for?
Before you get mad that I’m posting hundreds of lines here, let me just give a fast explaination about what I want to achieve, the codes I use to explain my situation better.
The code below works if you only want to paginate or only want to search:
1. Enter the main page, and the pagination works. Hit page 1, page 2, etc and you get the result you are expecting.
2. Use the search, and it works, but this gives ALL records without limitation in 1 page. I could set up a limit but the pagination still will not work.
I just don’t know how to get the search variable and add it to the pagination.
Is it possible to have an url like: search=Hello+World?orderby=custforename+custsurname?page=3..I think it is… How is something like this, ‘?nested get variables?’ called? Anyone knows a good place around for an advanced pagination tutorial?
I’m using this code on my main page:
<?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() );
And at last the pagination.php code:
<?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;
}
?>
If you want to pass more than 1 variable with GET, you have to use it like this:
url?var1=x&var2=y&var3=z
In PHP you would recover them like this:
About your ‘paginator’ problem, I usually only do 1 query and then paginate it with javascript on the client side, this way I can change pages without waiting for the server to answer.
If you want to do it with PHP only, you should set ‘LIMIT x,y’ on your query, where x is the first element of the page, and y the number of elements you want to show in one page. For example, if you are showing pages of 50 elements:
Something like this should work just fine.