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

Database.php

class Database{

    protected $connection;


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



    public function getData(){
        try{

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

            $cmd=null;
            return $rows;

        }catch(PDOException $ex){

        }

    }
public function display($start,$perpage){
    try{

        $cmd = $this->connection->prepare("SELECT name FROM names LIMIT ? , ?");
        $cmd->bindParam(1,$start);
        $cmd->bindParam(2,$perpage);
        $cmd->execute();

        $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){

    }
}

}

index.php

<?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">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
<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;?>';
            $('#mylist-ul').twbsPagination({
               totalPages: pages,
              visiblePages: 7,
               href: '?page='+pages
            });
        }); 
    
        </script>
    
    </head>
    <body>
       <div class="wrapper">
         <?php echo $data ?>
        <ul class="list-group" id="mylist-ul"></ul>
       </div>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
       <script type="text/javascript" src="js/jquery.twbsPagination.min.js"></script>
    </body>
    </html>

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.