Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

how to add simple pagination?

for my first extract data page,


michael heraghty

7:40 pm on Nov 22, 2007 (gmt 0)

10+ Year Member


I have just created my first database-driven page using PHP. It just takes data from a single table, and gives the user certain ways to order the data.

However, all the data is coming out on a single web page, which is too long.

Can anyone show me how to add some simple pagination? Here is the code.

Advice much appreciated (including any errors you see)!

/* This script gets listings from the database */
$db = mysql_connect("x", "y", "z");
if (!$db)
die('Could not connect: ' . mysql_error());
mysql_select_db("x", $db);

$orderby = $_GET["orderby"];
$selectcategory = $_POST[selectcategory];

/* If the user has clicked an 'ordered by' link*/
if ($orderby == 'category'):
$sql = "SELECT * FROM localfood ORDER BY category";
elseif ($orderby == 'name'):
$sql = "SELECT * FROM localfood ORDER BY name";

//if the user has chosen a category from the drop-down
elseif (!empty($selectcategory)) :
$sql = "SELECT * FROM localfood WHERE category ='".$selectcategory."'";
$sql = "SELECT * FROM localfood ORDER BY name";

/*Process the query and build the html table*/
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
print("<tr><td> ");

print("<tr><td><strong>Category:</strong> ");

print("<tr><td><strong>Address:</strong><br />");

print("<tr><td><strong>Phone:</strong> ");
printf("<tr><td><strong>Email:</strong> <a href=\"mailto:%s\">%s</a></td></tr>\n",
$row["email"], $row["email"]);
printf("<tr><td><strong>Web:</strong> <a href=\"%s\">%s</a>\n",
$row["web"], $row["web"]);
print("</td></tr>\n<tr><td><strong>Details:</strong><br />");


michael heraghty

11:43 am on Nov 23, 2007 (gmt 0)

10+ Year Member

Update: for anyone who's interested, I found this simple pagination script


[edited by: dreamcatcher at 5:09 pm (utc) on Nov. 23, 2007]
[edit reason] no urls as per T.O.S [webmasterworld.com].Thanks [/edit]


Featured Threads

Hot Threads This Week

Hot Threads This Month