How to arrange the first and second pages in php? - php

I have this code:
<?php
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
if ($page == 1) {
$out.= "<span style=\"display:none;\">" . $prevlabel . "</span>\n";
} elseif ($page == 2) {
$out.= "<li>" . $prevlabel . "\n</li>";
} else {
$out.= "<li>" . $prevlabel . "\n</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
}
?>
How to when the first page , appears 5 points. For E.g, When I was at page 1 , then I would like this 1 2 3 4 5.. but if I change the "$ adjacents=2;" to 4. Indeed, when the 1 page there is a 1 2 3 4 5.. But when I moved to the page=7 , then , The page displayed 1 2 3 4 5 6 7 8 9 a lot of numbers, or when I moved to page=12. The page displayed 8 9 10 11 12 13 14 15 16.. Whereas , I Want to display 5 digits only.
How so that when the first and second page only, is made to appear 5 digit page ?And when I turn the page , eg page 12 like this. 10 11 12 13 14.
or when in page 1 to be like this 1 2 3 4 5
or when in page 2 to be like this 1 2 3 4 5
or when in page 3 to be like this 1 2 3 4 5.. etc

You can copy paste this script
<?php
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
if ($page == 1) {
$out.= "<span style=\"display:none;\">" . $prevlabel . "</span>\n"; $pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$adjacents = 4;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
} elseif ($page == 2) {
$out.= "<li>" . $prevlabel . "\n</li>";
$adjacents = 3;
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
} else {
$out.= "<li>" . $prevlabel . "\n</li>";
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li>" . $i . "</li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
}
}
?>

Related

Pagination dupliacates pages on first and last page

If I'm on page 2 (?p=2) This is how the pager look like
Första « 1 2 3 » Sista
However if I'm on page 1 or 3 it looks like this
(?p=1) 1 2 3 3 » Sista
(?p=3) Första « 1 1 2 3
What am I doing wrong?
I'm using the logic from another question that was posted on this site but noone seems to experience this problem as I do. Limit pagination page number
function get_paging_info($tot_rows,$pp,$curr_page)
{
$pages = ceil($tot_rows / $pp); // calc pages
$data = array(); // start out array
$data['si'] = ($curr_page * $pp) - $pp; // what row to start at
$data['pages'] = $pages; // add the pages
$data['curr_page'] = $curr_page; // Whats the current page
return $data; //return the paging data
}
function pagin($db,$sql,$limit,$curr_page,$max_pages=7)
{
$res = $db->query($sql);
$count = $res->num_rows;
$paging_info = get_paging_info($count,$limit,$curr_page);
$out[] = "
<nav aria-label='Page navigation'>
<ul class='pagination'>";
if($paging_info['curr_page'] > 1)
{
$out[] = "
<li><a href='?p=1' title='Sida 1'>Första</a></li>
<li>
<a href='?p=". ($paging_info['curr_page'] - 1) ."' aria-label='Föregående'>
<span aria-hidden='true'>«</span>
</a>
</li>";
}
$max = $max_pages;
if($paging_info['curr_page'] < $max)
{
$sp = 1;
}
elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
{
$sp = $paging_info['pages'] - $max + 1;
}
elseif($paging_info['curr_page'] >= $max)
{
$sp = $paging_info['curr_page'] - floor($max/2);
}
if($paging_info['curr_page'] >= $max)
{
$out[] = "<li><a href='?p=1' title='Sida 1'>1</a></li>";
}
for($i = $sp; $i <= ($sp + $max -1);$i++)
{
if($i > $paging_info['pages']) continue;
if($paging_info['curr_page'] == $i)
{
$out[] = "<li class='active'><span class='strong'>". $i ."</span></li>";
}
else
{
$out[] = "<li><a href='?p=". $i ."' title='Sida ". $i ."'>". $i ."</a></li>";
}
}
if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2)))
{
$out[] = "<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>". $paging_info['pages'] ."</a></li>";
}
if($paging_info['curr_page'] < $paging_info['pages'])
{
$out[] = "
<li>
<a href='?p=". ($paging_info['curr_page'] + 1) ."' aria-label='Nästa' title='Page ". ($paging_info['curr_page'] + 1) ."'>
<span aria-hidden='true'>»</span>
</a>
</li>
<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>Sista</a></li>";
}
$out[] = "</ul></nav>";
return implode('',$out);
}
I think your problem is due to this two conditions before and after your for loop :
First:
if($paging_info['curr_page'] >= $max)
{
$out[] = "<li><a href='?p=1' title='Sida 1'>1</a></li>";
}
Second:
if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2)))
{
$out[] = "<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>". $paging_info['pages'] ."</a></li>";
}
You don't need this conditions because the corresponding pages are already displayed with the for loop.
Try this. I have removed some extra codes from your function. replace your pagin function with following code and try
function pagin($db,$sql,$limit,$curr_page,$max_pages=7)
{
$res = $db->query($sql);
$count = $res->num_rows;
$paging_info = get_paging_info($count,$limit,$curr_page);
$out[] = "
<nav aria-label='Page navigation'>
<ul class='pagination'>";
if($paging_info['curr_page'] > 1)
{
$out[] = "
<li><a href='?p=1' title='Sida 1'>Första</a></li>
<li>
<a href='?p=". ($paging_info['curr_page'] - 1) ."' aria-label='Föregående'>
<span aria-hidden='true'>«</span>
</a>
</li>";
}
$max = $max_pages;
if($paging_info['curr_page'] < $max)
{
$sp = 1;
}
elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
{
$sp = $paging_info['pages'] - $max + 1;
}
elseif($paging_info['curr_page'] >= $max)
{
$sp = $paging_info['curr_page'] - floor($max/2);
}
for($i = $sp; $i <= ($sp + $max -1);$i++)
{
if($i > $paging_info['pages']) continue;
if($paging_info['curr_page'] == $i)
{
$out[] = "<li class='active'><span class='strong'>". $i ."</span></li>";
}
else
{
$out[] = "<li><a href='?p=". $i ."' title='Sida ". $i ."'>". $i ."</a></li>";
}
}
if($paging_info['curr_page'] < $paging_info['pages'])
{
$out[] = "
<li>
<a href='?p=". ($paging_info['curr_page'] + 1) ."' aria-label='Nästa' title='Page ". ($paging_info['curr_page'] + 1) ."'>
<span aria-hidden='true'>»</span>
</a>
</li>
<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>Sista</a></li>";
}
$out[] = "</ul></nav>";
return implode('',$out);
}

