PHP Classes

File: advanced_page_iterator.php

Recommend this page to a friend!
  Classes of Karsten Juul Mikkelsen  >  PageIterator  >  advanced_page_iterator.php  >  Download  
File: advanced_page_iterator.php
Role: Example script
Content type: text/plain
Description: Advanced example
Class: PageIterator
Generate page links browse a large set of data
Author: By
Last change:
Date: 18 years ago
Size: 16,104 bytes
 

Contents

Class file image Download
<?php
/*
*******************************************************
advanced_page_iterator.php

Advanced example of using the PHP class PageIterator
*******************************************************
*/
// Include the PageIterator file
$includeFolder = './';
require_once $includeFolder . 'PageIterator.inc';
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Advanced PageIterator Example</title>
    <meta name="author" id="author" content="Karsten Juul Mikkelsen" />
    <meta name="description" id="description" content="Advanced example of using the PHP class PageIterator" />
    <style type="text/css">
        body  {
        	background-color : #f7fffe;
            color : #000000;
        	font-family : Tahoma;
        }
       caption  {
            font-weight : bold;
       	    font-size : larger;
       }
       .listlabel {
            font-weight : bold;
            text-align : right;
       }
       .textmarker {
            background-color : #ffffcc;
            color : #000000;
       }
       .formstyle {
           	background-color : #cccccc;
           	border-style : solid;
    		border-width : 2;
            border-top-color : #ebebeb;
            border-left-color : #ebebeb;
            border-bottom-color : #999999;
            border-right-color : #999999;
            font-size : x-small;
        }

        .formlabel {
        	color : #185EA0;
            background-color : #cccccc;
            text-align : right;
        	font-weight : bold;
        	font-size : x-small;
        }

        .formobject {
            color : #000000;
            background-color : #ffffef;
        }        
        .accesskey {
        	text-decoration : underline;
        }
    </style>
</head>

<body>

<h1>Advanced PageIterator Example</h1>
<?php
// Create an array of circle dimensions to use as data
$circles = array();
for ($i = 0; $i < 100; $i++) {
    $circles[$i]['radius'] = ($i + 1) * 2;
    $circles[$i]['diameter'] = $circles[$i]['radius'] * 2;
    $circles[$i]['perimeter'] = number_format($circles[$i]['diameter'] * pi(), 2);
    $circles[$i]['area'] = number_format(($circles[$i]['radius'] * $circles[$i]['radius']) * pi(), 2);
}

// Set global variables if not already set
if (empty($nextAction)) $nextAction = 'listCircles';
if (empty($pageNo)) $pageNo = 1;
if (empty($perPage)) $perPage = 10;
if (empty($pageLinks)) $pageLinks = '';
if (empty($circle)) $circle = 0;

/*
*******************************************************
    This script creates two different pages
    - one for browsing a list of circle dimensions
    - one for viewing the dimensions a chosen circle
*******************************************************
*/
?>
<?php if($nextAction == 'viewCircle') : ?>
<h2>Viewing a circle</h2>
<?php
/*
*******************************************************
    View a circle
*******************************************************
*/

echo '<p>';
echo 'You have chosen circle No. ' . ($circle + 1) . '. ';
echo "In the &quot;real&quot; cyberworld this page would probably allow you to edit the circle and/or display it as an image.<br />\r\n";
echo "Here you can set options for the list of circles and return to it.\r\n";
echo "</p>\r\n";

