Jump to content

First pagination script


graham23s

Recommended Posts

Hi Guys,

 

i decided to write a pagination script, after reading up quite a bit on them (i wanted to master it fairly quickly as its always been a thorn in my side lol) this code works kind of, it displays the page number links only after i click nect, when i click previous it makes a link disappear

 

code:

 

<?php

// database connection //
include("db_connection.php");

// pagination script start //

// number of records to display per page //
$display_per_page = 10;

// check the required number of pages //
if(isset($_GET['page'])) {

  $number_of_pages = $_GET['page'];

} else {

  // query for the number of pages //
  $query = "SELECT COUNT(*) FROM `files`";
  $result = mysql_query($query);
  
  // put the number of results in an array //
  $row = mysql_fetch_array($result);
  
  // this contains the overall TOTAL number of results in the db //
  $total_number_of_results = $row['0'];
  
  // calculate number of pages //
  if($total_number_of_results > $display) {
  
   $number_of_pages = ceil($total_number_of_results/$display_per_page);
  
  } else {
  
   $number_of_pages = 1; 
  
  }
   
} // end of number of pages if //

// now determine the start of the results //
if(isset($_GET['page'])) {

  $start_page = $_GET['page'];

} else {

  $start_page = 0;

}

// do another query this time use the variables in the limit clause //
$query = "SELECT * FROM `files` LIMIT $start_page, $display_per_page";
$result = mysql_query($query);

// loop out the results //
while($row = mysql_fetch_array($result)) {

   $id = $row['id'];

   echo ("$id");
   echo ("<br />");

}

   echo ("<hr />");

// 10 results displayed great //

// now make the links //
if($number_of_pages > 1) {

// find out what exactly the page the user is on [start page say 2 x 10 +1] //
$current_page_the_user_is_on = ($start_page/$display_per_page) + 1;

// if its not the first page we need a previous button //
if($current_page_the_user_is_on != 1)

// echo a previous button //

// vars //
$prev = ($number_of_pages - 1);
$next = ($number_of_pages + 1);

echo ("<a href=\"index.php?page=$prev\">PREV</a> ");

}

// make all the numbered pages //
for($i = 1; $i <= $number_of_pages; $i++) {

echo ("<a href=\"index.php?page=$i\">$i</a> ");

}

// finally make the next link //
if($current_page_the_user_is_on != $number_of_pages) {

echo ("<a href=\"index.php?page=$next\">NEXT</a> ");

}
?>

 

is there any improvements i could make or things i haven't done, any input would be appreciated

 

cheers

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/83770-first-pagination-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.