<!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/zebra_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     = '';
        $MySQL_username = '';
        $MySQL_password = '';
        $MySQL_database = '';

        // 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++ % 2 ? ' class="even"' : ''?>>
                <td><?php echo $row['country']?></td>
            </tr>

            <?php endwhile?>

        </table>

        <?php

        // render the pagination links
        $pagination->render();

        ?>

        <script type="text/javascript" src="jquery-1.7.2.js"></script>
        <script type="text/javascript" src="../public/javascript/zebra_pagination.js"></script>

    </body>

</html>
