Need some help in my pagination

can I ask some help please in my pagination. I am using this pagination plugin but I don’t know how to get this work. here is my attempt but I cannot load the data. can you help me please on this


class Database{

    protected $connection;

    public function __construct(PDO $connection)
        $this->connection = $connection;

    public function getData(){

            $cmd = $this->connection->prepare("SELECT COUNT(*) FROM names");
            $rows = $cmd->fetchColumn();

            return $rows;

        }catch(PDOException $ex){


public function display($start,$perpage){

        $cmd = $this->connection->prepare("SELECT name FROM names LIMIT ? , ?");

        $rows = $cmd->fetchAll(PDO::FETCH_ASSOC);
        $datali = '';
        foreach($rows as $r){
            $datali.='<li class="list-group-item">'.$r['name'].'</li>';

        return $datali;

    }catch(PDOException $ex){




<?php require_once 'includes/config.php'; require_once 'includes/Database.php'; $pdo = new PDO(HOST,USER,PASSWORD); $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $connectdb = new Database($pdo); $rows = $connectdb->getData(); $page = $_GET['page']; $per_page = 5; $pages = ceil( $rows / $per_page ); if($page == 0) $start = 0; else $start = ( $page - 1 ) * $per_page; $data = $connectdb->display($start,$per_page); ?>
    <!doctype html>
    <html lang="en">
        <meta charset="UTF-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
      <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <script type="text/javascript">
        $(function () {
            var pages = '<?php echo $pages;?>';
               totalPages: pages,
              visiblePages: 7,
               href: '?page='+pages
       <div class="wrapper">
         <?php echo $data ?>
        <ul class="list-group" id="mylist-ul"></ul>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
       <script type="text/javascript" src="js/jquery.twbsPagination.min.js"></script>

Thank you in advance.

Well for starters i’m gonna guess pages is supposed to be an integer not a string, so why did you wrap quotes around it?

            var pages = '<?php echo $pages;?>';

Second is when someone first loads the page, $_GET[‘page’] is not defined, so lets handle that a bit better.
If it’s defined, set $page to the int-casted value of $_GET[‘page’] (this part is important.).
If not defined, set it to 1.
If $page is < 1 or > $pages, set $page to 1.
$start may now be cleanly defined as ($page - 1) * $per_page.

Thanks I will fix my code, I have question how can I have two pagination with different content ?

different variable names, different function names, same as anything else. They can share a connection object though.

Ok thanks. Also can I use post method like this $_POST[‘page’] ?

Can you do pagination with post, yes.
Can you do it using that example with post? no. That example is a simplified version that just tells the jquery container to load a given URL’s data, which means GET only.

Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.