pagination using php with mysql data - php

i am trying to create a pagination in which there are 5 items at a time depending on the number of items in DB. i wrote this code . but i dono how to go further .its buggy..any better pagination or alteration for this
<?php
$myresult .= "<div class='pagination' >";
if ($pagenum == 1)
{
}
else
{
$pagenum = 1;
$myresult .= "<a href='javascript:newPage(\"".$pagenum."\")'> first </a>";
$myresult .= " ";
$previous = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous."\")'> Prev </a>";
if ($pagenum == $last)
{
$previous3 = $pagenum-4;
$myresult .= "<a href='javascript:newPage(\"".$previous3."\")'> $previous3 </a>";
$previous2 = $pagenum-3;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
}
if ($pagenum > 2)
{
$previous1 = $pagenum-2;
$myresult .= "<a href='javascript:newPage(\"".$previous1."\")'> $previous1 </a>";
}
if ($pagenum > 1)
{
$previous2 = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
$myresult .= " ";
}
}
$myresult .= "<span class=\"disabled\"> $pagenum </span>";
if ($pagenum == $last)
{
}
else {
if($pagenum < $last - 1)
{
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> $next </a>";
}
if($pagenum < $last - 2)
{
$next1 = $pagenum+2;
$myresult .= "<a href='javascript:newPage(\"".$next1."\")'> $next1 </a>";
}
if($pagenum == 1 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
$next3 = $pagenum+4;
$myresult .= "<a href='javascript:newPage(\"".$next3."\")'> $next3 </a>";
}
if($pagenum == 2 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
}
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> Next </a>";
$myresult .= "<a href='javascript:newPage(\"".$last."\")'> Last</a>";
}
$myresult .= "</div>";
$myresult .= "</br>";
?>

Maybe not the best answer ever but here is something I have used:
<?php
class Pagination {
function __construct() {
}
function getPaginatinationNavigation($page = 1, $num_rows, $rows_per_page, $page_name)
{
$lastpage = ceil($num_rows/$rows_per_page);
$page = (int)$page;
if ($page > $lastpage)
{
$page = $lastpage;
}
if ($page < 1) {
$page = 1;
}
$content='<p style="text-align: center;">';
if ($page == 1) {
$content.= " FIRST PREV ";
} else {
$content.= " <a href='$page_name?page=1'>FIRST</a> ";
$prevpage = $page-1;
$content.= " <a href='$page_name?page=$prevpage'>PREV</a> ";
}
$content.= " ( Page $page of $lastpage ) ";
if ($page == $lastpage) {
$content.= " NEXT LAST ";
} else {
$nextpage = $page+1;
$content.= " <a href='$page_name?page=$nextpage'>NEXT</a> ";
$content.= " <a href='$page_name?page=$lastpage'>LAST</a> ";
}
$content.= '</p>';
return $content;
}
}
?>
Import the Object
require_once('classes/Pagination.php');
Get the amount of total rows:
$query= "SELECT COUNT(*) FROM TABLE_NAME";
$row = mysql_fetch_array($getResults);
$numRecords = $row[0];
Make the LIMIT to limit the rows based upon page number:
$limit = ' LIMIT ' . ($page - 1) * 25 .', 25';
Query USING LIMIT
$query = 'SELECT * FROM TABLE_NAME' . $limit;
$getResults=mysql_query($query) or die(mysql_error());
Display the Results as you normally would:
while($row = mysql_fetch_array($getResults))
{
DISPLAY RESULTS HERE
}
Use the Class to spit out Navigation:
$pagination->getPaginatinationNavigation($page, $numRecords, 25, 'display_page_name.php');
So Overall, for pagination you need to make 2 queries:
1) To get the total amount of records in your search
2) To get the Records LIMITed to those items in a range: say 25 to 50 or 1000 to 1050
You display records from the limited query and to go to the next set of records you increase the page number by one.
Hope this helps. Don't forget to scrub any data you get from the url querystring.
Let me know if you would like me to explain further.

Related

Limiting bootstrap pagination in PHP to 5 pages

