Author Topic: PHP Paging Tutorial  (Read 3956 times)

0 Members and 1 Guest are viewing this topic.

Offline bfr

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 819
  • Rating: +4/-0
    • View Profile
    • bfr's website
PHP Paging Tutorial
« on: October 22, 2006, 07:57:00 am »
Here's a horribly tutorial that I just quickly made that I based off of some of my sloppy code:

PHP Paging Basics

About

OK, here's something I just slopped up after I made a basic paging system because I felt like it.  Well, it started like this (if you aren't interested, skip down to where it says "Introduction"):

I was typing up some spanish homework, and not having all of the needed characters, I searched Google for "unicode characters."  I found some results, and got what I needed.  But, I decided to make a PHP web page that displayed all of the unicode characters from 0 to 1423, which seemed to cover all of the important ones.  So, I made it.  But, I never made anything that was used a "paging system" before, so I decided to make it use a paging system, and, well, I had had a unicode character displayer that used a PHP paging system.  After that, I decided to make this tutorial for other who want to make a basic PHP paging system but need some help.  

Introduction

What is a "paging system"?  Well, as far as I know, it's not an official term or anything, just a way that coders refer to how parts of data can be displayed be selecting different "pages" of it.  This tutorial will hopefully teach you the basics of how to make one.  Note that I expect that you know the basics of HTML and PHP.

What We Need to Do

OK.  We want the user to click on a number, and it displays the corresponding data on that page.  For a little bonus, and because I already did it anyway, and also because many paging systems are "dynamic", or dealing with data that is usually changing in content and size, I'm going to add another feature.  This doesn't really help with making paging systems with dynamic data, since the unicode characters aren't changing and more aren't being added (well, maybe some are being added, but we're only dealing with the first 1424 ones) or being taken out, but...whatever.  

So, the thing that I'm going to add is a little textbox that lets the user input the amount of pages to use.  

The Code

It's not very large, so I might as well post it all now:

c1-->
CODE
ec1


Unicode Character List




Pages







$per = $_GET['chars'];
$p = $_GET['p'];
for($i = 1; $i <= $per; $i++){
echo ''.$i.' ';
}
echo '
';
$a = (($p-1)*1424)/$per;
$a = (int)$a;
$b = $p*(1424/$per);
$b = (int)$b;
for($i = $a; $i <= $b; $i++) {
echo $i.'. &#'.$i.'
';
}
?>


c2
ec2

Yeah, I know it's not all XHTML-compliant, the variable's aren't named very well, and it's sloppy and skimpy, but bare with me; this wasn't originally meant for a tutorial.  Maybe if I, or somebody else, feels like it, I/he/she can improce the code.

The Explanation

Blah who cares about the HTML.  Let's get to the meat of the code.  Actually, part of the HTML is actually interesting, so

let's take a look at it:

c1
-->
CODE
ec1


Pages







c2
ec2

The fieldset stuff just makes it look nice.  So yeah, we have our textbox where the number of pages, which in this case has the id and name of "chars" (oh and by the way, I just like giving it an identity and a name :)smile.gif).  Then we have our submit button, called "submit", and our hidden input thingy "p" which contains the detault page number.

So, now let's get to the fun stuff....

c1
-->
CODE
ec1
$per = $_GET['chars'];
$p = $_GET['p'];
for($i = 1; $i <= $per; $i++){
echo ''.$i.' ';
}
echo '
';
$a = (($p-1)*1424)/$per;
$a = (int)$a;
$b = $p*(1424/$per);
$b = (int)$b;
for($i = $a; $i <= $b; $i++) {
echo $i.'. &#'.$i.'
';
}
?>
c2
ec2

1.  Don't question my variable names.

2.  The explanation....

c1
-->
CODE
ec1$per = $_GET['chars'];c2
ec2 <--$per contains the number of pages to use
c1
-->
CODE
ec1$p = $_GET['p'];c2
ec2 <--$p contains the current page

c1
-->
CODE
ec1
for($i = 1; $i <= $per; $i++){
echo ''.$i.' ';
}
echo '
';
c2
ec2

This displays all of the links that let the user choose which page to view.  

c1
-->
CODE
ec1
$a = (($p-1)*1424)/$per;
$a = (int)$a;
$b = $p*(1424/$per);
$b = (int)$b;
c2
ec2

OK, now here is the juicy stuff.  The first line of code above makes $a contain the beginning of the data, or the unicode character number, to display. It contains the page minus one because to get the first page, it would start with the "0th" piece of data, but I personally would rather click a "1" for the first page than a "0."  It is multiplied by 1424 because 1424 is the amount of unicode characters.  It is divided by $per because that is the amount per page.  That was probably kind of confusing, but thing about it like this, if it helps:

You have ten apples (or 1424 unicode characters), and you divide them into five groups (or $per pages), and put each of the groups next to each other.  There are two apples per group (1424/$per would equal 2 if $per equalled 1212). To access the first group, you would access the (1-1)*(10/5) apple, or the 0th apple.  The next group wold start with the (2-1)*(10/5) apple, or 2nd apple.  

well, I hope that little analogy helped.  If it didn't, then I'm sorry.  Somebody else might be able to explain it better.  

The second line in the code right above, or c1
-->
CODE
ec1$a = (int)$a;c2
ec2, converts a to an integer in case it's a fraction,

because the is no unicode character number 4.562345435 or something like that.  

The third line, or c1
-->
CODE
ec1$b = $p*(1424/$per);c2
ec2 contains the end of that page.  If you noticed, I didn't subtract 1 from

$p that time, because it contains the end of that page, or the beginning of the next section.

After that, I converted $b into an integer in case it was a fraction.    

The following:

c1
-->
CODE
ec1
for($i = $a; $i <= $b; $i++) {
echo $i.'. &#'.$i.'
';
}
c2
ec2

displays all of the unicode characters.

Conclusion

Well, I hope you learned something about the basics of PHP "paging."  Again, note that this code wasn't optimized, was

sloppy, etc., but it should demonstrate the basics of PHP paging.  

Happy coding!  :)smile.gif

So, what do you all think of it?

EDIT:  btw, this is just me ghetto way of paging.  I didn't read tutorials on this and didn't ask for help, so there might be a better way to do this.