PHP MySQL pagination mysqli_result() error

I'm working on pagination, but it didn't work I tried using mysqli_fetch_array and also tried mysqli_result.
Please guide me if you have some ideas.
For the pagination page I tried a method that I found but still have problem with it.
Here is the code I tried,
<?php
$conn = mysqli_connect("localhost", "root", "", "vm");
include 'paginate.php';
$per_page = 2;
$result = mysqli_query($conn, "SELECT * FROM table1");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page'])) {
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
$start = 0;
$end = $per_page;
}
} else {
$start = 0;
$end = $per_page;
}
$page = intval($_GET['page']);
$tpages = $total_pages;
if ($page <= 0) {
$page = 1;
}
function paginate($reload, $page, $tpages)
{
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out .= "<span>" . $prevlabel . "</span>\n";
} elseif ($page == 2) {
$out .= "<li>" . $prevlabel . "\n</li>";
} else {
$out .= "<li>" . $prevlabel . " \n</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out .= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n";
} elseif ($i == 1) {
$out .= "<li>" . $i . "\n</li>";
} else {
$out .= "<li>" . $i . "\n</li>";
}
}
if ($page < ($tpages - $adjacents)) {
$out .= "<a style='font-size:11px' href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if ($page < $tpages) {
$out .= "<li>" . $nextlabel . " \n</li>";
} else {
$out .= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out .= "";
return $out;
}
Thanks in advance.

Pagination in search php

