Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,759 members, 7,817,099 topics. Date: Saturday, 04 May 2024 at 05:28 AM

Simple Pagination In PHP - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Simple Pagination In PHP (1253 Views)

Please Help Me In This PHP Pagination Code / Jquery Pagination Plugin / GMAIL Style Mailbox Design With PHP Pagination And Sorting (2) (3) (4)

(1) (Reply) (Go Down)

Simple Pagination In PHP by pragneshpj: 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
?>

1 Like

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.

1 Like

Re: Simple Pagination In PHP by Nobody: 11:57pm On Aug 26, 2016
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.

1 Like

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)

Female Ethical Hackers Wanted! / ... / Breaking News. " Donald Trump Arrested' Virus Warning : Snopes.com

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.