I'm trying to create pagination in PHP where 5 pages are displayed.
When on the first page we have:
[1][2][3][4][5] ...[325][>>]
Clicking on [325] will take you to that page (the last record), clicking on the right arrow will take you to page [2].
When on the second page we have:
[<<][1]...[2][3][4][5][6] ...[325][>>]
And when on the last page we have:
[<<][1]...[321][322][323][324][325]
I've been researching on how to do this without much luck. I think I understand that I need to create an array with adjacent pages of 2 on each side of the active page, when we are on any page except the first or last page. I also need to create an <li>1</li>
and
<li><?php echo $last_record; ?></li>
for when a user is anywhere but the first or last record.
I have the following code which works great, however when we start getting a large number of records, the pagination count gets ridiculous.
<ul class="pagination pull-left pagination-md">
<?php
// Creates back button in pagination
if(isset($page)) {
if($page > 1) {
$page_minus = $page-1;
echo "<li><a href='blog.php?page=$page_minus'> « </a></li>";
}
}
?>
<?php
global $con;
$q_pagination = mysqli_prepare($con, "SELECT COUNT(*) FROM admin_panel WHERE ");
$q_pagination->execute();
$result_pagination = $q_pagination->get_result();
$rows_result = $result_pagination->fetch_array();
$total_rows = array_shift($rows_result);
$post_per_page = $total_rows/15;
$post_per_page = ceil($post_per_page);
for($i = 1; $i <= $post_per_page; $i++) {
if(isset($page)){
if($i == $page) {
echo "<li class='active'><a href='blog.php?page=$i'>$i</a></li>";
}
else {
echo "<li><a href='blog.php?page=$i'>$i</a></li>";
}
}
}
// Creates the forward button in pagination
if(isset($page)){
if($page+1 <= $post_per_page) {
$page_plus = $page+1;
echo "<li><a href='blog.php?page=$page_plus'> » </a></li>";
}
}
?>
</ul>
I'll admit, after researching and attempting to make this work I'm just getting twisted in the logic. Does anyone have any thoughts on how to best approach this? Leads, current examples, etc. Most of what I've found is dated, incomplete, or stupid long.
<?php
session_start();
include "mysqli_connect.php";
$companyID = $_SESSION['compid'];
$db = new Database();
$dbc = $db->getConnection();
$display = 3; //number of records per page
$pages;
$dbb = new Database();
$dbcb = $dbb->getConnection();
$stat = "select * from reservationStatus where resStatId = '$companyID' ";
$r = mysqli_query($dbcb, $stat);
//variable for sorting - default is for registration date
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';
switch ($sort)
{
case 'ln':
$orderby = 'uniquenumber ASC';
break;
case 'fn':
$orderby = 'status ASC';
break;
case 'rd':
$orderby = 'resId ASC';
break;
case 'em' :
$orderby = 'resDate ASC';
break;
default:
$orderby = 'resId ASC';
break;
}
if(isset($_GET['p']) ) //already calculated
{
$pages=$_GET['p'];
}
else
{
//get the total number of records from the table
$q = "select count(resId) from reservation where companyID = '$companyID'";
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_NUM);
$records=$row[0];
if($records > $display ) //$display is the number of records per page
//ceil rounds fractions up to integer value
$pages=ceil($records/$display);
else
$pages = 1;
}
//now determine where in the database to start
if(isset($_GET['s']) ) //already calculated
$start=$_GET['s'];
else
$start = 0;
//$q = "select * from users LIMIT $start, $display";
$q = "select * from reservation where companyID = '$companyID' order by $orderby LIMIT $start, $display";
$r = mysqli_query($dbc, $q);
if($r)
{
echo '<br />';
//display a table of results
echo '<div class="container">';
echo '<h1> Your Orders </h1>';
echo '<table align="center" class="table table-bordered table-striped" width="60%">';
echo '<tr bgcolor="#87CEEB">
<td><b>View</b></td>
<td><b>Change Status</b></td>
<td><b> Reference Number</b></td>
<td><b>Status</b></td>
<td><b>Date</b></td>
<td><b>Total Price</b></td></tr>';
//above is the header
//loop below adds the user details
//use the following to set alternate backgrounds
$bg = '#eeeeee';
while($row = mysqli_fetch_array($r))
{
$stat = "select * from reservationStatus where resStatusId = $row[7] ";
$rr = mysqli_query($dbcb, $stat);
$roww = mysqli_fetch_array($rr);
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
echo '<tr bgcolor="' . $bg. '">
<td>View</td>
<td>Change Status </td>
<td>'.$row[2].'</td>
<td>'.$roww[1].'</td>
<td>'.$row[1]. ' ' . $row[8].'</td>
<td>'.$row[3].'</td>
</tr>';
}
echo '</table></div>';
}
else
{
echo '<p class="error">' . $q . '</p>';
echo '<p class="error"> Oh dear. There was an error</p>';
echo '<p class="error">' . mysqli_error($dbc) .'</p>';
}
mysqli_free_result($r);
//makes links to other pages if required
if($pages > 1)
{
echo '<br /><p> ' ;
//find out what page we are on
$currentpage = ($start/$display)+1;
//need a previous button if not first page
if($currentpage != 1)
{
echo ' <a href="viewOrdersForCompanies.php?$s=' . ($start - $display) .
'&p=' .$pages . '&sort='.$sort.'">&nbspPrevious&nbsp</a>';
}
//create the numbered pages
for($i = 1; $i <= $pages; $i++)
{
if($i != $currentpage)
{
//the 's' paramater is used in the link to determine which the value
// in the LIMIT clause used in the select statement near the top of the page
echo '<a href="viewOrdersForCompanies.php?s=' . (($display * ($i-1))) . '&p='
. $pages . '&sort='.$sort.'">&nbsp' . $i . '&nbsp</a>';
}
//&nbsp is a character to insert a whitespace
}
//if not last page create next button
if($currentpage != $pages)
{
echo '<a href="viewOrdersForCompanies.php?s=' . ($start+$display) . '&p=' . $pages
. '&sort='.$sort.'">&nbspNext&nbsp</a>';
}
echo '</p>';
}
?>

