Jump to content

Php Pagination


ckrudelux

Recommended Posts

I was reading on this page about making a pagination http://www.phpeasystep.com/and I don't get it. :) Well I get it but I can't make the pieces fall in to place.Can someone explain what it's doing like first we are doing this couse we want this and then this to get that.Or some other good way of declare this for me.

Link to comment
Share on other sites

You could try my tutorial http://www.aspektas.com/blog/really-simple-php-pagination :)Otherwise, what specific parts are you having trouble with? Were there any error messages? What code did you end up with?

Link to comment
Share on other sites

You could try my tutorial http://www.aspektas.com/blog/really-simple-php-pagination :)Otherwise, what specific parts are you having trouble with? Were there any error messages? What code did you end up with?
Okay I will try it out.. And I haven't done any copy paste I'm just trying to read througt the code trying to understand it so I what it's doing. So no error massages yet.
Link to comment
Share on other sites

Okay I have been looking through your codes and I got some questions.For starters

$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;

This line makes me confused I have never seen this kind of code before but reading through some times I kind of get it.The first I get (isset) this I know what it's mean but the (?) I think it's says if not. (int) I hav know clue of that it does.and this ( : ) means what?This code was fun to look at:

$pages = implode(mysql_fetch_assoc(mysql_query("SELECT COUNT(key) FROM table")));

I have never used implode but this is really nice to have everything on one row. :)

$result = mysql_query("SELECT * FROM table LIMIT " . (($page - 1) * 6) . ", 6);

The LIMIT what does it do in this line?

Link to comment
Share on other sites

This line makes me confused I have never seen this kind of code before but reading through some times I kind of get it.
That line makes use of the ternary operator - http://www.php.net/manual/en/language.oper...parison.ternary.
I have never used implode but this is really nice to have everything on one row.
implode() converts an array into a string, by concatenating all the elements into one.
The LIMIT what does it do in this line?
In an SQL query, LIMIT restricts the number of rows that are returned. In this case it tells the query to return 6 records, starting at record number (($page - 1) * 6).
Link to comment
Share on other sites

That line makes use of the ternary operator - http://www.php.net/manual/en/language.oper...parison.ternary.implode() converts an array into a string, by concatenating all the elements into one.In an SQL query, LIMIT restricts the number of rows that are returned. In this case it tells the query to return 6 records, starting at record number (($page - 1) * 6).
Okay but in my eyes this seems strange.For the first page we get the 6 first rows from the database. But for the second page I don't get it. "(2-1), 6" this would mean I only moved down one step.
Link to comment
Share on other sites

"(2-1), 6" this would mean I only moved down one step.
$page = 1 ∴ ($page - 1) * 6 = (1 - 1) * 6 = 0 ∴ LIMIT 0, 6 ∴ will get six records, beginning at the 1st one (because row numbers in MySQL begin at 0).$page = 2 ∴ ($page - 1) * 6 = (2 - 1) * 6 = 6 ∴ LIMIT 6, 6 ∴ will get six records, beginning at the 7th one.$page = 3 ∴ ($page - 1) * 6 = (3 - 1) * 6 = 12 ∴ LIMIT 12, 6 ∴ will get six records, beginning at the 13th one.Et cetera.LIMIT n, m means "get m records, beginning at the n+1th one".
Link to comment
Share on other sites

$page = 1 ∴ ($page - 1) * 6 = (1 - 1) * 6 = 0 ∴ LIMIT 0, 6 ∴ will get six records, beginning at the 1st one (because row numbers in MySQL begin at 0).$page = 2 ∴ ($page - 1) * 6 = (2 - 1) * 6 = 6 ∴ LIMIT 6, 6 ∴ will get six records, beginning at the 7th one.$page = 3 ∴ ($page - 1) * 6 = (3 - 1) * 6 = 12 ∴ LIMIT 12, 6 ∴ will get six records, beginning at the 13th one.Et cetera.LIMIT n, m means "get m records, beginning at the n+1th one".
Ow.. I just saw what I missed the times 6 in the code :) Now everything seems to add up. Thanks for the help :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...