public function make_pagination() { $total = 0; $query = "SELECT COUNT(downloads.dn_id) FROM downloads WHERE downloads.dn_type = 'audios'"; $stmt = $this->conn->prepare($query); $stmt->execute(); $total = $stmt->fetchColumn(); //echo 'row_count = ' . $total; // How many items to list per page $limit = 11; // How many pages will there be $pages = ceil($total / $limit); // What page are we currently on? $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array( 'options' => array( 'default' => 1, 'min_range' => 1, ), ))); // Calculate the offset for the query $offset = ($page - 1) * $limit; // Some information to display to the user $start = $offset + 1; $end = min(($offset + $limit), $total); // The "back" link $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>'; // The "forward" link $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>'; // Display the paging information echo '<div id="paging"><p>'.$prevlink.' Page '.$page.' of '.$pages. ' pages'. $nextlink.' </p></div>'; //prepare the page query $query2 = " SELECT * FROM downloads, map_artists, song_artists WHERE map_artists.dn_id = downloads.dn_id AND song_artists.artist_id = map_artists.artist_id AND downloads.dn_type = 'audios' GROUP BY downloads.dn_id ORDER BY downloads.dn_time DESC LIMIT :limit OFFSET :offset "; $stmt2 = $this->conn->prepare($query2); $stmt2->bindParam(':limit', $limit, PDO::PARAM_INT); $stmt2->bindParam(':offset', $offset, PDO::PARAM_INT); $stmt2->execute(); // Do we have any results? if ($stmt2->rowCount() > 0) { // Define how we want to fetch the results $stmt2->setFetchMode(PDO::FETCH_ASSOC); $iterator = new IteratorIterator($stmt2); // Display the results foreach ($iterator as $row) { echo '<p>'. $row['dn_title'].' - '. $row['artist_name'].'</p>'; } } else { echo '<p>No results could be displayed.</p>'; } }
source share