PDA

View Full Version : Pagination! PHP MYSQL RESULTS



Foxcom
10-02-2013, 11:10 AM
Hello guys!

I'm currently in the middle of a project and to finalize this i have to add a pagination into my MYSQL results..

I seriously have tried what i could think of and obviously it's just not enough! I'd love it if someone could
kindly fix this code so that the results would show only show 25 lines then ask for page 2 etc etc.

The code is: (ignore my messy coding)

<?php


require("header.php");


// Retrieve data from database
$sql='SELECT * FROM repairdata ORDER BY id DESC';
$result=mysql_query($sql);

?>
/// STYLE & HTML CODE ////

<?php

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<tr class='odd gradeX'>
<td width='64'><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['id']; ?></font></td>
<td width='90'><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['Status']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustName']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustPh1']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustInvoiceNumber']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustPh2']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['Product']; ?></font></td>
</tr><?php
// close while loop
} ?>

/// MORE HTML CODE ////

<?php
// close MySQL connection
mysql_close();
?>



Currently when i load the page it's showing all of my records in this database which is just over 1,000 lines. (Not Kidding)

I'm quite desperate at the moment! I'll do anything for a quick response that will fix this!

Thank you,
Chris

FlyingPigs
10-02-2013, 11:36 AM
Something like this: (haven't tested)



<?php

$limit = 15;
$page = (isset($_GET["page"])) ? (int) $_GET["page"] : 1;

$start = ($page - 1) * $limit;
$result = "YOUR QUERY HERE";
$row = mysql_num_rows($result);
$pages = ceil($row / $limit);
$counter = 1;

while($counter <= $pages){
echo "<a href=\"page_name.php?page=$counter\">$counter</a>";
$counter++;
}

$result2 = mysql_query("YOUR QUERY HERE BUT WITH: LIMIT $start, $limit");
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result2)){
?>
<tr class='odd gradeX'>
<td width='64'><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['id']; ?></font></td>
<td width='90'><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['Status']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustName']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustPh1']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustInvoiceNumber']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustPh2']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['Product']; ?></font></td>
</tr><?php
// close while loop
} ?>

/// MORE HTML CODE ////

<?php
// close MySQL connection
mysql_close();
?>


I haven't done this for a while so it may not work.

Foxcom
10-02-2013, 01:28 PM
Thank you for your input!

I have had this issue resolved by Matt,
If someone could close this it'd be great!

Thank you once again!



Something like this: (haven't tested)



<?php

$limit = 15;
$page = (isset($_GET["page"])) ? (int) $_GET["page"] : 1;

$start = ($page - 1) * $limit;
$result = "YOUR QUERY HERE";
$row = mysql_num_rows($result);
$pages = ceil($row / $limit);
$counter = 1;

while($counter <= $pages){
echo "<a href=\"page_name.php?page=$counter\">$counter</a>";
$counter++;
}

$result2 = mysql_query("YOUR QUERY HERE BUT WITH: LIMIT $start, $limit");
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result2)){
?>
<tr class='odd gradeX'>
<td width='64'><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['id']; ?></font></td>
<td width='90'><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['Status']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustName']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustPh1']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustInvoiceNumber']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['CustPh2']; ?></font></td>
<td><font size='1'><a href="booking.php?id=<? echo $rows['id']; ?>"><? echo $rows['Product']; ?></font></td>
</tr><?php
// close while loop
} ?>

/// MORE HTML CODE ////

<?php
// close MySQL connection
mysql_close();
?>


I haven't done this for a while so it may not work.

Want to hide these adverts? Register an account for free!