PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Oct 19, 2019 11:52 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Dec 21, 2010 2:17 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Hello,

This is a pagination class that i created and I am looking for feedback on it. I created it because pagination comes up often when i work for clients and having to constantly copy functions and amend them is becoming tedious. This class displays the paginated results from a database in a tabular form with navigation just below that.

When i created it i thought about implementing it without having the user (of the script) enter database information like username, password etc. It should be easy to incorporate into an existing system and use existing resources. The constructor accepts the arguments needed for the script to work in the following order:

1. A mysqli link identifier
2. An array with field names
3. A name of a page where the pagination will be displayed (sans an extension)
4. Table name
5. Count column (Prefrably a PK that is auto_increment)
6. The amount of items per page

Because the script uses an existing connection I suppressed all the SQL errors but if they should occur a notification message will be issued along with the mysqli_error() explanation (if any). Database connection errors in this instance is the responsibilitty of the person using the script and not of the script itself (another pluspoint IMO).

If you do test the script (a pre-emptive Thank you is in order) i'd welcome the following feedback:

Q1. Is it easy to use? I would like newbies to also use this but it might contain code that is more within the grasp of intermediate users.
Q2. How can you break it? Things like "I entered an invalid table name" or "I injected some additional sql code.." would be helpful.
Q3. How can it be improved? My goal wasn't to create the silver bullet for pagination but instead a class that can be easily implemented with minimal effort and something that would solve a common problem for many users.

I tested the script on a XAMPP server running PHP 5.2.5 and MySQL server version 3.23.32.


Attachments:
File comment: pagination class
_pagination.zip [1.86 KiB]
Downloaded 266 times

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Top
 Profile  
 
PostPosted: Tue Dec 21, 2010 1:59 pm 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California


Top
 Profile  
 
PostPosted: Tue Dec 21, 2010 11:34 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Dec 22, 2010 6:55 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Wed Dec 22, 2010 1:28 pm 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California
I think your class should concentrate less on the actual content, and more on the number links. For the content, it's my opinion that you should just generate a partial SQL query, like this:

Syntax: [ Download ] [ Hide ]
LIMIT 5, 30


That is passed back to the caller so that they can it can execute it's query, and output the content. Or even just an associative array; that way the class can be used to paginate arrays.
Syntax: [ Download ] [ Hide ]
array(
    'offset' => 5,
    'limit' => 30
);


Top
 Profile  
 
PostPosted: Wed Dec 22, 2010 2:22 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Dec 22, 2010 5:51 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Sun Jan 02, 2011 7:45 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
I've revised my previous attempt and made the following changes:

- Added error constants
- Created multiple classes, a core that contains basic functions used for pagination and 3 additional classes (extending the core class) which are geared towards certain types of pagination (SQL, SQLi and pagination using an array)
- Changed sanitize method
- Pagination can now be created from more complex queries instead of only SELECT fields FROM table
- Improved the method for aquiring the name of the page where pagination will be used

When throwing an exception, should i stick with only a generic error message or use options like mysql_error() to display more information about the error situation (for instance syntax errors in the queries)?


Attachments:
File comment: Revised pagination class.
pagination.zip [6.6 KiB]
Downloaded 250 times

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group