I have a search form and i want to put a pagination, but when i press the next button or second page on the search using pagination , all the results get messed up, and there is no data being fetched from the query. I don't know where the exact problem could be, but this is my search code:
include ('paginate.php'); //include of paginat page
$per_page = 5;
$find = $_POST['find'];
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find); // number of results to show per page
$result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%$find%'");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//------------if page is setcheck-----------------//
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 1;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
$show_page=1;
}
// display pagination
if(isset($_GET['page'])){
$page = intval($_GET['page']);
}else{
$page =1;
}
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>Project</th> <th>Country</th> <th>Active</th>
</tr></thead>";
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
?><form name="frmactive" method="POST" action="">
<input type="hidden" name="id" value="<?php echo mysql_result($result, $i, 'p_id');?>" />
<?php
// echo out the contents of each row into a table
echo '<tr><td>' . mysql_result($result, $i, 'p_name') . '</td>';
echo '<td>' . mysql_result($result, $i, 'p_country') . '</td>';
if (mysql_result($result, $i, 'p_isActive')=='1'){
echo '<td>Active</td>';
echo '<td align="center">Edit</td>';
}
else{
echo'<td>Inactive</td> ';
echo '<td align="center">Edit</td>';
echo "</tr>";
}
// close table>
}
echo "</table>";
// pagination
?>
and this is the paginate.php
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out.= "<span>" . $prevlabel . "</span>\n";
} elseif ($page == 2) {
$out.= "<li>" . $prevlabel . "\n</li>";
} else {
$out.= "<li>" . $prevlabel . "\n</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n";
} elseif ($i == 1) {
$out.= "<li>" . $i . "\n</li>";
} else {
$out.= "<li>" . $i . "\n</li>";
}
}
if ($page < ($tpages - $adjacents)) {
$out.= "<a style='font-size:11px' href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if ($page < $tpages) {
$out.= "<li>" . $nextlabel . "\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
}
Could you please point out where the bug is taking place ? Thank you for your time
Try this (pay attention to the comments please) :
<?php
/*
Assuming that your search form has a POST method set to it,
be sure to do a redirect to this page and send the find parameter gotten
from the posted form urlencoded
ie path_to_my_file?find='.urlencode(trim($_POST['find']))
*/
include ('paginate.php'); //include of paginat page
$per_page = 5;
$find = strip_tags(strtoupper(trim(urldecode($_GET['find']))));
$page = intval($_GET['page']);
$result_total = mysql_query("SELECT COUNT(*) AS total FROM projects WHERE p_name LIKE '%".$find."%'");
$tmp_total = mysql_fetch_assoc($results_total);
$totalpages = ceil($tmp_total['total'] / $per_page);
if ($totalpages != 0 && $page > $totalpages) {
$page = $totalpages;
} else if ($page < 1) {
$page = 1;
}
$offset = ($page - 1) * $per_page;
$result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%".$find."%' LIMIT ".$offset.", ".$per_page);
$show_page = 1;
//You need to pass the phrase you are searching for in every link in order for the pagination to work (or user Sessions to save the keyword somewhere if you don't want it in the link)
$reload = $reload = 'http://localhost/Personal/Stackoverflow/2/index.php?find='.urlencode($find);
echo ' <div class="pagination">
<ul>';
if ($totalpages > 1) {
echo paginate($reload, $show_page, $totalpages);
}
echo ' </ul>
</div>';
// display data in table
if(mysql_num_rows($result) > 0) {
echo ' <table class="table table-bordered">
<thead>
<tr>
<th>Project</th>
<th>Country</th>
<th>Active</th>
</tr>
</thead>';
while($row = mysql_fetch_assoc($result)) {
echo ' <tr>
<td>'.$row['p_name'].'</td>
<td>'.$row['p_country'].'</td>
<td>'.((intval($row['p_isActive']) == 1) ? 'Active' : 'Inactive').'</td>
<td align="center">
Edit
</td>
</tr>';
}
echo ' </table>';
}
?>

Easy to Use Pagination

