Paginaiton help
Posted: Fri Nov 13, 2009 8:29 pm
Hey all.
As my first post, I want to say it's great to be a part of this community.
I am currently just needing some help in creating a pagination effect based off this. I've been talking to some people about this idea, as my original approach was using a mysql database, but after looking at the site, they suggested trying a "front controller pattern", maping to a directory in the filesystem and the page as a document within that filesystem eg:
Now this is where the hard part comes in. I've been playing around with this pagination tutorial which is fine when using a database, but I was given this code as an example to start off with creating pagination that doesn't use a database:
And now I'm back to being confused. The include function seems to grab variables from somewhere (I'm asssuming the pagination function), that makes sense, but with the pagination tutorial I listed above it uses a database, begining with calling the db and accessing table rows which makes it slightly unrelatable for me.
Now if I am using the way this person suggested none of this is still relevant for the approach I am taking, correct? I mean all it's there for is to link up to a database, and display how many rows from the selected table to display, but what I'm wanting is to access files in for example, specified dir, for example, the "elaine" directory.
The next part of this code makes abit more sense to me as once I get the total pages of a specified directory, things like $currentpage will fall into place.
Lastly, the actual pagination navigation.
Now I understand that the current page variable's use wont change too much in the sense that it be used for numbering the html docs 1 to 7 inside the elaine dir . But one thing I need to know is what kind of function would you write to ensure that it is accessing docs 1 through X within the specified directory? As without accessing table rows in the database approach $currentpage and $totalpages could be anywhere.
Thanks all for your time.
As my first post, I want to say it's great to be a part of this community.
I am currently just needing some help in creating a pagination effect based off this. I've been talking to some people about this idea, as my original approach was using a mysql database, but after looking at the site, they suggested trying a "front controller pattern", maping to a directory in the filesystem and the page as a document within that filesystem eg:
Code: Select all
wwwroot/
|
|
---stories/
|
|
---elaine/
| |
| |
| ---1.html
| 2.html
| 3.html
|
|
---tremain/
|
|
---1.html
2.html
...
Code: Select all
// page design code here
<div id="story">
<?php
include("stories/$_GET['story']/$_GET['page']");
// Pagination code goes here
?>
Code: Select all
// database connection info
$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM numbers";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
The next part of this code makes abit more sense to me as once I get the total pages of a specified directory, things like $currentpage will fall into place.
Code: Select all
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
Code: Select all
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
Thanks all for your time.