I am using SQLite3 and trying to adapt the found Paginator script. After changing commands like MYSQL, it works, but does not display the correct number of elements and seems to differ in the results it gives.
I also get the following error, which I'm not sure how to fix:
Note: Undefined index: video in C: \ xampp \ htdocs \ Projects \ index.php on line 27
The code I use is:
Paginator.php
<?php class Paginator { private $_conn; private $_limit; private $_page; private $_query; private $_total; public function __construct( $conn, $query ) { $this->_conn = $conn; $this->_query = $query; $rs= $this->_conn->query( $this->_query ); $this->_total = count($rs); } public function getData( $limit = 10, $page = 1 ) { $this->_limit = $limit; $this->_page = $page; if ( $this->_limit == 'all' ) { $query = $this->_query; } else { $query = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit"; } $rs = $this->_conn->query( $query ); while ( $row = $rs->fetchArray() ) { $results[] = $row; } $results[] = []; $result = new stdClass(); $result->page = $this->_page; $result->limit = $this->_limit; $result->total = $this->_total; $result->data = $results; return $result; } public function createLinks( $links, $list_class ) { if ( $this->_limit == 'all' ) { return ''; } $last = ceil( $this->_total / $this->_limit ); $start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1; $end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last; $html = '<ul class="' . $list_class . '">'; $class = ( $this->_page == 1 ) ? "disabled" : ""; $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">«</a></li>'; if ( $start > 1 ) { $html .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>'; $html .= '<li class="disabled"><span>...</span></li>'; } for ( $i = $start ; $i <= $end; $i++ ) { $class = ( $this->_page == $i ) ? "active" : ""; $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>'; } if ( $end < $last ) { $html .= '<li class="disabled"><span>...</span></li>'; $html .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>'; } $class = ( $this->_page == $last ) ? "disabled" : ""; $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">»</a></li>'; $html .= '</ul>'; return $html; } } ?>
index.php
<?php require_once 'Paginator.php'; $db = new SQLite3('latest.db'); $limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 4; $page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1; $links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 4; $query = "SELECT ID, video FROM latest"; $Paginator = new Paginator( $db, $query ); $results = $Paginator->getData( $page, $limit ); ?> <!DOCTYPE html> <head> <title>PHP Pagination</title> <link rel="stylesheet" href="css/stylesheet.css"> </head> <body> <div class="container"> <div class="col-md-10 col-md-offset-1"> <h1>PHP Pagination</h1> <?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?> <p><?php echo $results->data[$i]['video']; ?></p> <?php endfor; ?> <p><?php echo $Paginator->createLinks($links, 'pagination pagination-sm'); ?></p> </div> </div> </body> </html>
To create a database and check the added data, I ran this script once:
<?php // Create the Database $db = new SQLite3('latest.db'); // Create the Table $db->query('CREATE TABLE IF NOT EXISTS latest (ID INTEGER PRIMARY KEY ASC, video STRING)'); // Insert the data $db->query('INSERT INTO latest (video) VALUES ("XoiEkEuCWog")'); $db->query('INSERT INTO latest (video) VALUES ("jsbeemdD2rQ")'); $db->query('INSERT INTO latest (video) VALUES ("hv44srAsAo4")'); $db->query('INSERT INTO latest (video) VALUES ("nwpj9_hrK_A")'); $db->query('INSERT INTO latest (video) VALUES ("sY3rIlrTTh8")'); $db->query('INSERT INTO latest (video) VALUES ("QpbQ4I3Eidg")'); $db->query('INSERT INTO latest (video) VALUES ("M0it_zMP-EM")'); $db->query('INSERT INTO latest (video) VALUES ("6X_C9E55CfM")'); $db->query('INSERT INTO latest (video) VALUES ("cNw8A5pwbVI")'); $db->query('INSERT INTO latest (video) VALUES ("J-gYJBsln-w")'); echo '<h1>The Following Data Was Created</h1>'; // Get the data $results = $db->query('SELECT ID, video FROM latest'); while ($row = $results->fetchArray()) { echo '<b>ID:</b> ' . $row['ID'] . ', <b>Video:</b> ' . $row['video'] . '<br>'; } ?>
How can I fix the error and is it correct to work with this code?
Edit:
Thanks to the proposed removal fix, "$ results [] = [];" The index page now displays correctly, but it doesn’t work properly.
What I expected was that since the $ limit is set to 10, it will list 10 lines and show the “1” paginator buttons, and if the $ limit was set to 5, then 5 displays and “1,2” as buttons, however, it currently only displays one line, for example:
nwpj9_hrK_A
In addition, clicking the next button and the page button gives unexpected results, and not the end of the page list if it gives something random.