Login   Register  
PHP Classes
elePHPant
Icontem

File: examples/example2.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Stefan Gabos  >  Zebra_Pagination  >  examples/example2.php  >  Download  
File: examples/example2.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Zebra_Pagination
Show links to browse listings split in pages
Author: By
Last change:
Date: 2011-01-10 09:51
Size: 3,660 bytes
 

Contents

Class file image Download
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Zebra_Pagination, database example</title>
        <meta http-equiv="content-type" content="text/html;charset=UTF-8">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <link rel="stylesheet" href="reset.css" type="text/css">
        <link rel="stylesheet" href="style.css" type="text/css">
        <link rel="stylesheet" href="../public/css/pagination.css" type="text/css">
    </head>
    <body>
        <h2>Zebra_Pagination, database example</h2>
        <p>For this example, you need to first import the <strong>countries.sql</strong> file from the examples folder
        and to edit the <strong>example2.php file and change your database connection related settings.</strong></p>
        
        <?php
        
// database connection details
        
$MySQL_host     'localhost';
        
$MySQL_username 'root';
        
$MySQL_password '';
        
$MySQL_database 'tmp';
        
// if could not connect to database
        
if (!($connection = @mysql_connect($MySQL_host$MySQL_username$MySQL_password))) {
            
// stop execution and display error message
            
die('Error connecting to the database!<br>Make sure you have specified correct values for host, username and password.');
        }
        
// if database could not be selected
        
if (!@mysql_select_db($MySQL_database$connection)) {
            
// stop execution and display error message
            
die('Error selecting database!<br>Make sure you have specified an existing and accessible database.');
        }
        
// how many records should be displayed on a page?
        
$records_per_page 10;
        
// include the pagination class
        
require '../Zebra_Pagination.php';
        
// instantiate the pagination object
        
$pagination = new Zebra_Pagination();
        
// the MySQL statement to fetch the rows
        // note how we build the LIMIT
        // also, note the "SQL_CALC_FOUND_ROWS"
        // this is to get the number of rows that would've been returned if there was no LIMIT
        // see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
        
$MySQL '
            SELECT
                SQL_CALC_FOUND_ROWS
                country
            FROM
                countries
            LIMIT
                ' 
. (($pagination->get_page() - 1) * $records_per_page) . ', ' $records_per_page '
        '
;
        
// if query could not be executed
        
if (!($result = @mysql_query($MySQL))) {
            
// stop execution and display error message
            
die(mysql_error());
        }
        
// fetch the total number of records in the table
        
$rows mysql_fetch_assoc(mysql_query('SELECT FOUND_ROWS() AS rows'));
        
// pass the total number of records to the pagination class
        
$pagination->records($rows['rows']);
        
// records per page
        
$pagination->records_per_page($records_per_page);
        
?>
        <table class="countries" border="1">
            <tr><th>Country</th></tr>
            <?php $index 0?>
            <?php while ($row mysql_fetch_assoc($result)):?>
            <tr<?php echo ($index++) % == ' class="even"' ''?>>
                <td><?php echo $row['country']?></td>
            </tr>
            <?php endwhile?>
        </table>
        <?php
        
// render the pagination links
        
$pagination->render();
        
?>
    </body>
</html>