₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,953 members, 8,424,304 topics. Date: Thursday, 11 June 2026 at 12:35 AM

Toggle theme

Simple Pagination In PHP - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingSimple Pagination In PHP (1370 Views)

1 Reply (Go Down)

Simple Pagination In PHP by pragneshpj(op): 9:55am On Aug 24, 2016
<?php
$num_rec_per_page=10;
mysql_connect('localhost','root','');
mysql_select_db('apex1');
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM student LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
?>
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['Phone']; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = "SELECT * FROM student";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);

echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Goto 1st page

for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>
Re: Simple Pagination In PHP by paranorman(m): 4:45pm On Aug 26, 2016
Good work. Repeated call to the same program...

How about the next/previous styled pagination?
Re: Simple Pagination In PHP by babatope88(m): 6:35pm On Aug 26, 2016
This program might been working well, but, I think designing a code like this in the year 2016 is very bad. I'm not after the use of mydql_* functions or doing it in a procedural way.
Though, it is very hard to name things, I'm pretty sure, you can still do better. like me I would name this $num_rec_per_page=10; as $per_page;
You are not even afraid of SQL injection.
Re: Simple Pagination In PHP by Nobody:
babatope88:
This program might been working well, but, I think designing a code like this in the year 2016 is very bad. I'm not after the use of mydql_* functions or doing it in a procedural way.
Though, it is very hard to name things, I'm pretty sure, you can still do better. like me I would name this $num_rec_per_page=10; as $per_page;
You are not even afraid of SQL injection.
While you are very correct about his improper naming conventions, a tutorial is just meant to share the basics.. all security concerns or other unadded functionality can be added by the user. For example, most tutorials only hash passwords using MD5() which is known to be insecure, it's now up to the user to use password_hash() or any other hashing functions or mechanism available.
Re: Simple Pagination In PHP by guente02(m): 4:09am On Aug 27, 2016
That moment when u understood 50% of the op's post and you're just starting out. Great
Re: Simple Pagination In PHP by hardikt: 9:33am On Sep 13, 2016
Good Article For Beginners.. would be great if you provide code for pagination with ajax..
1 Reply

Please Help Me In This PHP Pagination CodeJquery Pagination PluginGMAIL Style Mailbox Design With PHP Pagination And Sorting234

Learn Java Programming From The Begining.Just Launched My Android Game On Google PlayOutdated content (Removed)