PHP Classes

File: sample.php

Recommend this page to a friend!
  Classes of Vagharshak Tozalakyan   MyPager   sample.php   Download  
File: sample.php
Role: Example script
Content type: text/plain
Description: Sample
Class: MyPager
Output navigation links to split listings in pages
Author: By
Last change: A bug in the sample script fixed.
Date: 18 years ago
Size: 1,360 bytes
 

Contents

Class file image Download
<?php

 
// Connect the database
 
mysql_connect('localhost', 'root', '') or die(mysql_error());
 
mysql_select_db('armshop') or die(mysql_error());

 
// Include class definition
 
require_once('mypager.class.php');

 
// Create MyPager object
 
$pager = new MyPager(20); // 20 items on each page
 
$pager->on_first_last = true; // show links to the first and last item
 
$pager->on_prev_next = true; // show links to the previous and next item
 
$pager->links_range = 3; // number of numeric links on the left and right from current

  // Get the total number of items
 
$res = mysql_query("SELECT COUNT(*) FROM goods") or die(mysql_error());
 
$pager->total = mysql_result($res, 0, 0);

 
// Send SELECT query. Values of $pager->offset and $pager->limit will be
  // calculated automatically.
 
$res = mysql_query("SELECT good_id, good_title_en FROM goods
    ORDER BY good_id LIMIT "
. $pager->offset . ", " . $pager->limit)
    or die(
mysql_error());

 
// Display retrieved items
 
while($row = mysql_fetch_assoc($res))
  {
   
extract($row);
    echo
"$good_id $good_title_en<br />";
  }

 
// Show informative message about the range of items currently selected.
 
list($r1, $r2) = $pager->GetRange();
  echo
"<p>Display $r1-$r2 of " . $pager->total . '</p>';

 
// Show the links bar.
 
$pager->PrintLinks();

?>