echo '<table cellspacing="10" cellpadding="20">' . "\r\n";
echo '<tr valign="top">' . "\r\n";
echo '<td>' . "\r\n";
    // Circle details
    echo '<table cellspacing="0" cellpadding="5">' . "\r\n";
    echo '    <caption>Circle #' . ($circle + 1) . '</caption>' . "\r\n";
    echo '    <tr>' . "\r\n";
    echo '        <td class="listlabel">' . "\r\n";
    echo '            Radius:';
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
    echo '            ' . $circles[$circle]['radius'];
    echo '        </td>' . "\r\n";
    echo '    </tr>' . "\r\n";
    echo '    <tr>' . "\r\n";
    echo '        <td class="listlabel">' . "\r\n";
    echo '            Diameter:';
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
    echo '            ' . $circles[$circle]['diameter'];
    echo '        </td>' . "\r\n";
    echo '    </tr>' . "\r\n";
    echo '    <tr>' . "\r\n";
    echo '        <td class="listlabel">' . "\r\n";
    echo '            Perimeter:';
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
    echo '            ' . $circles[$circle]['perimeter'];
    echo '        </td>' . "\r\n";
    echo '    </tr>' . "\r\n";
    echo '    <tr>' . "\r\n";
    echo '        <td class="listlabel">' . "\r\n";
    echo '            Area:';
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
    echo '            ' . $circles[$circle]['area'];
    echo '        </td>' . "\r\n";
    echo '    </tr>' . "\r\n";
    echo '</table>' . "\r\n";
echo '</td>' . "\r\n";
    
echo '<td>' . "\r\n";
    // Options form
    assert(is_numeric($circle));
    assert(is_numeric($perPage));
    if (!$pageLinks) $pageLinks = '';
    else assert(is_numeric($pageLinks) and $pageLinks >= 3);
    echo '<form action="' . basename($PHP_SELF) . '">' . "\r\n";
    echo '    <table class="formstyle" cellpadding="5">' . "\r\n";
    echo '        <tr>' . "\r\n";

                  // Circle drop-down box
    echo '            <td class="formlabel">' . "\r\n";
    echo '                <label for="circle" accesskey="P">Show <span class="accesskey">p</span>age containing circle</label>' . "\r\n";
    echo '            </td>' . "\r\n";
    echo '            <td>' . "\r\n";
    echo '            <select name="circle" id="circle" class="formobject" size="1">' . "\r\n";
            $j = sizeof($circles);
            for ($i = 0; $i < $j; $i++) {
                echo '    <option value="' . $i . '"';
                if ($circle == $i) {
                    echo ' selected="selected"';
                }
                echo '>#' . ($i + 1) . '</option>'. "\r\n";
            }
    echo '            </select>' . "\r\n";
    echo '            </td>' . "\r\n";
    echo '        </tr>' . "\r\n";

    echo '        <tr>' . "\r\n";

                  // Per page drop-down box
    echo '            <td class="formlabel">' . "\r\n";
    echo '                <label for="perPage" accesskey="C">Maximum number of <span class="accesskey">c</span>ircles per page</label>' . "\r\n";
    echo '            </td>' . "\r\n";
    echo '            <td>' . "\r\n";
    echo '            <select name="perPage" id="perPage" class="formobject" size="1">' . "\r\n";
            $ppOptions = array(
                            array('value' => sizeof($circles), 'option' => '&lt;All&gt;'),
                            array('value' => '5', 'option' => '5'),
                            array('value' => '10', 'option' => '10'),
                            array('value' => '25', 'option' => '25'),
                            array('value' => '50', 'option' => '50')
                         );
            $j = sizeof($ppOptions);
            for ($i = 0; $i < $j; $i++) {
                echo '    <option value="' . $ppOptions[$i]['value'] . '"';
                if ($perPage == $ppOptions[$i]['value']) {
                    echo ' selected="selected"';
                }
                echo '>' . $ppOptions[$i]['option'] . '</option>'. "\r\n";
            }
    echo '            </select>' . "\r\n";
    echo '            </td>' . "\r\n";
    echo '        </tr>' . "\r\n";
    
                  // Page link drop-down box
    echo '        <tr>' . "\r\n";
    echo '            <td class="formlabel">' . "\r\n";
    echo '                <label for="pageLinks" accesskey="L">Maximum number of page <span class="accesskey">l</span>inks</label>' . "\r\n";
    echo '            </td>' . "\r\n";
    echo '            <td>' . "\r\n";
    echo '            <select name="pageLinks" id="pageLinks" class="formobject" size="1">' . "\r\n";
            $linkOptions = array(
                            array('value' => '', 'option' => '&lt;All&gt;'),
                            array('value' => '3', 'option' => '3'),
                            array('value' => '5', 'option' => '5'),
                            array('value' => '10', 'option' => '10'),
                            array('value' => '15', 'option' => '15')
                         );
            $j = sizeof($linkOptions);
            for ($i = 0; $i < $j; $i++) {
                echo '    <option value="' . $linkOptions[$i]['value'] . '"';
                if ($pageLinks == $linkOptions[$i]['value']) {
                    echo ' selected="selected"';
                }
                echo '>' . $linkOptions[$i]['option'] . '</option>'. "\r\n";
            }
    echo '            </select>' . "\r\n";
    echo '            </td>' . "\r\n";
    echo '        </tr>' . "\r\n";
    
                // Submit button and hidden field
    echo '        <tr align="center">' . "\r\n";
    echo '          <td colspan="2">' . "\r\n";
    echo '          <input type="submit" value="Return to the list" />' . "\r\n";
    echo '          </td>' . "\r\n";
    echo '        </tr>' . "\r\n";
    echo '    </table>' . "\r\n";
    echo '    <input type="hidden" name="nextAction" id="nextAction" value="listCircles" />' . "\r\n";
    echo '</form>' . "\r\n";
