Manila Bulletin Online
Nav Bar   Mon Jan 23,2006 Navigation Nav Bar
spacer
 
spacer
spacer
spacer
spacer
spacer
spacer



 
spacer
Hardcode
spacer


More than just a guestbook 2 - By Albert Tagaban

In my article published last week, I mentioned that we can still do a few more things with the guestbook aside from displaying guestbook entries with alternating colors. We can add another feature, namely, displaying partial guestbook entries, to avoid displaying a very long list of entries that will probably not be read at all. We can accomplish this with pagination.

Pagination is not a new thing in the World Wide Web and you probably come across this kind of feature regularly.  Pagination can be found on message boards that you can find in every community website such as Philweavers and Grabeh.com.  Most of the time, it is on the top right--just above the first post on a specific topic.  What you can see is a number beginning with 1 and it is incremented by one until it reaches the last possible page.

 

Another thing that can be implemented is the use of a next-and-previous link in place of the incremented number.  This one is a lot easier to implement than the first—pagination--as it won’t require you to loop from one to the last possible page; and you only need to know which page is the current page the user is trying to access. 

 

A third thing that you can implement is combining both pagination and the next-and-previous link.  Now, you get three implementations to choose from and you just have to choose one that you think is suitable for your needs.  In my case, I usually choose the third option.

 

Let’s start implementing pagination.  Here are simple steps to achieve it:

 

1. Determine the current page that the user is currently viewing;

2. Determine the number of possible pages;

3. Determine the previous page and display the link;

4. Loop through from 1 to the last possible page and display the page;

5. Determine the next page and display the link;

6. Query the database and get only a portion of guestbook entries; and,

7. Display the guestbook entries for the current page.

 

We start with the page that the current user is viewing.  Assuming that you link all the numbers and the next-and-previous link like this:

 

<a href=”guestbook.php?page=n”>

 

where guestbook.php is the script to be executed and n is the current page, we can set a variable representing the page and, for the sake of this example, I name the variable $page where the value is equal to ($_GET[‘page’]).

 

If the register global is on, then we don’t need to assign the ($_GET[‘page’]) to $page because a variable of the same name is already set.  But for security purposes I recommend that even if the register global is on, assign the HTTP GET variables to a variable probably with the same name (just make sure to validate it properly).  A simple way to validate it is to require a single line of code that you can use in place of the line where you assign ($_GET[‘page’]) to a variable.

 

Here is how you do it:

 

$page = (is_numeric($_GET[‘page’])) ? $_GET[‘page’] : 1

 

The snippet above will ensure that the HTTP GET variable page is numeric; but if it’s not, then the value is 1.

Now, we move on to the next step. We need to query the database to get how many entries there are, and then we divide that with the number of entries we want to show for each page.  There are times that the result is not an integer and of a float type.  And if you get a value 10.xx the page should be 11, and we can’t use the native PHP function round because it will also create a problem if the value can’t be rounded off to a higher value; it’ll return the absolute value instead.

 

We can solve that by using three lines of code.  Of course, you can try another way which you think is better, but here is how I do it:

 

$total = round(count($totalguestbookentries)/$pagelimit)

 

$totalpageorg = count($totalguestbookentries)/$pagelimit

 

if($total < $totalpageorg) $total++

 

In the above snippet, $pagelimit is the number of guestbook entries per page which you can set to any number of guestbook entries you want to display per page.  $totalguestbookentries is the total guestbook entries that you can get after you query the database and loop through the dataset.

 

You can use this query:

 

SELECT count(guestbookid) as totalentries from guestbooktable

 

where guestbookid is the unique ID of the table.

 

The third step is a lot easier since we only need to determine if the current page is not the first page.  So, using the variable page we set in the first step, we will use an if-then-else statement to check that $page is not equal to 1.  If so, then we set a variable $previous with a value equal to $page; then we display the link (e.g., <a href=”guestbook.php?page=<?=$previous;?>”>Previous</a>)

 

The fourth step will require us to use a loop from 1 to the total page possible that we set in the second step.  A simple for-loop will do the trick; just take note that you don’t need to display a page link to the current page.

 

The fifth step is the same as the third step.  The difference is that you’ll do the opposite.  Instead of checking for the first page, you have to check that the current page is not the last page.  If so, then set a variable $next with a value equal to $page++.  Then we display the link the same way we did in the third step and replace $previous with $next.

 

The sixth step will require you to query just a portion; therefore you have to slightly change the query you use to get all the guestbook entries you displayed.  The trick is to use the limit.  A sample query to achieve this is: SELECT * from guestbooktable limit $startofpage, $pagelimit.  The $startofpage can be determined with the following snippet:

 

$startofpage = ($page-1) * $pagelimit

 

Soon after you get the query, you’re set to display the guestbook entries. A guestbook snippet is also available in my blog (http://www.alberttagaban.com).

 

You’ll see all the snippets mentioned in this article.

 

Again, comments can be sent to albert.tagaban@gmail.com

Printer Friendly Version spacer Email to a friend
 

spacer
OTHER TECHNEWS NEWS
spacer
spacer
spacer
spacer
 

spacer




HOME | SUBSCRIBE | ADVERTISE | CONTACT US | SEARCH | ARCHIVE | FEEDBACK

FEATURES: MB WAP | MB Mobile Edition | Desktop Headlines

SECTIONS: MAIN NEWS | BUSINESS | OPINION & EDITORIAL | SPORTS | YOUTH & CAMPUS | ENTERTAINMENT | AGRICULTURE | INFOTECH | HEALTH | TOURISM | SOCIETY | METRO & NATIONAL NEWS | PROVINCIAL NEWS | MOTORING SECTIONS | SCHOOLS COLLEGES AND UNIVERSITIES | WELL BEING | TECHNEWS | TASTE | WEDDINGS | I | BOARD PASSERS |

LINKS: PHILIPPINE PANORAMA | TEMPO | CLASSIFIED ADS ONLINE | USER PRIVACY POLICY

Copyright © 2001-2005, Manila Bulletin. All Rights Reserved.

designed and developed by
Alchemy Solutions