Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.