echo '</td>' . "\r\n";
echo '</tr>' . "\r\n";
echo '</table>' . "\r\n";

?>
<?php else : ?>
<h2>List of circles</h2>
<?php
/*
*******************************************************
    Viewing a list circles
*******************************************************
*/

// Create a PageIterator object for the specified page number or circle
if ($circle) {
    $paginator = new PageIterator(sizeof($circles), $pageNo, $perPage, $pageLinks, $circle);
    $pageNo = $paginator -> getCurrentPageNumber(); // $circle may cause $pageNo to change
}
else {
    $paginator = new PageIterator(sizeof($circles), $pageNo, $perPage, $pageLinks);
}

echo "<p>\r\n";

// Explain what is being shown here
echo 'Page ' . $pageNo . ' of ' . $paginator -> getPageCount() 
    . '. Circles ' . ($paginator -> getFirstElementNumber() + 1) . ' to ' 
    . ($paginator -> getLastElementNumber() + 1) . ' of ' . sizeof($circles) . ".<br /><br />\r\n";

// Print page links like << < [1] [2] [3] ... [n] > >>
if ($paginator -> getPageCount() > 1) {
    if ($pageNo == 1) {
        echo '&lt;&lt;&nbsp; &lt;&nbsp; ';
    }
    else {
        echo '<a href="' . basename($PHP_SELF) . '?pageNo=1&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks . '" target="_self" title="First page">&lt;&lt;</a>&nbsp; ';
        echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . ($pageNo - 1) . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks
        . '" target="_self" title="Previous page">&lt;</a>&nbsp; ';
    }
    
    while ($paginator -> hasNextPage()) {
        $ndx = $paginator -> nextPage();
        if ($ndx == $pageNo) {
            echo "$ndx &nbsp; ";
        }
        else {
            echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . $ndx 
                . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks
                . '" target="_self" title="Page ' . $ndx . '">';
            echo '[' . $ndx . ']</a>&nbsp; ';
        }
    }
    
    if ($pageNo == $paginator -> getPageCount()) {
        echo '&gt;&nbsp; &gt;&gt;&nbsp; ';
    }
    else {
        echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . ($pageNo + 1) . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks . '" target="_self" title="Next page">&gt;</a>&nbsp; ';
        echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . $paginator -> getPageCount() . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks . '" target="_self" title="Last page">&gt;&gt;</a>&nbsp; ';
    }
}

echo "</p>\r\n";

