Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(!function_exists("ShowRecords")) {
- function ShowRecords($sql_query, $table) {
- global $conn;
- $button_display='';
- $query_string = '?';
- foreach($_GET as $q => $k) {
- if($q == 'id' or $q == 'pagenum') {
- //
- } else {
- if($q != '') {
- $query_string = '&'.$q.'='.$k;
- }
- }
- }
- // Find total number of rows in table
- //$sql="SELECT COUNT(*) FROM $table";
- $sql = $sql_query;
- $rs = mysql_query($sql,$conn);
- if(mysql_num_rows($rs) > 0) {
- $total_rows = mysql_num_rows($rs);
- // Set rows per page
- if($total_rows < 25) {
- $rows_per_page = $total_rows;
- } else {
- $rows_per_page = 25;
- }
- // Calculate total number of pages
- $total_pages = ceil($total_rows / $rows_per_page);
- // Get current page
- $current_page = (isset($_GET['pagenum']) && $_GET['pagenum'] > 0) ? (int) $_GET['pagenum'] : 1;
- // If current page is greater than last page, set it to last.
- if ($current_page > $total_pages)
- $current_page = $total_pages;
- // Set starting post
- $offset = ($current_page - 1) * $rows_per_page;
- // Get rows from database
- $sql = $sql_query.' LIMIT '.$offset.', '.$rows_per_page;
- //$sql="SELECT * FROM $table LIMIT $offset, $rows_per_page";
- $rs = mysql_query($sql,$conn) or die(mysql_error());
- //return rows
- $results = array();
- while($result = mysql_fetch_array($rs)) {
- $results[] = $result;
- //echo $result["company"].'<br>';
- }
- //return $results;
- // Build navigation
- // Range of pages on each side of current page in navigation
- $range = 4;
- //if the total number of pages are less then 20 then display all page numbers
- //otherwise display a certain range of pages in between with ...<LastPageNumber>
- unset($_GET["pagenum"]);
- if($total_pages > 20) {
- //Previous page and first page
- if ($current_page > 1) {
- $back = $current_page - 1;
- $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$back.'\'" value="Previous" /> ';
- if($current_page > ($range + 1)) {
- $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum=1\'" value="1" /> ... ';
- }
- } else {
- $button_display.= ' <input type="button" value="Previous" disabled="disabled" /> ';
- }
- //numbers between
- for($i = $current_page - $range; $i < ($current_page + $range) + 1; $i++) {
- if($i > 0 && $i <= $total_pages) {
- if($i == $current_page) {
- $button_display.= ' <input type="button" value="'.$i.'" class="active" /> ';
- } else {
- $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$i.'\'" value="'.$i.'" /> ';
- }
- }
- }
- //next and last page
- if($current_page != $total_pages) {
- $next = $current_page + 1;
- if(($current_page + $range) < $total_pages) {
- $button_display.= ' ... <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$total_pages.'\'" value="'.$total_pages.'" /> ';
- }
- $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$next.'\'" value="Next" /> ';
- } else {
- $button_display.= ' <input type="button" value="Next" disabled="disabled" /> ';
- }
- } else {
- //Previous page
- if ($current_page > 1) {
- $back = $current_page - 1;
- $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$back.'\'" value="Previous" /> ';
- } else {
- $button_display.= ' <input type="button" value="Previous" disabled="disabled" /> ';
- }
- //numbers between
- for($i = 0; $i < ($total_pages) + 1; $i++) {
- if($i > 0 && $i <= $total_pages) {
- if($i == $current_page) {
- $button_display.= ' <input type="button" value="'.$i.'" class="active" /> ';
- } else {
- $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$i.'\'" value="'.$i.'" /> ';
- }
- }
- }
- //next button
- if($current_page != $total_pages) {
- $next = $current_page + 1;
- $button_display.= ' <input type="button" onClick="parent.location=\'?'.$query_string.'&pagenum='.$next.'\'" value="Next" /> ';
- } else {
- $button_display.= ' <input type="button" value="Next" disabled="disabled" /> ';
- }
- }
- $ShowingFrom = ($current_page-1) * $rows_per_page + 1;
- $ShowintTo = $current_page * $rows_per_page;
- $table_display = '<table width="100%" border="0" cellspacing="10" cellpadding="10">
- <tr>
- <td align="left"><h4>Showing Records '.$ShowingFrom.' to '.$ShowintTo.' of '.$total_rows.'</h4></td>
- <td align="right">'.$button_display.'</td>
- </tr>
- </table>';
- return array("results" => $results, "PageNavi" => $table_display);
- } else {
- //no results
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment