sql - Simple PHP Pagination script -
sql - Simple PHP Pagination script -
i have rows of info coming database, have table simple pagination, easiest way of doing it? i'd glad if provide.
this mix of html , code it's pretty basic, easy understand , should simple decouple suit needs think.
try { // find out how many items in table $total = $dbh->query(' select count(*) table ')->fetchcolumn(); // how many items list per page $limit = 20; // how many pages there $pages = ceil($total / $limit); // page on? $page = min($pages, filter_input(input_get, 'page', filter_validate_int, array( 'options' => array( 'default' => 1, 'min_range' => 1, ), ))); // calculate offset query $offset = ($page - 1) * $limit; // info display user $start = $offset + 1; $end = min(($offset + $limit), $total); // "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>'; // "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 paging info echo '<div id="paging"><p>', $prevlink, ' page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>'; // prepare paged query $stmt = $dbh->prepare(' select * table order name limit :limit offset :offset '); // bind query params $stmt->bindparam(':limit', $limit, pdo::param_int); $stmt->bindparam(':offset', $offset, pdo::param_int); $stmt->execute(); // have results? if ($stmt->rowcount() > 0) { // define how want fetch results $stmt->setfetchmode(pdo::fetch_assoc); $iterator = new iteratoriterator($stmt); // display results foreach ($iterator $row) { echo '<p>', $row['name'], '</p>'; } } else { echo '<p>no results displayed.</p>'; } } grab (exception $e) { echo '<p>', $e->getmessage(), '</p>'; } php sql pagination
Comments
Post a Comment