Problem:- Simple Pagination Code with PHP – MySql PDO
Solution:-
<?php $user = "root"; $pass = ""; $dbname = 'testdb'; $host = "localhost"; $mysql = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $offset = isset($_GET['offset'])? intval($_GET['offset']) : 1; $per_page = 5; $total = $mysql->query('SELECT COUNT(*) FROM data')->fetchColumn(0); $limitedSQL = "SELECT * FROM data ORDER BY id LIMIT $per_page OFFSET ". ($offset-1); $lastRowNumber = $offset-1; foreach ($mysql->query($limitedSQL) as $row2) { $lastRowNumber++; print "{$row2['name']} ===========> {$row2['mobile']}<br>"; } indexed_links($total, $offset, $per_page); print "<br>"; print "(Displaying $offset-$lastRowNumber of Total)"; //=========function for print text and record=======// //=================================================// function print_link($inactive, $text, $offset=" "){ if ($inactive) { print "<span class='inactive'>$text</span>"; }else{ print "<span class='active'>". "<a href='".htmlentities($_SERVER['PHP_SELF'])."?offset=$offset'>$text</a></span>"; } } //Indexded link funtion function indexed_links($total, $offset, $per_page){ //=========for print <<PREV link=======// //=====================================// $separator = " | "; print_link($offset == 1, "<< Prev", max(1, $offset - $per_page)); //=========for print group Record=======// //==========not print Last one=========// //====================================// for ($start=1, $end = $per_page ; $end < $total; $start += $per_page, $end += $per_page) { print $separator; print_link($offset == $start, "$start-$end", $start); } $end = ($total > $start) ? "- $total": " "; //=========for print Last Group=======// //=====================================// print $separator; print_link($offset == $start, "$start$end", $start); //=========for print <<NEXT link=======// //=====================================// print $separator; print_link($offset == $start, 'Next>>', $offset + $per_page); } ?>
Output:-
इसे भी पढ़े – Display Inline Message in Form With Re-display Form