Pagination with Jquery MySQLi?

how to convert MySQL to MySQLi with PHP and jQuery.
If I run the file below using MySQL work but I want to use MySQLi, how to change it?
cek my file:
pagination_class.php
<?php class Pagination_class
{
var $result;
var $anchors;
var $total;
function Pagination_class($qry,$starting,$recpage)
{
$rst = mysql_query($qry) or die(mysql_error());
$numrows = mysql_num_rows($rst);
$qry .= " limit $starting, $recpage";
$this->result = mysql_query($qry) or die(mysql_error());
$next = $starting+$recpage;
$var = ((intval($numrows/$recpage))-1)*$recpage;
$page_showing = intval($starting/$recpage)+1;
$total_page = ceil($numrows/$recpage);
if($numrows % $recpage != 0)
{
$last = ((intval($numrows/$recpage)))*$recpage;
}
else
{
$last = ((intval($numrows/$recpage))-1)*$recpage;
}
$previous = $starting-$recpage;
$anc = "<ul class='pagination m_bottom_0 f_top_5'>";
if($previous < 0)
{
$anc .= "<li class='disabled'>
<a href='javascript:;'>
<i class='mdw-icon fa fa-angle-left'></i>
</a>
</li>";
}
else
{
$anc .= "<li>
<a href='javascript:pagination($previous);'>
<i class='mdw-icon fa fa-angle-left'></i>
</a>
</li>";
}
$norepeat = 2;
$j = 2;
$anch = "";
for($i=$page_showing; $i>1; $i--)
{
$fpreviousPage = $i-1;
$page = ceil($fpreviousPage*$recpage)-$recpage;
$anch = "<li><a href='javascript:pagination($page);'>$fpreviousPage </a></li>".$anch;
if($j == $norepeat) break;
$j++;
}
$anc .= $anch;
$anc .= "<li class='active'><a href='javascript:;'>".$page_showing."</a></li>";
$j = 1;
for($i=$page_showing; $i<$total_page; $i++)
{
$fnextPage = $i+1;
$page = ceil($fnextPage*$recpage)-$recpage;
$anc .= "<li><a href='javascript:pagination($page);'>$fnextPage</a></li>";
if($j==$norepeat) break;
$j++;
}
if($next >= $numrows)
{
$anc .= "<li class='disabled'>
<a href='javascript:;'>
<i class='mdw-icon fa fa-angle-right'></i>
</a>
</li>";
}
else
{
$anc .= "<li>
<a href='javascript:pagination($next);'>
<i class='mdw-icon fa fa-angle-right'></i>
</a>
</li>";
}
$anc .= "</ul>";
$this->anchors = $anc;
$this->total = "<div>Halaman : <strong>$page_showing</strong> dari <strong>$total_page</strong>. Total : <strong>$numrows</strong> records</div>";
}
} ?>
and below file berita_view
please check in pastebin
Thanks.

Class pagination

