Jump to content

Recommended Posts

I have been going through a couple of PHP OOP pagination tutorials I followed several and decided to use this one but it just does not work. I am sure I am connected to the database  but it will not pull the information from the database.

I am just going to include the index and pagination class. 

Here is the index.php page

<?php
// Initialize both configuration and pagination file one by one
include_once("config.php");
include_once("pagination.php");
 
//get the page number using request method
$myoffset = $_REQUEST["page"];
 
// check whether the requested page variable is empty. If empty start with one
$page = !empty($myoffset) ? (int)$myoffset:1;
 
//Assign per page results
$per_page = 5;
 
//Assign total count of records am manually entered value here.
//If you receive data from database see the next code
$total_count = 25; 
        
//Query For getting total count
$sql = "SELECT COUNT(*) FROM City";
$totalcount = mysqli_query($sql);
        echo "total count is $totalcount";
 
//Query For Fetching Results as per Pagination count from Database
$mysql = "SELECT * FROM City ORDER by id DESC LIMIT {$per_page} OFFSET {$myoffset}";
 
//Assign all value to populate pagination with oops
$pagination = new Pagination($page,$per_page,$total_count);
// Store all pagination values in this variable called $res
$res = "";
 
//Checking total page, if total page not more than one than hide pagination
if($pagination->total_pages() > 1)
{
//check if it has previous page
if($pagination->has_previous_page())
{ 
$res .= '<a href="index.php?page='.$pagination->previous_page().'"> « Previous </a>'; 
} 
//populating looping values to show navigation
for($i=$page;$i<=$pagination->total_pages();$i++)
{ 
$res .= '<a href="index.php?page='.$i.'"  class="active"> '.$i.'</a>';
}
//check if it has next page
if($pagination->has_next_page())
{ 
$res .= '<a href="index.php?page='.$pagination->next_page().'">Next »</a>'; 
} 
}
?>
 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pagination</title>
<!-- Adding style for pagination -->
<style>
.paginationblock{
width:350px;
height:auto;
margin:0 auto;
margin-top:300px
}
.paginationblock a{
width:auto;
height:auto;
float:left;
padding:10px 15px 10px 15px;
text-decoration:none;
font-family: Helvetica, Arial, 'lucida grande',tahoma,verdana,arial,sans-serif;
font-size:16px;
background-color:#222222;
color:#ffffff;
margin-right:3px;
}
.paginationblock a:hover{
background-color:#09F;
}
</style>
</head>
<body>
<!-- Initialize pagination script -->
 <div class="paginationblock">
  <?php echo $res; ?>
 </div>
</body>
</html>
 

now my pagination.php page

<?php
class Pagination
{
    public $current_page;
    public $per_page;
    public $total_count;
 
        public function __construct($page=1,$per_page=0,$total_count=0) {
            $this->current_page = (int)$page;
            $this->per_page = (int)$per_page;
            $this->total_count = (int)$total_count;
        }
 
        public function offset(){
           return ($this->current_page - 1) * $this->per_page ;
        }
 
        public function total_pages(){
           return ceil($this->total_count/$this->per_page);
        }
 
        public function previous_page(){
            return $this->current_page-1;
        }
 
        public function next_page(){
            return $this->current_page+1;
        }
 
        public function has_previous_page(){
            return $this->previous_page() >= 1 ? true:false;
        }
 
        public function has_next_page(){
            return $this->next_page() <= $this->total_pages() ? true:false;
        } 
}
        // Declaring Object here 
        $pagination = new Pagination();
?>
 

All help greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/298099-php-oop-pagination-tutorials/
Share on other sites

I am sure I am connected to the database  but it will not pull the information from the database.

Makes sense: there isn't actually any code in there to execute the query (which is supposedly contained in the $mysql variable), nor to display the results. Does the tutorial add that code later?

 

And what does it say about the query? Because what you have in $mysql is incorrect.

Edited by requinix

Makes sense: there isn't actually any code in there to execute the query (which is supposedly contained in the $mysql variable), nor to display the results. Does the tutorial add that code later?

 

And what does it say about the query? Because what you have in $mysql is incorrect.

 Thank you for responding but to  answer your question it does not add the code later. How is the mysql query incorrect.

The offset is incorrect: $myoffset will be a page number like 1,2,3 when it needs to be a starting offset like 0,25,50. I suggest putting off building the query, or at least the LIMIT part of it, until after the pagination object is available - then you can use its calculations for the offset (in the offset() method).

 

After you have the query, execute it and display the results however you want. Strictly speaking the pagination work is really just about getting the page number in the URL, getting links to navigate between pages, and adjusting the query's LIMIT accordingly. And you have that part.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.