user2990693
user2990693

Reputation: 23

Create a simple pagination

Here's a screenshot of the page that I want to put a pagination Below is my code and I want to create a simple pagination. I tried some examples available in this site but unfortunately it doesn't work for me or I might have missed something in the code.

<?php
session_start();
$server = 'localhost';
$user = 'root';
$pass = 'root';
$connect = mysql_connect($server,$user,$pass)
or die(mysql_error());

$selectdb = mysql_select_db('des')
or die(mysql_error());

?>

<form  method="post" name="action_form" action="admin2.php">
<div id="gallery1" class="lbGallery">
<table class="table" width="100%" cellpadding="10">
            <tbody>

            <?php

            $allRecords = mysql_query('select * from cms ORDER BY id DESC limit 4');
            if(is_resource($allRecords))
            {
                while($row = mysql_fetch_assoc($allRecords))
                {
                    ?>
                    <tr><ul>
                        <td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
                        <td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
                        </ul>
                    </tr>
                    <?php
                }
            }
            ?>
          </tbody>
</table>
</div>
</form> 

Upvotes: 0

Views: 138

Answers (1)

Rob
Rob

Reputation: 149

Try this. What is does is:

  • create a contant $amount en a variable $offset
  • Create a link (next) with which sends the value of $offset back to the script
  • Catch the value in $_GET['offset'];
  • Add the value of $amount to $offset to create a new offset.
  • Run MySQL statement again with the new values for LIMIT

I didn't actualy test this code for typo's, but you'll get the general idea. Hope this is of any help.

(Best to use the new mysqli statement by te way).

<?php
            $amount = 4;
            if (!empty($_GET['offset']))
            {
                $offset = $_GET['offset'] + $amount;
            }
            else
            {
                $offset = 1;
            }



            $allRecords = mysql_query('select * from cms ORDER BY id DESC limit $amount,$offset');
            if(is_resource($allRecords))
            {
                while($row = mysql_fetch_assoc($allRecords))
                {
                    ?>
                    <tr><ul>
                        <td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
                        <td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
                        </ul>
                    </tr>
                <tr>
                <td colspan="2">
                    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?offset=<?php echo $offset;?>">Next</a>
                </td>
                </tr>
                    <?php
                }
            }
            ?>

Upvotes: 1

Related Questions