PHP Classes

File: limit/limit.php

Recommend this page to a friend!
  Classes of Johan Barbier   SPL and Iterators   limit/limit.php   Download  
File: limit/limit.php
Role: Example script
Content type: text/plain
Description: limit example
Class: SPL and Iterators
Implements several iterators using SPL
Author: By
Last change: Added documentation
Date: 17 years ago
Size: 1,529 bytes
 

Contents

Class file image Download
<?php
/**
 * SQL access
 *
 */
DEFINE ('SQL_LOGIN', 'root');
DEFINE ('SQL_PWD', 'pwd');
DEFINE ('SQL_DB', 'dbname');
DEFINE ('SQL_HOST', 'localhost');

/**
 * Includes
 */
require_once 'package.oLimit.php';

/**
 * mysql testing
 */
/**
 * Connection
 */
$hCon = mysql_connect (SQL_HOST, SQL_LOGIN, SQL_PWD);
mysql_select_db (SQL_DB, $hCon);
/**
 * Query
 */
$sQuery = <<<sql
SELECT id FROM table
sql;
$rRes = mysql_query ($sQuery, $hCon);

try {
   
/**
     * Creating the mysqlLimit object using the factory, with arbitrary values (here, staring at offset 20, and retrieving the 10 next results)
     */
   
$limit = LimitFactory::factory ('MYSQL', $rRes, 20, 10);
   
/**
     * while loop
     */
   
while ($limit -> valid()) {
       
/**
         * Displaying some values from mysqlLimit object
         */
       
echo '<p><strong>Internal Position : ', $limit -> getInternalPos (), ' && Row Offset : ',$limit -> getExternalPos (),' => </strong></p>';
       
/**
         * Displaying current result array
         */
       
echo '<pre>', print_r ($limit -> current ()), '</pre>';
       
/**
         * Jumping to the next position
         */
       
$limit -> next ();
    }
} catch (
Exception $e) {
    echo
$e -> getMessage (), ' on line ', $e -> getLine ();
}

mysql_close ($hCon);

/**
 * array testing
 */
$aTab = range (1, 1000, 5);

try {
   
$limit = LimitFactory::factory ('ARRAY', $aTab, 150, 20);
    while (
$limit -> valid()) {
        echo
$limit -> key (), ' => ', $limit -> current (), '<br />';
       
$limit -> next ();
    }
} catch (
Exception $e) {
    echo
$e -> getMessage (), ' on line ', $e -> getLine ();
}
?>