I am trying to get a nice paginator to work but I'm having trouble with getting the upcoming/middle numbers to work properly.
The goal is to show the first and last 5 pages of a result set regardless of what page your on, but these first and last 5 can only show if there are enough pages to allow it.
The pagination would look something like this: << 1 2 3 4 5 - 12 13 14 - 78 79 80 81 82 >>
With only a few pages: << 1 2 3 4 5 6 7 8 9 10 >>
And how to avoid this?: << 1 2 3 4 5 - 5 6 7 - 7 8 9 10 11 >>
My code so far is:
function pagination_links($page, $num_rows, $results_per_page, $each_direction = 3)
{
$total_pages = $num_rows ? ceil($num_rows / $results_per_page) : 1 ;
if($total_pages < 2)
{
return null;
}
$page = ((is_numeric($page)) && ($page >= 1) && ($page <= $total_pages)) ? (int)$page : 1 ;
$output = null;
if($page > 1)
{
$output .= '<div class="pageBtn"><<</div>' ;
}
else
{
$output .= '<div class="pageBtnDis"><<</div>' ;
}
for($i=1;$i<$total_pages;$i++)
{
if($page != $i)
{
$output .= '<div class="pageBtn">' . $i . '</div>' ;
}
else
{
$output .= '<div class="pageBtnSet">' . $i . '</div>' ;
}
if($i > 4)
{
break ;
}
}
for($i = $page - $each_direction; $i <= $page + $each_direction; $i++)
{
if(($i > 5) && ($i <= $total_pages-5))
{
if($page != $i)
{
$output .= '<div class="pageBtn">' . $i . '</div>' ;
}
else
{
$output .= '<div class="pageBtnSet">' . $i . '</div>' ;
}
}
}
for($i = $total_pages-5;$i<$total_pages;$i++) {
if($page != $i)
{
$output .= '<div class="pageBtn">' . $i . '</div>' ;
}
else
{
$output .= '<div class="pageBtnSet">' . $i . '</div>' ;
}
}
if($page < $total_pages)
{
$output .= '<div class="pageBtn">>></div>' ;
}
else
{
$output .= '<div class="pageBtnDis">>></div>' ;
}
return $output ;
}
I think the current best solution is to use Digg Style Pagination Class. It greatly simplifies the creation and styling of your pagination markup.
The Pager PEAR class is highly customizable and I've had a lot of success with it.

PHP question pagination

How would I do so that the page I'm on won't be clickable?
$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$limit = ($page - 1) * 15;
$sql = mysql_query("SELECT * FROM log LIMIT $limit, 15");
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM log"),0);
$totalpages = ceil($totalres / 15);
for ($i = 1; $i <= $totalpages; $i++) {
$pagination .= "$i ";
}
<p><?php echo $pagination ?></p>
if ($i == $page)
$pagination .= "$i ";
else
$pagination .= "$i ";
Just need to modify the for loop to check what the current page is.
for ($i = 1; $i <= $totalpages; $i++)
{
if ($i != $page)
$pagination .= "$i ";
else
$pagination .= " " . $i . " ";
}
With an if inside the for,
if ( $i != $page) {
//Your code.
}
else
{
//Same page.
$pagination .= " " . $page . " "
}
#RedBlueThing Has soon has I saw your post I got it to work, although I got the idea from a different stackoverflow question with a little modifying of the code.
<?php
echo "<section>";
$link = mysqli_connect("localhost", "root", "root", "user_db");
$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$offset = 4;
$x = 5;
$limit = ($page - 1) * $offset;
$sql = mysqli_query($link,"SELECT * FROM cfp_blogs;");
$totalres = mysqli_num_rows($sql);
$totalpages = ceil($totalres / $offset);
$stmt = mysqli_query($link, "SELECT * FROM blogs LIMIT $limit, $offset;");
if($stmt->num_rows > 0){
while($obj = mysqli_fetch_object($stmt)) {
echo "<h1>$obj->title</h1>";
echo "<p>$obj->content</p>";
$stmt2 = $link->query("SELECT COUNT(id) AS Tot FROM blogs");
if($stmt2->num_rows > 0){
while ($obj2 = $stmt2->fetch_object()){
echo "<p>($obj2->Tot)</p>";
}
}
else
echo "<p>(0)</p>";
$stmt2->close();
}
}
else {
echo "<section class='error'><h1>There was a problem with the database.</h1></section>";
}
mysqli_free_result($stmt);
for ($i = $page - $x; $i < $page; $i++)
{
if ($i >= 1) {
$pagination .= "$i ";
}
}
$pagination .= " " . $i . " ";
for ($i = $page + 1; $i <= $page + $x; $i++)
{
if ($i <= $totalpages) {
$pagination .= "$i ";
}
}
/*for ($i = 1; $i <= $totalpages; $i++)
{
if ($i != $page){
$pagination .= "$i ";
}
else{
$pagination .= " " . $i . " ";
}
}*/
mysqli_close($link);
echo $pagination;
echo "</section>";
?>
The only thing I couldn't get was having a "next" or "previous" button. If anyone is up to the task of adding on those buttons, give it a shot. Other wise..... Enjoy..... :) This is my way of giving back to the stackoverflow community for helping me.

Categories