Zend - The PHP Company




Menus & Navigation

Add Code


Page Numbers  

Type: code fragment
Added by: tebrino
Entered: 01/07/2003
Last modified: 09/12/2002
Rating: - (fewer than 3 votes)
Views: 6177
This script enables you to create page number links and previous / next links. It is useful when displaying search results or large amount of data. It also allows user to choose how many records to display per page. To use it simply replace MySQL queries with your own. With small changes you can use it with any database server. If you have any questions, comments, or if you find any bugs please feel free to contact me: Author: Ilir Fekaj Contact: tebrino@hotmail.com Created: July 01. 2003. Modified: July 01. 2003. If you like it, use it, you can also give me a credit


<?php
/*
This script enables you to create page number links and previous / next links.
It is useful when displaying search results or large amount of data.
It also allows user to choose how many records to display per page.
To use it simply replace MySQL queries with your own. With small changes
you can use it with any database server. 
If you have any questions, comments, or if you find any bugs please feel free to contact me:
Author: Ilir Fekaj
Contact: tebrino@hotmail.com
Created: July 01. 2003.
Modified: July 01. 2003.
If you like it, use it, you can also give me a credit.
*/

// config-------------------------------------
$host "localhost"//your database host
$user "username"// your database user name
$pass "password"// your database password
$db "database_name"// your database name

$filename "your_file_name.php"// name of this file
$option = array (5102550100200);
$default 25// default number of records per page
$action $_SERVER['PHP_SELF']; // if this doesn't work, enter the filename
// end config---------------------------------

$opt_cnt count ($option);

$go $_GET['go'];
if (
$go == "") {
$go $default;
}
elseif (!
in_array ($go$option)) {
$go $default;
}
elseif (!
is_numeric ($go)) {
$go $default;
}
$nol $go;
$limit "0, $nol";
$count 1;


echo 
"<form name="form1" id="form1" method="get" action="$action">rn";
echo 
"<select name="go" id="go">rn";

for (
$i 0$i <= $opt_cnt$i ++) {
if (
$option[$i] == $go) {
echo 
"<option value="".$option[$i]."" selected="selected">".$option[$i]."</option>rn";
} else {
echo 
"<option value="".$option[$i]."">".$option[$i]."</option>rn";
}
}

echo 
"</select>rn";
echo 
"<input type="submit" name="Submit2" id="Submit2" value="Go" />rn";
echo 
"</form>rn";

$connection mysql_connect ($host$user$pass) or die ("Unable to connect");
mysql_select_db ($db) or die ("Unable to select database $db");


// control query------------------------------
/* this query checks how many records you have in your table.
change this to match your own table*/
$off_sql mysql_query ("SELECT * FROM table_name") or die ("Error in query: $off_sql".mysql_error());
$off_pag ceil (mysql_num_rows($off_sql) / $nol);
//--------------------------------------------

$off $_GET['offset'];
if (
get_magic_quotes_gpc() == 0) {
$off addslashes ($off);
}
if (!
is_numeric ($off)) {
$off 1;
}
if (
$off $off_pag) {
$off 1;
}

if (
$off == "1") {
$limit "0, $nol";
}
elseif (
$off <> "") {
for (
$i 0$i <= ($off 1) * $nol$i ++) {
$e $i;
$limit "$e$nol";
$count $e 1;
}
}

// Query to extract records from database. Change this to match your own table, but leave "LIMIT $limit" part unchanged.
$sql mysql_query ("SELECT title, url FROM table_name ORDER BY title LIMIT $limit") or die ("Error in query: $sql".mysql_error());

while (
$row mysql_fetch_object($sql)) {
echo 
"$count. <a href="$row->url">$row->title</a><br />rn"// this is example, you may enter here anything you like
$count += 1;
}
echo 
"<br /><br />rn";
if (
$off <> 1) {
$prev $off 1;
echo 
"[ &lt; <a href="$filename?offset=$prev&amp;go=$go">prev</a> ] rn";
}
for (
$i 1$i <= $off_pag$i ++) {
if (
$i == $off) {
echo 
"[<b> $i </b>] rn";
} else {
echo 
"[ <a href="$filename?offset=$i&amp;go=$go">$i</a> ] rn";
}
}
if (
$off $off_pag) {
$next $off 1;
echo 
"[ <a href="$filename?offset=$next&amp;go=$go">next</a> &gt; ] rn";
}

echo 
"<br /><br />rn";
echo 
"Page $off of $off_pag<br />rn";
?>


Usage Example




Rate This Script





Search



This Category All Categories