Page 1 of 1

Filter MySQL to display first 10 records per page

Posted: Wed Jul 20, 2011 8:16 am
by zacthespack
Now that I have my template set up to display the contact of a MySQL database correctly via PHP I wish to edit the PHP used so it will on the first page display the first 10 records, and then I can create a second page for the next 10 results etc etc.
The PHP used is

Code: Select all

<?php
mysql_connect('localhost', '**********', '***********') or die (mysql_error());
mysql_select_db('*********') or die (mysql_error());
$result = mysql_query("SELECT * from ********");
//Table starting tag and header cells
echo "<table border='0'><tr bgcolor='#adadad' align='center' valign='top'><th>Hire Code</th><th>Picture (click to enlarge)</th><th>Product Description</th><th>Hire Cost £ (Excl. VAT)</th>";
while($row = mysql_fetch_array($result)){
   //Display the results in different cells
   echo "<tr align='center' valign='top' bgcolor='#f0f0f0'><td>" . $row['productcode'] . "</td><td><img src='" . $row['picture'] . "' alt='image' /> </td><td>" . $row['description'] . "</td><td>" . $row['salesprice'] . "</td></tr>";
}
//Table closing tag
echo "</table>";
?>
Any help would be very useful.
Thank you very much.

Re: Filter MySQL to display first 10 records per page

Posted: Wed Jul 20, 2011 9:05 am
by social_experiment

Code: Select all

<?php $result = mysql_query("SELECT * from ******** LIMIT 0, 10"); ?>
You will have to append the sql query as show above. 10 will be constant as it is the amount of items per page but 0 will change with each page because it is the number from which to select the records. On page 2 it will be LIMIT 10, 10 on page 3 LIMIT 20, 10, etc. Search for 'pagination' in the forum as the topic have been covered a few times.