I have problem error for load page 2,3,4 n etc of my original link is http://bali-webdesign.com/staging/astina3/subkategori-Bali%20Package-3-3.htm..
here my class_paging.php
class Paging{
function cariPosisi($batas){
if(empty($_GET['halaman'])){
$posisi=0;
$_GET['halaman']=1;
}
else{
$posisi = ($_GET['halaman']-1) * $batas;
}
return $posisi;
}
function jumlahHalaman($jmldata, $batas){
$jmlhalaman = ceil($jmldata/$batas);
return $jmlhalaman;
}
function navHalaman($halaman_aktif, $jmlhalaman){
$link_halaman = "";
// Link ke halaman pertama (first) dan sebelumnya (prev)
if($halaman_aktif > 1){
$prev = $halaman_aktif-1;
$link_halaman .= "<li class='paginate_button previous' id='table-gallery_previous'><a aria-controls='table-gallery' data-dt-idx='0' tabindex='0' href=$_SERVER[PHP_SELF]?halaman=1>«</a></li>
<li><a href=$_SERVER[PHP_SELF]?halaman=$prev>‹</a></li>";
}
else{
$link_halaman .= "<li class='disabled'><a>«</a></li><li class='disabled'><a>‹</a></li>";
}
// Link halaman 1,2,3, ...
$angka = ($halaman_aktif > 3 ? "" : " ");
for ($i=$halaman_aktif-2; $i<$halaman_aktif; $i++){
if ($i < 1)
continue;
$angka .= "<li><a href=$_SERVER[PHP_SELF]?halaman=$i>$i</a></li>";
}
$angka .= "<li class='paginate_button active'><a aria-controls='table-gallery' data-dt-idx='1' tabindex='0'>$halaman_aktif</a></li>";
for($i=$halaman_aktif+1; $i<($halaman_aktif+3); $i++){
if($i > $jmlhalaman)
break;
$angka .= "<li><a aria-controls='table-gallery' data-dt-idx='1' tabindex='0' href=$_SERVER[PHP_SELF]?halaman=$i>$i</a></li>";
}
$angka .= ($halaman_aktif+2<$jmlhalaman ? "<li><a href=$_SERVER[PHP_SELF]?halaman=$jmlhalaman>$jmlhalaman</a></li>" : " ");
$link_halaman .= "$angka";
// Link ke halaman berikutnya (Next) dan terakhir (Last)
if($halaman_aktif < $jmlhalaman){
$next = $halaman_aktif+1;
$link_halaman .= "<li class='paginate_button next' id='table-gallery_next'><a aria-controls='table-gallery' data-dt-idx='2' tabindex='0' href=$_SERVER[PHP_SELF]?halaman=$next>›</a></li><li><a href=$_SERVER[PHP_SELF]?halaman=$jmlhalaman>»</a></li>";
}
else{
$link_halaman .= "<li class='disabled'><a href='#'>›</a></li><li class='disabled'><a href='#'>»</a></li>";
}
return $link_halaman;
}
}
then in my page which I load pagination function
include "inc/config/class_paging.php";
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$s = new Paging;
$batas = 10;
$posisi = $s->cariPosisi($batas);
$produk = mysql_query("SELECT * FROM produk where id_subkategori='$_GET[idkk]' LIMIT $posisi,$batas");
$jmldata = mysql_num_rows(mysql_query("SELECT * FROM produk"));
while($p=mysql_fetch_array($produk)){
echo "blabla";
}
$jmlhalaman = $s->jumlahHalaman($jmldata, $batas);
$linkHalaman = $s->navHalaman($_GET[halaman], $jmlhalaman);
echo "<div align='center' class='dataTables_paginate paging_simple_numbers' id='table-about_paginate'>
<ul class='pagination'>$linkHalaman</ul>
</div>";
Right now, when I click page 2 button of pagination, the result link will be bali-webdesign.com/staging/astina3/subkategori.php?halaman=2 but that link can't show my data. I want when I click page 2 button of pagination will show my data
what should I change in class_paging.php for link url so it can show data on page 2,3,4 n etc

unable to display :active page in my pagination function

