Weborum Webmaster Forum > [Intermediate] Pagination Magic! :D
Help - Search - Members - Calendar
Full Version: [Intermediate] Pagination Magic! :D
Weborum Webmaster Forum > TUTORIAL ARCHIVE - tutorials & scripts to save you scouring the internet. Please feel free to add your own. > PHP Tutorials & scripts
Timo
I'm back with a new tutorial!

You do not need sql for this.. smile.gif

To use this tutorial you need a basic understand of queries and the LIMIT function.

This script will allow you to move forward and backwards through entries... In the future I may quickly add an addition to this to show all the pages in between and count up the pages, but for now, here goes the tutorial...

CODE
function dbInsans($text) {
$text = strip_tags ($text, "");
$text = str_replace(chr(10),"",$text);
$text = str_replace(chr(13), "<br />", $text);
$text = str_replace("\"","&quot;",$text);
$text = str_replace("'","'",$text);
$text = addslashes($text);
return($text);
}


This function will strip away many holes that hackers could use to cause an SQL Injection and such... It must go at the top of the page...

CODE

$page = dbinsans($_GET['page']);
$number_of_results = dbinsans($_GET['results']);
if (($page==NULL)||($page==0))
$page=1;
if ($number_of_results==NULL)
$number_of_results=30;
$start_from= ($page-1)*$number_of_results;
$results_final= ($page)*$number_of_results;


I'll break this up... But it must appear about the query.

$page = dbinsans($_GET['page']);
This uses GET to recieve the page number sent via the url. It runs it through our dbinsans function...

$number_of_results = dbinsans($_GET['results']);
This grabs how many results are set by the user and runs it through out dbinsans function...

if (($page==NULL)||($page==0))
$page=1;
If there is no page given or the page somehow ended up as 0, we set the page to the first page.

if ($number_of_results==NULL)
$number_of_results=30;
If the user did not set the results in the url sent, we set the results to a number... You can change the 30 to whatever you want; it's the default value.

CODE

$query = "SELECT * FROM OurQuery LIMIT $start_from, $results_final";

This is our query... You can change it to whatever you want as long as the "LIMIT $start_from, $results_final" is in our query. This can go anywhere... as long as the forementioned items appear before it.

CODE

<a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?results=<?php echo $number_of_results;?>&amp;page=<?php echo ($page-1);?>">Previus <?php echo $number_of_results;?> entries</a> || <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?results=<?php echo $number_of_results;?>&amp;page=<?php echo ($page+1);?>">Next <?php echo $number_of_results;?> entries</a>


I'll break this down... This can go anywhere at all in the script...

<a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?results=<?php echo $number_of_results;?>&amp;page=<?php echo ($page-1);?>">Previus <?php echo $number_of_results;?> entries</a>

This is a simple <a href=""></a> link with lots of coding inside of it...
The $_SERVER['SCRIPT_NAME']; grabs the name of the script being run (the script you put this in). The other values just echo the variables to embed the previous page number in the script.


<a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?results=<?php echo $number_of_results;?>&amp;page=<?php echo ($page+1);?>">Next <?php echo $number_of_results;?> entries</a>

This bunch does the same thing as the previous block of code but instead of embeding the previous page number, it embeds the next page number.


There is not that much to it... I only called it Intermediate because you'll need to know SQL to use it properly...

I have only tested this in the script I originally made it it, please post any problems if any arise.

-Tim

Edit: Added this...

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die("Unable to connect!");
mysql_select_db($dbname,$db);
$querytocheck = "SELECT * FROM OurTable";
$queryresource = mysql_query ($querytocheck,$db);
$NumRows = msql_num_rows($queryresource);
$PageCount = ceil($NumRows/$number_of_results);

for ($i=0; $PageCount>$i; $i++)
{
if ($i!=$page)
echo '<a href="'.$_SERVER['SCRIPT_NAME'].'?results='.$number_of_results.'&amp;page='.$i.'"> '.$i.' </a>';
else
echo " $i ";
}

It should display all the pages... you need to setup the query yourself... instead of the * I suggest you just pick one variable such as an ID or such so that the MySQL load is lower... smile.gif

Feel free to test out the thing under the edit... I've never tested it, just made it a second ago, so I don't know if it works... it should work though...
Daniel8_9_3
good tutorial tim:) excellent man shades2.gif

when i get an account with php im going to slap that shoutbox in as well as some other php scripts.
Timo
QUOTE (Daniel8_9_3 @ Jun 1 2004, 07:30 AM)
good tutorial tim:) excellent man shades2.gif

when i get an account with php im going to slap that shoutbox in as well as some other php scripts.

haha thanks biggrin.gif
Josh
Looks good tim thumbsupsmileyanim.gif
Timo
QUOTE (Josh @ Jun 1 2004, 02:58 PM)
Looks good tim thumbsupsmileyanim.gif

Thanks biggrin.gif

I'll have to get around to adding page links later if you want them... It's not hard, I'm just lazy... To do it... I would query the database and find out how many objects there are, then I would divide by $number_of_results, round up, and then for loop out the links while keeping the $page valued one as just text...

Ehhh I guess I'll go do it...
Timo
QUOTE (Timo @ Jun 1 2004, 03:11 PM)
QUOTE (Josh @ Jun 1 2004, 02:58 PM)
Looks good tim thumbsupsmileyanim.gif

Thanks biggrin.gif

I'll have to get around to adding page links later if you want them... It's not hard, I'm just lazy... To do it... I would query the database and find out how many objects there are, then I would divide by $number_of_results, round up, and then for loop out the links while keeping the $page valued one as just text...

Ehhh I guess I'll go do it...

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die("Unable to connect!");
mysql_select_db($dbname,$db);
$querytocheck = "SELECT * FROM OurTable";
$queryresource = mysql_query ($querytocheck,$db);
$NumRows = msql_num_rows($queryresource);
$PageCount = ceil($NumRows/$number_of_results);

for ($i=0; $PageCount>$i; $i++)
{
if ($i!=$page)
echo '<a href="'.$_SERVER['SCRIPT_NAME'].'?results='.$number_of_results.'&amp;page='.$i.'"> '.$i.' </a>';
else
echo " $i ";
}


I don't like how I did the if in the loop, but I figured that was the easiest way to do it... I didn't test this, but I think this would work... You could make it fancy by only listing the first few and then the last if you want, that wouldn't be hard at all, but I'll leave it to you. smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.