Jump to content

pagination problem


slk512

Recommended Posts

how i can insert my web pages to this code given below i am beginner to php plz tell me where to make changes in this code and where to add my pages to create pagination ............... code is given below. i hve run this code and its working showing output prevoius 1 2 3 4 next but i dont know how to add my pages in this code . if anyone have other suggestion regarding how to install pagination other way plz let me know thx in advance ....

 

 

 

pagination class code.

<?php

 

  /************************************************************\

  *

  *   PHP Array Pagination Copyright 2007 - Derek Harvey

  *   www.lotsofcode.com

  *

  *   This file is part of PHP Array Pagination .

  *

  *   PHP Array Pagination is free software; you can redistribute it and/or modify

  *   it under the terms of the GNU General Public License as published by

  *   the Free Software Foundation; either version 2 of the License, or

  *   (at your option) any later version.

  *

  *   PHP Array Pagination is distributed in the hope that it will be useful,

  *   but WITHOUT ANY WARRANTY; without even the implied warranty of

  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

  *   GNU General Public License for more details.

  *

  *   You should have received a copy of the GNU General Public License

  *   along with PHP Array Pagination ; if not, write to the Free Software

  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

  *

  \************************************************************/

 

  class pagination

  {

    var $page = 1; // Current Page

    var $perPage = 10; // Items on each page, defaulted to 10

    var $showFirstAndLast = false; // if you would like the first and last page options.

   

    function generate($array, $perPage = 10)

    {

      // Assign the items per page variable

      if (!empty($perPage))

        $this->perPage = $perPage;

     

      // Assign the page variable

      if (!empty($_GET['page'])) {

        $this->page = $_GET['page']; // using the get method

      } else {

        $this->page = 1; // if we don't have a page number then assume we are on the first page

      }

     

      // Take the length of the array

      $this->length = count($array);

     

      // Get the number of pages

      $this->pages = ceil($this->length / $this->perPage);

     

      // Calculate the starting point

      $this->start  = ceil(($this->page - 1) * $this->perPage);

     

      // Return the part of the array we have requested

      return array_slice($array, $this->start, $this->perPage);

    }

   

    function links()

    {

      // Initiate the links array

      $plinks = array();

      $links = array();

      $slinks = array();

     

      // Concatenate the get variables to add to the page numbering string

      if (count($_GET)) {

        $queryURL = '';

        foreach ($_GET as $key => $value) {

          if ($key != 'page') {

            $queryURL .= '&'.$key.'='.$value;

          }

        }

      }

     

      // If we have more then one pages

      if (($this->pages) > 1)

      {

        // Assign the 'previous page' link into the array if we are not on the first page

        if ($this->page != 1) {

          if ($this->showFirstAndLast) {

            $plinks[] = ' <a href="?page=1'.$queryURL.'">«« First </a> ';

          }

          $plinks[] = ' <a href="?page='.($this->page - 1).$queryURL.'">« Prev</a> ';

        }

       

        // Assign all the page numbers & links to the array

        for ($j = 1; $j < ($this->pages + 1); $j++) {

          if ($this->page == $j) {

            $links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item

          } else {

            $links[] = ' <a href="?page='.$j.$queryURL.'">'.$j.'</a> '; // add the link to the array

          }

        }

 

        // Assign the 'next page' if we are not on the last page

        if ($this->page < $this->pages) {

          $slinks[] = ' <a href="?page='.($this->page + 1).$queryURL.'"> Next » </a> ';

          if ($this->showFirstAndLast) {

            $slinks[] = ' <a href="?page='.($this->pages).$queryURL.'"> Last »» </a> ';

          }

        }

       

        // Push the array into a string using any some glue

        return implode(' ', $plinks).implode($this->implodeBy, $links).implode(' ', $slinks);

      }

      return;

    }

  }

?>

 

 

 

2nd code :

<?

  $num_of_items = 200;

  $items_per_page = 9;

  $max_links = 10;

  if($_GET['page_num']){

  $page_num = $_GET['page_num'];

  } else {

  $page_num = 1;

  }

  ?>

<h1>PAGE: <? echo $page_num; ?></h1>

<strong>example:</strong> <br>

<strong>number of items</strong> = <? echo $num_of_items; ?><br>

<strong>number of items per page</strong> = <? echo $items_per_page; ?><br>

<strong>number of links per page</strong> = <? echo $max_links; ?><br>

<?

  ############################################ PAGINATION FUNCTION ########################################

  ############################################ PAGINATION FUNCTION ########################################

  function pagination_link($id, $page_num)

  {

 

  return $_SERVER['PHP_SELF'].'?page_num='.$page_num;

  }

  function pagination($num_of_items, $items_per_page, $id, $page_num, $max_links)

  {

  $total_pages = ceil($num_of_items/$items_per_page);

  if($page_num) {

  if($page_num >1){

  $prev = '   <a href="'.pagination_link($id, ($page_num -1 )).'">< PREV</a>   ';

  $first = '<a href="'.$_SERVER['PHP_SELF'].'"><<</a>';

  }

  }

  if($page_num <$total_pages){

  $next = '   <a href="'.pagination_link($id, ($page_num+1)).'">NEXT ></a>   ';

  $last = '   <a href="'.pagination_link($id, $total_pages).'">>></a>   ';

  }

  echo $first;

  echo $prev;

  $loop = 0;

  if($page_num >= $max_links) {

  $page_counter = ceil($page_num - ($max_links-1));

  } else {

  $page_counter = 1;

  }

 

  if($total_pages < $max_links){

  $max_links = $total_pages;

  }

  do {

  if($page_counter == $page_num) {

  echo '   <strong>'.$page_counter.'</strong>   ';

  } else {

  echo '<a href="'.pagination_link($id, ($page_counter)).'">'.$page_counter.'</a>   ';

  }

  $page_counter++; $current_page=($page_counter+1);

  $loop++;

  } while ($max_links > $loop);

  echo $next;

  echo $last;

  }

 

?>

<div align="center">

<?

  pagination($num_of_items, $items_per_page, $id, $page_num, $max_links)

  ?>

Link to comment
Share on other sites

If you want to understand how to paginate, please reply and I'll provide a link. If you just want to use the above code, insert this:

<?php
    pagination ($num_of_items, $items_per_page, $id, $page_num, $max_links);
?>

in the places where you want the page numbers to be displayed. Usually, you can display pagination on the top and/or the bottom of every page of your site.

 

Try to avoid using <? instead of <?php, because some servers don't have enabled the option to use the simplified notation.

Link to comment
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.