hello guys im using this second part of pagination script to show pagination;
<?
if ($pageno == 1) {
echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
$prevpage = $pageno-1;
echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
echo " <li class='active'>$pageno</li>";
if ($pageno == $lastpage) {
echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
$nextpage = $pageno+1;
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>
like you see here: echo " <li class='active'>$pageno</li>"; i only can show first, prev, active page, next, last page..
my question is: i want to show more pages near active page.. how can i do this?
i mean pagination style is now like:
FIRST PREV 1 NEXT LAST
i want
FIRST PREV 1 2 3 4 5 6 7 NEXT LAST
thanks
Just add a loop which loops from the first to the last page numbers:
for($page_number = 1; $page_number <= $amount_of_pages; $page_number++)
if($page_number == $pageno)
echo " <li class='active'>$page_number (active)</li>";
else
echo " <li class='active'>$page_number</li>";
To let this work you'd need to find the maximum amount of items and divide it with the amount of items on a page:
$result = mysql_query("SELECT COUNT(*) FROM table");
$row = mysql_fetch_row($result);
$amount_of_items = $row[0];
$amount_of_pages = $amount_of_items / 10; // 10 items on a page
Of course you need to add some checks and stuff, this only shows the basic principles.
Between the part which displays the previous page links and the part which displays the next page links, you need a loop between 1 and total pages.
<?php
$currentPage = 3;
$totalPages = 10;
?>
First page
Prev page
<?php
for ($i = 0; < $totalPages; $i++) {
printf('Page %d', $_SERVER['PHP_SELF'], $i, ($i == $currentPage ? 'active' : ''), $i);
}
?>
Next page
Last page
<?
if ($pageno == 1) {
echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
$prevpage = $pageno-1;
echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
for($page_number = 1; $page_number <= $lastpage; $page_number++)
if($page_number == $pageno) {
echo "<li class='active'>$pageno</li>";
}
else {
echo "<li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$page_number'>$page_number</a></li>";
}
if ($pageno == $lastpage) {
echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
$nextpage = $pageno+1;
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>
this is fixed version. works fine.
Related
I have counted the total results and designed the pagination links. onclicking i am passsing the number value to php file. how to handle $page variable in ajax response. how to show previous 1 2 ... 100 next links like the pagination functionality. I have tried with the following functionality
$page = 1;
$total = 10;
$limit = 20;
$total_pages = ceil($total / $limit);
if (isset($_GET["page"] ))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
<ul class="pagination">
<li class="association_page active">1</li>
<li class="association_page">2</li>
<li class="association_page">3</li>
</ul>
echo "<ul class='pagination'>";
if ($page > 1) {
echo "<li class='association_page'><a href='#' class='button'>Previous</a></li>";
}
for ($i=1; $i<=$total_pages; $i++) {
if($page==$i){
echo "<li class='association_page active'>".$i."</li>";
} else {
echo "<li class='association_page' >".$i."</li>";
}
};
if($page < $total_pages){
echo "<li class='association_page'>NEXT</li>";
}
echo "</ul>";
Onclick i am passing the page number to php file and reponse the results json encoding. How to show
Json Output:
{"page":"8","html_content":"<li class=\"14\"></li>"}
I have not shared full content
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>";
}
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> ";
I am using Bootstrap to display pagination on my pages but I have an issue. I want to use the bootstrap active class to show the current page but I don't know how to go about it.
When I add the active class (as shown in the code below), all the links become active. Please what do I do?
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 6;
$sql = "SELECT * FROM offers ORDER BY i.id ASC LIMIT $start_from, 6";
$sql2 = "SELECT COUNT(id) FROM offers";
$rs_result = $db-> query($sql2, array('id' => $_SESSION['id']));
$row = $rs_result->fetch();
$total_records = $row[0];
echo $total_records;
$total_pages = ceil($total_records / 6);
for ($i=1; $i<=$total_pages; $i++) {
;echo"
<ul class='pagination'>
<li class='active' ><a href='myoffer.php?page=$i' >$i</a></li>
</ul>
";
};
?>
You should add a condition that tells which page is currently being displayed (based on the value of the query string 'page', also you should put the echo <ul line before your loop:
echo "<ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
if ($page == $i)
echo "<li class='active' >";
else
echo "<li>";
echo "<a href='myoffer.php?page=$i' >$i</a></li>";
};
echo "</ul>";
This is a copy of Aram Tchekrekjian's answer
($page == $i) ? "<li class='active' >" : "<li>";
Even better than that is to have the ternary operator inside of the <li>
tag and task it with echo class ="active" portion. So it would look like this
<li <?php ($page == $i) ? class="active" : false;?>>1</li>
I made this script for my website.
It works and everything but there is one small problem.
You can go forward infinitely
You can just click Next Page in an endless loop
And for some reason when i echo $num_pages I get 1 0_0
How can I fix the "infinie nexting" - my weird definition for the problem :)
<?php
$per_page = 4;
$start = 1;
if(!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
if($page <= 1) {
$start = 1;
$page = 1;
} else {
$start = $page * $per_page - $per_page;
}
$next = $page+1;
$previous = $page-1;
$GetAllComments = $con->query("SELECT * FROM comments LIMIT $start, $per_page");
$num_rows = $GetAllComments->num_rows;
$num_pages = $num_rows / $per_page;
while($GAC = $GetAllComments->fetch_object()) {
echo "<div class='well'> <h3>". $GAC->Title. "</h3>
". $GAC->Content. " <hr /> <em> Posted By ". $GAC->PosterName ." </em>
</div>";
}
$pagen = $page+1;
$pagep = $page-1;
echo "
<div class='pagination'>
<ul>
";
if($page > 1) {
echo "
<li><a href='?page=$previous'>".$pagep."</a> </li>
";
}
echo "
<li class='disabled'><a href='#'>$page</a></li>
";
echo "
<li> <a href='?page=$next'>" . $pagen . "</a></li>
</ul>
</div>
";
I would put the display in a for loop:
for($i = 1; $i <= $num_pages; $i++)
{
enter code here
}
That way it will repeat for as many pages are in num_pages and not anymore.