Highlight Current page number PHP Pagination - php

I'm having problems resolving this so I'm asking for more professional solutions. I have pagination setup for my list of products. Problem is when I switch pages, the active class on the number is not showing or showing wrongly. For example, when users click on 2 (Page 2), it displays the 2nd page of items, but 5 is the one highlighted. Need help fixing this up to highlight the currently being viewed page.
This code is for setting the offset.
$totalrows = db_getvalue("select count(buyinfo_id) from buyinfo,user $addtable where buyinfo.user_id=user.user_id $clause $orderbyclause", $global_connection);
if ($totalrows == "")
$totalrows = 0;
$totalpages = ceil($totalrows/$listcount);
if ($totalpages > 1) {
$curpageoffset = 0;
if (isset($_REQUEST['offset'])) {
if ($_REQUEST['offset'] >= $totalpages)
$curpageoffset = $totalpages - 1;
else if ($_REQUEST['offset']>0)
$curpageoffset = $_REQUEST['offset']*$listcount-1;
}
$limitstr = " limit $curpageoffset,$listcount";
}
When I go to Page 3 for example, it displays www.site.com/?offset=2
I guess its because of the -1 in $curpageoffset = $_REQUEST['offset']*$listcount-1;
This is the code responsible for the page numbers:
<?php
if ($totalpages>1) {
echo "<tr><td><ul class='pagination'>";
echo "<li><a href='' aria-label='Previous' ";
if ($curpageoffset == 0) {
echo "onclick='return false;'";
} else {
echo "onclick='util_reload(\"posts/?offset=".($curpageoffset-1)."$pageparam\"); return false;'";
}
echo " ><span aria-hidden='true'>«</span></a></li>";
$tmpctr = 0;
while ($tmpctr < $totalpages) {
$curstyle = "";
if ($tmpctr == $curpageoffset)
$curstyle = "class='active'";
echo "<li $curstyle><a href='' onclick='util_reload(\"posts/?offset=$tmpctr$pageparam\"); return false;'>";
$tmpctr++;
echo "$tmpctr</a></li>";
}
echo "<li><a href='' aria-label='Next' ";
if ($curpageoffset >= $totalpages-1) {
echo "onclick='return false;'";
} else {
echo "onclick='util_reload(\"posts/?offset=".($curpageoffset+1)."$pageparam\"); return false;'";
}
echo " ><span aria-hidden='true'>»</span></a></li>";
echo "</ul></td></tr>";
}
?>

Since you've set the value of curpageoffset for use in your query, you can't use it in your pagination code to highlight the page you're on ... I would change your code this way .. using the original $_REQUEST['offset'] since that's essentially the page number.
while ($tmpctr < $totalpages) {
$curstyle = "";
if ($tmpctr == $_REQUEST['offset'])
$curstyle = "class='active'";
echo "<li $curstyle><a href='' onclick='util_reload(\"posts/?offset=$tmpctr$pageparam\"); return false;'>";
$tmpctr++;
echo "$tmpctr</a></li>";
}

Related

Pagination does not work perfectly

I have the following code:
$max = 4;
$page = isset($_GET['page']) ? ($_GET['page']) : '1';
$init = $page - 1;
$init= $max * $init;
$strCount = "SELECT COUNT(*) AS 'total_mytable' FROM mytable";
$varstrCount = $crud->viewdatas($strCount);
$total = 0;
if(count($varstrCount)){
foreach ($varstrCount as $row) {
$total = $row["total_mytable"];
}
}
$result = "SELECT * FROM mytable ORDER BY id_mytable LIMIT $init,$max";
$varresult = $crud->viewdatas($result);
content of page:
<?php
if(count($varresult)){
foreach ($varresult as $res) {
?>
<h5><?php echo $res['title'] ?></h5>
<?php
}
}
?>
<?php
$max_links = 10;
$previous = $page - 1;
$next = $page + 1;
$pgs = ceil($total / $max);
if($pgs > 1 ){
if($previous > 0){
echo "<li><a href='".BASE_URL."/category/$previous' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
} else{
}
for($i=$page-$max_links; $i <= $pgs-1; $i++) {
if ($i <= 0){
}else{
if($i != $page{
if($i == $pgs){ //if end insert 3 dots
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li> ...";
}else{
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li>";
}
} else{
if($i == $pgs){ //if end insert 3 dots
echo "<li>".$i."</li> ...";
}else{
echo "<li>".$i."</li>";
}
}
}
}
if($next <= $pgs){
echo "<li><a href='".BASE_URL."/category/$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
}
}
?>
The result:
And I not understand the reason for the active number stay right off the paging menu
In the code I defined max-links for 10, but no show number 5 and if I define max links for 3 changes nothing , displays the same result as the image.
Thanks for any help
echo "<li>".$i."</li>";
on the above line add up a href, seems like you have css formatting for li>a because of which you are not getting the required formatting on only li. so for getting better formatting for all paging links current, prev, next. you need to add up a tags.
echo "<li><a>".$i."</a></li>"; //in your else part

php search pagination is not working

i can make pagination by query database and it works just fine. but when i use form to search from database, i can only get the first page data, the next page data won't show up.
i just cannot figure out how to maintain the search query..
this is my code. the problem should be in the url links in the pagination, but i just cannot see the problem
<?php
require('koneksi.php');
if(isset($_GET['search'])) {
$search = $_GET['search'];
$keyword = $_GET['keyword'];
$koneksi = mysqli_connect("localhost","root","","mycompany");
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM product WHERE deskripsi LIKE '%" . $keyword . "%'";
$result = mysqli_query($koneksi,$sql);
$rss = mysqli_fetch_row($result);
$numrows = $rss[0];
//numbers or rows to show per page
$rowperpage = 6;
//find out total pages
$totalpages = ceil($numrows/$rowperpage);
//get the current page or set default
if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page number
$currentpage = 1;
} // end if
// if the current page is greater than total pages...
if($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than total pages...
if($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowperpage;
$sql = "SELECT * FROM product WHERE deskripsi LIKE '%" . $keyword . "%' LIMIT $offset, $rowperpage";
$result = mysqli_query($koneksi, $sql);
// while there are rows to be fetched
while ($list = mysqli_fetch_assoc($result)) {
// echo data
echo $list['product_code'];
echo "<br>";
echo $list['deskripsi'];
}
/****** build the pagination links ******/
// range of num links to show
$range = 6;
$url = "searchbar.php";
// if not on page 1, don't show back links
if($currentpage > 1) {
// show << link to go to page 1
echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1'> << </a>";
//get previous page num
$prevpage = $currentpage - 1;
} // end if
echo " <li class='arrow'><a href='$url?currentpage=prevpage?&keyword=$keyword?cari=$cari'>«</a></li> ";
for($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it is a valid page number
if(($x > 0) && ($x <= $totalpages)) {
// if we are on current page
if($x == $currentpage) {
echo "<li class=''><a href=''> $x </a></li>";
} else {
// make it a link
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'> $x </a> ";
//echo "<li class=''><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x </a></li>";
echo "<li class=''><a href='$url?currentpage=$x?&keyword=$keyword?cari=$cari'> $x </a></li>";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <li class='arrow'><a href='$url?currentpage=$nextpage?&keyword=$keyword?cari=$cari'>»</a></li> ";
// echo forward link for lastpage
// echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> >> </a> ";
} // end if
} // end if get search
require('footer.php');
?>
Looks like there could be a couple things missing here. For instance:
echo " <li class='arrow'><a href='$url?currentpage=prevpage?&keyword=$keyword?cari=$cari'>«</a></li> ";
in this line you are missing a $ for the prevpage: $prevpage but the querystring in the URL should only start with a ? and not contain question marks elsewhere, so this line should read more like
echo " <li class='arrow'><a href='$url?currentpage=$prevpage&keyword=$keyword&cari=$cari'>«</a></li> ";
I'm not 100% sure if that's going to fix your issue, but there is one big thing that I would ask you to look into before actually using this code anywhere and that's the SQL Injection that you have in your query.
You might read a bit of How can I prevent SQL injection in PHP? to get a better idea of how to rewrite your sql queries.
So, checkout your links, make sure the querystring is formatted correctly (http://host.com/script.php?querystring=something&var2=anothervar) where variables are separated only by &
As #aaronott pointed out, most of your links are wrong.
You are using cari=$cari which is not set anywhere, while in fact I guess you should add search=1 (or search=$search, but it's not really useful).
Also, you cannot have more than on ? in your querystring, so fix all your links like this:
...
if($currentpage > 1) {
echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=1&search=1&keyword=$keyword'> << </a>";
$prevpage = $currentpage - 1;
}
echo " <li class='arrow'><a href='$url?currentpage=$prevpage&search=1&keyword=$keyword'>«</a></li> ";
...
if($x == $currentpage) {
echo "<li class=''><a href=''> $x </a></li>";
} else {
echo "<li class=''><a href='$url?currentpage=$x&search=1&keyword=$keyword'> $x </a></li>";
} // end else
...
echo " <li class='arrow'><a href='$url?currentpage=$nextpage&search=1&keyword=$keyword'>»</a></li> ";

pagination with search result

I am using pagination with my script. But even it has few search cafeterias. And my pagination is also not working because of that search criteria. I want to display all the records 1st and then if the search criteria given it has to display according to that. Then only my pagination works.
But i am not getting how to do
here is my script which is to display records from database with pagination
if(isset($_GET['page'])){ $page=$_GET['page']; } else { $page=1; }
$start_page=($page-1)*5;
`$sql="query";
if(isset($_POST['submit']))
{
$company = $_POST['company'];
$product = $_POST['product'];
if(isset($_POST['submit']) && ($_POST['company']!='') && ($_POST['product']!=''))
{
$sql= "My query goes here";
}
elseif(isset($_POST['submit']) && ($_POST['company']!=''))
{
$sql= "My query goes here";
}
elseif(isset($_POST['submit']) && ($_POST['product']!=''))
{
$sql= "My query goes here";
}
elseif(isset($_POST['submit']))
{
$sql= "My query goes here";
}
}
$sql1 = mysql_query($sql) or die(mysql_error());
//Display
?>
<div class="w-box w-box-blue">
<div class="w-box-header">
</div>
<div class="w-box-content">
<table id="dt_hScroll" class="table table-striped">
<thead><tr>
<th>Order ID</th>
<th>Company</th>
<th>Reference</th>
<th>Total Value</th>
<th>Action</th>
</tr>
</thead>
<?php
while($row=mysql_fetch_array($sql1))
{
//Display
}
?>
</table>
</div>
</div>
<?php
//PAGINATION SCRIPT
$sql1="select COUNT(order_id) from orders";
$rs_result = mysql_query($sql1);
$row1 = mysql_fetch_row($rs_result);
$total_records = $row1[0];
$total_pages = ceil($total_records / 5);
if ($page > $total_pages) {
// set current page to last page
$page = $total_pages;
} // end if
// if current page is less than first page...
if ($page < 1) {
// set current page to first page
$page = 1;
} // end if
$range=4;
echo "<div style='float:left; width:200px;text-align:center; margin-left:40%;'>";
// echo "<a href='view_student.php?page=".$i."'>".$i."</a> ";
if ($page > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?page=1'>First</a> ";
// get previous page num
$prevpage = $page - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>Previous</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $total_pages)) {
// if we're on current page...
if ($x == $page) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($page != $total_pages) {
// get next page
$nextpage = $page + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?page=$total_pages'>Last</a> ";
} // end if
echo "</div>";
?>
Try using some kind of query builder, that would make your task easier.
All popular frameworks have that kind of tool. It would be much easier to build count query for pagination.
You may look here for example http://framework.zend.com/manual/1.12/en/zend.db.select.html

How to continue numbered list in all next pages with pagination?

I am trying to display data in an ordered list with pagination in php. It is working fine on page one but on next page, the list starts again from 1 instead of continuing from previous number.
Here is my code:
<?php
// find out how many rows are in the table
$sql01 = mysql_query("SELECT COUNT(*) FROM pm,pm_reply");
$r = mysql_fetch_row($sql01);
$numrows = $r[0];
$rowsperpage = 3;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
}
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
}
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
}
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
// while there are rows to be fetched...
$sql=mysql_query("select *from pm where send_to='$_GET[name]' limit $offset, $rowsperpage");
$sql2=mysql_query("select *from pm_reply where send_to='$_GET[name]' limit $offset, $rowsperpage");
echo "<ol>";
while($rows=mysql_fetch_assoc($sql)) {
echo"<li>From: <a href='profile.php?name=$rows[send_by]'>$rows[send_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows[subject]'>$rows[subject]</a></li>";
echo "<hr>";
echo "<br>";
}
while($rows2=mysql_fetch_assoc($sql2)) {
echo"<li>From: <a href='profile.php?name=$rows2[replied_by]'>$rows2[replied_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows2[subject]'>$rows2[subject]</a></li>";
echo "<hr>";
echo "<br>";
}
echo "</ol>";
echo "</div>";
//End Div Content
echo "<div style='clear:both;'></div>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<div id='pagelinks'>";
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=1'>First...</a> ";
// get previous page num
$prevpage = $currentpage - 1;
}
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
// if we're on current page...
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
} else {
// if not current page...
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=$x'>$x</a> ";
}
}
}
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=$totalpages'>...Last</a> ";
}
Just add start="$offset" to your echo "<ol>"; line.
echo "<ol start='$offset'>";

pagination outputting to disable a link after clicking a page

hello i have a code the next previous and last page are working but i can't get the link for the pages to work where the page will be disabled when you go there . i tried changing some parts but when i tried it there's no error simply all of the page number is disabled
here's my php code the last part only can't fix it tried but i'm still really a beginner
echo "<center>" . $numrows . " search results found</center>";
echo "<center>";
if ($pages >=1 && $page <= $pages) {
if($page == $first_page){
echo "Previous ";
} else{
if(!isset($page)){
echo "Previous ";
}else{
// But if page is set and it's not 1.
$previous = $page-1; echo "<a
href='?page=".$previous."&q=".$searchtext."'>Previous</a> ";
}
}
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.' </strong>' : ''.$x.' ' ;
}
if($page == $last_page){
echo "Next ";
}else{
// If page is not set or it is set and it's not the last page.
if(!isset($page)){
$next = $first_page+1; echo "<a href='?page=".$next."'>Next</a> ";
}else{
$next = $page+1; echo "<a href='?page=".$next."'>Next</a> ";
}
} echo "<a href='?page=".$last_page."&q=".$searchtext."'>Last
page</a>";
}
echo "</center>";
the
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.' </strong>' : ''.$x.' ' ;
}
part is the one i use to output the pages don't know the configuration for the isset of that part.
If I understand your question correctly you want the current page number to have no link? If so you could try something like the following.
for ($x=1; $x<=$pages; $x++) {
if ($page <> $x){
echo ($x == $page) ? '<strong>'.$x.'</strong>' : ''.$x.' ';
} else { echo $x.' '; }
}

Categories