am using following function to display pagination
public function paginationLinks(){
$outputString = "";
$q = $this->db->query('SELECT COUNT(*) FROM videos');
$res = $q->fetch();
$newsNumber = $res[0];
$q->closeCursor();
for($i = 1; $i <= ceil($newsNumber / $this->newsByPage); $i++){
$outputString .="<li><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}
return $outputString;
}
and this is how i display
<ul class="pagination pagination-sm">
<?php echo $news->paginationLinks(); ?>
</ul>
now bootstrap doesn't display active page its because of function
how do i add some more option like next, previous and :active
just replace your function with this one
public function paginationLinks(){
$outputString = "";
$crpage = isset($_GET['page']) && trim($_GET['page']) != ''?trim($_GET['page']):1;
$q = $this->db->query('SELECT COUNT(*) FROM videos');
$res = $q->fetch();
$newsNumber = $res[0];
$q->closeCursor();
for($i = 1; $i <= ceil($newsNumber / $this->newsByPage); $i++){
if($crpage == $i){
$outputString .="<li class='active'><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}else{
$outputString .="<li><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}
}
return $outputString;
}
For next or previous see that link
great example for PHP pagination.
Please try this .I hope this help you
public function paginationLinks(){
$num_rec_per_page = 10;
$outputString = "";
$page = (isset($_GET['page']) && $_GET['page'] != '') ? trim($_GET['page']) : 1;
$start_from = ($page-1) * $num_rec_per_page;
$q = $this->db->query('SELECT COUNT(*) FROM videos');
$res = $q->fetch();
$total_records = $res[0];
$q->closeCursor();
$total_pages = ceil($total_records / $num_rec_per_page);
$outputString .= "<li><a href='?page=1'>".'|<'."</a></li>"; // Goto 1st page
if($page > 1){
$prev = $page - 1;
$outputString .= "<li><a href='?page=".$prev."'>Prev</a></li>"; // Goto previous page
}
for ($i=1; $i<=$total_pages; $i++) {
$activeClass = ($page == $i) ? 'active' : '';
$outputString .="<li class=".$activeClass."><a href='?page=" . $i . "'>" . $i . "</a></li> ";
}
if($page < $total_pages){
$next = $page + 1;
$outputString .= "<li><a href='?page=".$next."'>Next</a></li>"; // Goto Next page
}
if($page > 1){
$outputString .= "<li><a href='?page=".$total_pages."'>".'|>'."</a></li>"; // Goto last page
}
return $outputString;
}

How do active class on pagination

I'm trying to active class on list
Kindly if any one can help me to add class="active" on displayed pagination page:
$perpage= $conf['perpage'];
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $perpage;
$result = "SELECT * FROM topics LIMIT $start_from, $perpage";
$result = mysql_query ($result);
$n = 0;
while ($row = mysql_fetch_array ($result)){
echo '<tr>';
echo '<td>'.$row['topic_no'].'</td>';
echo '</tr>';
++$n;
}
$sql = "SELECT * FROM topics";
$result = mysql_query($sql);
$total_records = mysql_num_rows($result);
$total_pages = ceil($total_records / $perpage);
echo '<ul class="pagination">';
echo "<li><a href='topics.php?page=1'>".'<'."</a></li> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<li ><a href='topics.php?page=".$i."'>".$i."</a></li> ";
};
echo "<li><a href='topics.php?page=$total_pages'>".'>'."</a> </li>";
echo '</ul> ';
Thanks in Advance
You Can also try this code. it's working
$c="active";
for ($i=1; $i <$total_page ; $i++) {
if($page==$i)
{
$c="active";
}
else
{
$c="";
}
echo "<li class=\"$c\">$i</li>";
}
$active = $i == $page ? 'class="active"' : '';
echo "<li ><a {$active} href=\"topics.php?page={$i}\">{$i}</a></li> ";
We just need to add class="active" only if page is current, otherwise we add nothing. If you already have classes for rows - you just need to use smth like
$activeClass = $i == $page ? 'active' : '';
echo "<li ><a class=\"my-row-class1 {$activeClass}\" href=\"topics.php?page={$i}\">{$i}</a></li> ";
echo "<li if($_GET['page']==$i){ class='active'}><a href='topics.php?page=".$i."'>".$i."</a></li> ";
Please try this. It might help you
$search = #$_GET['page']; // get value form other page
$page ='A';
for ($Page=1; $Page <27 ; $Page++) { ?> // abcdef... create
<li class="<?php if($search==$page){ echo 'active'; } ?>"> <?php echo ''. $page++ . '' ;?> </li>
<?php } ?>
/* if($search==$page){ echo 'active'; } this code means if search value == alphabet then class active call automatically */

Categories