tags:

views:

355

answers:

2

Hi, This is a pagination code used for the navigation, any ideas how to get this code to display simply a numbered list of the pages as links?

if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} 
else {
   $pageno = 1;
}
if(isset($_GET['niche']))
{


$query = "SELECT count(*) FROM studies WHERE niche = '{$_GET['niche']}'";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
}


$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
$rows_per_page = 4;
$lastpage      = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
}
if ($pageno < 1) {
   $pageno = 1;
} // if
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM studies WHERE niche = '{$_GET['niche']}' $limit";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);

and...

if ($pageno == 1) {
   echo "<div class='container'>FIRST PREV ";
} else {
   echo "<div class='container'> <a href='{$_SERVER['PHP_SELF']}?pageno=1&niche={$_GET['niche']}'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage&niche={$_GET['niche']}'>PREV</a> ";
} // if
echo " ( Page $pageno of $lastpage ) ";
if ($pageno == $lastpage) {
   echo " NEXT LAST</div><br />";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&niche={$_GET['niche']}'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage&niche={$_GET['niche']}'>LAST</a></div><br /> ";
} // if
?>
+1  A: 

Try this:

$totalpages = ceil($numrows / $rows_per_page);

if($totalpages >= 1){ $pagelinkcount = 1; } else { $pagelinkcount = 0; }

while($pagelinkcount <= $totalpages && $totalpages > 1) {

     echo "<a href=\"/page/{$pagelinkcount}\">{$pagelinkcount}</a>&nbsp;";

     $pagelinkcount++;

}

On a side note, as Ian Elliot pointed out in the comments for your question, using $_GET in an SQL query leaves your database VERY vulnerable, and is thus considered an extremely insecure coding practice. You should escape and parse the $_GET data that you need diligently before passing it to the DB.

gclaghorn
A: 

Here's a function I've been using for pagination for a while. It returns nothing if there's only one page, returns up to 15 pages with numbers, then adds a dropdown that lets you skip to any 10th page when there are more than 15 pages. It relies on some prev/next images, but you can easily take that out.

function paginate( $items_per_page, $number_of_results ) {

 if( isset( $_REQUEST['page'] ) ) {
  $page = $_REQUEST['page'];
 } else {
  $page = 1;
 }

 $url = htmlentities( preg_replace( '/(\?|&)page=[\d]+/', '', $_SERVER['REQUEST_URI'] ).'&' );

 $html = '';
 $numbers_html = '';
 $navigation_html = '';
 if( $number_of_results > $items_per_page ) {
  $html .= '<div class="pagination">';
  if( $page == 1 or $page == '1' ) {
   $numbers_html .= '<img src="images/prev.png" alt="&larr; prev" class="inactive" /> - ';
  } else {
   $numbers_html .= '<a href="'.$url.'/page'.($page-1).'"><img src="images/prev.png" alt="&larr; prev" /></a> - ';
  }
  $count = 0;
  $total_pages = ceil( $number_of_results / $items_per_page )-1;
  while( $count <= $total_pages ) {
   $count++;
   if( $total_pages > 12 and floor($count / 10) != floor($page / 10) ) {
    while( $count < $total_pages and floor($count / 10) != floor($page / 10) ) {
     if( $count == 1 ) {
      $endpage = 9;
     } elseif( $count + 9 < $total_pages ) {
      $endpage = $count + 9;
     } else {
      $endpage = $total_pages + 1;
     }
     $ten_group = floor( $count / 10 );
     if( $ten_group == 0 ) {
      $navigation_html .= '<option value="'.$url.'page='.$count.'">page 1</option>';
     } else {
      $navigation_html .= '<option value="'.$url.'page='.$count.'">page '.($ten_group*10).'</option>';
     }
     $count += 10;
    }
    $count -= 2;
   } else {
    if( $page == $count ) {
     $numbers_html .= '<span class="current">'.$count.'</span>';
     if( $count == 1 ) {
      $endpage = 9;
     } elseif( $count + 9 < $total_pages ) {
      $endpage = $count + 9;
     } else {
      $endpage = $total_pages + 1;
     }
     if( $total_pages > 15 ) {
      $ten_group = floor( $count / 10 );
      if( $ten_group == 0 ) {
       $navigation_html .= '<option value="'.$url.'page='.$count.'" selected="selected">page 1</option>';
      } else {
       $navigation_html .= '<option value="'.$url.'page='.$count.'" selected="selected">page '.($ten_group*10).'</option>';
      }
     }
    } else {
     $numbers_html .= '<a href="'.$url.'/page'.$count.'">'.$count.'</a>';
    }
    if( ( $total_pages > 12 and $count % 10 == 9 ) or $count == $total_pages+1 ) {
    } else {
     $numbers_html .= ' - ';
    }
   }
  }
  if( $page != $count ) {
   $numbers_html .= ' - <a href="'.$url.'/page'.($page+1).'"><img src="images/next.png" alt="next &rarr;" /></a>';
  } else { 
   $numbers_html .= ' - <img src="images/next.png" alt="next &rarr;" class="inactive"/>';
  }
  $count++;
  $html .= '<div class="pagination_numbers">'.$numbers_html.'</div>';
  if( $navigation_html ) {
   $html .= '<div class="pagination_navigation">skip to: <select onchange="window.location=this.value">'.$navigation_html.'</select> of '.($total_pages+1).'</div>';
  }
  $html .= '</div>';
 }
 return $html;
}
sakabako