// Print a table of circles
echo '<table cellspacing="0" cellpadding="5">' . "\r\n";
echo '    <caption>Circle Dimensions</caption>' . "\r\n";
echo '    <tr>' . "\r\n";
echo '        <th>#</th>' . "\r\n";
echo '        <th>Radius</th>' . "\r\n";
echo '        <th>Diameter</th>' . "\r\n";
echo '        <th>Perimeter</th>' . "\r\n";
echo '        <th>Area</th>' . "\r\n";
echo '    </tr>' . "\r\n";
$trClass = '';
while ($paginator -> hasNextElement()) {
    $ndx = $paginator -> nextElement();
    if ($ndx % 2 == 0) {
        $trClass = '';
    }
    else {
        $trClass = ' class="textmarker"';
    }
    $hrefString = '<a href="' . basename($PHP_SELF) . '?nextAction=viewCircle'
                . '&amp;circle=' . $ndx
                . '&amp;perPage=' . $perPage
                . '&amp;pageLinks=' . $pageLinks
                . '" target="_self" title="View circle #' . ($ndx + 1) . '">';
    echo '    <tr align="right"' . $trClass . ">\r\n";
    echo '        <td>' . "\r\n";
        echo '        ' . $hrefString;
        echo ($ndx + 1) . '.';
        echo '</a>' . "\r\n";
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
        echo '        ' . $hrefString;
        echo $circles[$ndx]['radius'];
        echo '</a>' . "\r\n";
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
        echo '        ' . $hrefString;
        echo $circles[$ndx]['diameter'];
        echo '</a>' . "\r\n";
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
        echo '        ' . $hrefString;
        echo $circles[$ndx]['perimeter'];
        echo '</a>' . "\r\n";
    echo '        </td>' . "\r\n";
    echo '        <td>' . "\r\n";
        echo '        ' . $hrefString;
        echo $circles[$ndx]['area'];
        echo '</a>' . "\r\n";
    echo '        </td>' . "\r\n";
    echo '    </tr>' . "\r\n";
} // while
echo '</table>' . "\r\n";

echo '<p>' . "\r\n";


// Print a new set of page links at the bottom of the page
if ($pageNo == 1) {
    echo '&lt;&lt;&nbsp; &lt;&nbsp; ';
}
else {
    echo '<a href="' . basename($PHP_SELF) . '?pageNo=1&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks . '" target="_self" title="First page">&lt;&lt;</a>&nbsp; ';
    echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . ($pageNo - 1) . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks . '" target="_self" title="Previous page">&lt;</a>&nbsp; ';
}

$ndx = $paginator -> firstPage(); // Use firstPage() to reset the page pointer
    if ($ndx == $pageNo) {
        echo "$ndx &nbsp; ";
    }
    else {
        echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . $ndx 
            . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks
            . '" target="_self" title="Page ' . $ndx . '">';
        echo '[' . $ndx . ']</a>&nbsp; ';
    }

while ($paginator -> hasNextPage()) {
    $ndx = $paginator -> nextPage();
    if ($ndx == $pageNo) {
        echo "$ndx &nbsp; ";
    }
    else {
        echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . $ndx 
            . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks
            . '" target="_self" title="Page ' . $ndx . '">';
        echo '[' . $ndx . ']</a>&nbsp; ';
    }
}

if ($pageNo == $paginator -> getPageCount()) {
    echo '&gt;&nbsp; &gt;&gt;&nbsp; ';
}
else {
    echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . ($pageNo + 1) . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks . '" target="_self" title="Next page">&gt;</a>&nbsp; ';
    echo '<a href="' . basename($PHP_SELF) . '?pageNo=' . $paginator -> getPageCount() . '&amp;perPage=' . $perPage . '&amp;pageLinks=' . $pageLinks . '" target="_self" title="Last page">&gt;&gt;</a>&nbsp; ';
}
echo '</p>' . "\r\n";
?>
<?php endif; ?>
</body>
</html>
For more information send a message to info at phpclasses dot org.