Tuesday, November 15, 2011

PHP pagination Code

<?php
/*
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
        include('../config.php');
       
        // number of results to show per page
        $per_page = 5;
       
        // figure out the total pages in the database
        $result = mysql_query("SELECT * FROM latest_news");
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];
               
                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page;
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page;
                }              
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page;
        }
       
        // display pagination
       
        echo "<p><a href='latest-events.php'>View All</a> | <b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='view-paginated.php?page=$i'>$i</a> ";
        }
        echo "</p>";
         echo '<td width="37%" align="right"><a href="new.php" class="button">Add New Record</a></td>';       
        // display data in table
        echo "<table  width='100%' border='0' cellspacing='1' cellpadding='5'>";
       echo "<tr><th align='left' bgcolor='#EAEAEA' class='box-txt'>ID</th> <th align='left' bgcolor='#EAEAEA' class='box-txt'>Date</th> <th align='left' bgcolor='#EAEAEA' class='box-txt'>Heading</th> <th align='left' bgcolor='#EAEAEA' class='box-txt'>Story</th> <th align='left' bgcolor='#EAEAEA' class='box-txt'> Image</th> <th align='left' bgcolor='#EAEAEA' class='box-txt'>Edit</th><th align='left' bgcolor='#EAEAEA' class='box-txt'>Delete</th></tr>";
        // loop through results of database query, displaying them in the table
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }
       
                // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'id') . '</td>';
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'date') . '</td>';
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'heading') . '</td>';
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'story') . '</td>';
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt">' . mysql_result($result, $i, 'image') . '</td>';
               echo '<td align="left" bgcolor="#FFFFFF" class="box-txt"><a href="editnews.php?id=' . mysql_result($result, $i, 'id') . '"><img src="images/edit-icon.jpg" width="16" height="18" border="0" /></a></td>';
                echo '<td align="left" bgcolor="#FFFFFF" class="box-txt"><a href="delete.php?id=' . mysql_result($result, $i, 'id') . '"><img src="images/del-icon.jpg" width="16" height="15" border="0" /></a></td>';
                echo "</tr>";
        }
        // close table>
        echo "</table>";
       
        // pagination
       
?>
                 
               

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts