CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Limit MySQL result per page (http://www.codingforums.com/showthread.php?t=248805)

dunhillx69 01-14-2012 10:10 AM

Limit MySQL result per page
 
Hi... I need some help over here... attached is my coding that will display all the result in a single page. I need to limit the result let say to 50 results per page. Anybody can help me out here? Million thanks in advance... :thumbsup:

Code:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("dbase") or die(mysql_error());

// Get all the data from the "defaulter" table
$result = mysql_query("SELECT * FROM `defaulter` WHERE `NamaMajikan` IS NOT NULL")
or die(mysql_error()); 

echo "<center>&nbsp;<br>";
echo "<table border='1' cellspacing='1' cellpadding='0'>";
echo "<tr> <th rowspan='2'>&nbsp;Kod Majikan&nbsp;</th> <th rowspan='2'>Nama Majikan</th> <th colspan='5'>Bilangan Bulanan</th> <th colspan='5'>Anggaran Amaun Tertunggak</th> <th rowspan='2'>&nbsp;Jumlah Besar&nbsp;</th></tr>";
echo "<tr><th>&nbsp;2007&nbsp;</th> <th>&nbsp;2008&nbsp;</th> <th>&nbsp;2009&nbsp;</th> <th>&nbsp;2010&nbsp;</th> <th>&nbsp;2011&nbsp;</th><th>&nbsp;2007&nbsp;</th> <th>&nbsp;2008&nbsp;</th> <th>&nbsp;2009&nbsp;</th> <th>&nbsp;2010</th> <th>&nbsp;2011&nbsp;</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>&nbsp;";
        echo $row['KodMajikan'];
        echo "</td><td>&nbsp;";
        echo $row['NamaMajikan'];
        echo "</td><td align='center'>";
        echo $row['Bil2007'];
        echo "</td><td align='center'>";
        echo $row['Bil2008'];
        echo "</td><td align='center'>";
        echo $row['Bil2009'];
        echo "</td><td align='center'>";
        echo $row['Bil2010'];
        echo "</td><td align='center'>";
        echo $row['Bil2011'];
        echo "</td><td align='center'>";
        echo $row['Amt2007'];
        echo "</td><td align='center'>";
        echo $row['Amt2008'];
        echo "</td><td align='center'>";
        echo $row['Amt2009'];
        echo "</td><td align='center'>";
        echo $row['Amt2010'];
        echo "</td><td align='center'>";
        echo $row['Amt2011'];
        echo "</td><td align='center'>";
        echo $row['JumlahBesar'];
        echo "</td></tr>";
       
}

echo "</table></center>";
?>


Kekke 01-14-2012 10:47 AM

use LIMIT

$query = "SELECT * FROM defaulter WHERE NamaMajikan IS NOT NULL LIMIT 50";

dunhillx69 01-14-2012 10:52 AM

thanks for your reply mate... but to my understanding, that will only show 50 results, am i right...

my concern is let say it will show 300 results, it will display 50 results in first page and another 50 results in all other page... that means all together will be 6 pages

:D

melloorr 01-14-2012 11:09 AM

Then just use pagination and limit the number of pages.

Kekke 01-14-2012 11:26 AM

And here you have a great example of how to do pagination:
http://www.codingforums.com/showthread.php?t=151398

12k 01-15-2012 03:11 AM

Its pretty simple.

In your query:
SELECT * FROM `blah` LIMIT Start, Amount

Start = the number of rows where to start (typically page id * amount)

Amount = How many results u want displayed. Typically 10


All times are GMT +1. The time now is 03:39 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.