Home arrow JavaScript arrow Page 2 - Building a Page Controller with the jQuery Quick Pagination Plug-in
JAVASCRIPT

Building a Page Controller with the jQuery Quick Pagination Plug-in


Welcome to the final installment of the series that introduces you to the jQuery Quick Pagination plug-in. Comprised of five parts, this series takes an in-depth look at the most useful features provided by this jQuery add-on and shows you how to use it either for splitting up hard-coded HTML elements, such as paragraphs and images in chunks of readable data, or for paginating sets of database records.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
December 31, 2009
TABLE OF CONTENTS:
  1. · Building a Page Controller with the jQuery Quick Pagination Plug-in
  2. · Review: web application source code so far
  3. · Building a simple page controller in PHP
  4. · Paginating user-related data

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building a Page Controller with the jQuery Quick Pagination Plug-in - Review: web application source code so far
(Page 2 of 4 )

As usual, before developing the remaining blocks of the PHP-based web application, I'd like to reintroduce the source code of its existing parts, as they were initially created in the previous chapter of the series.

So here's the SQL code that defines the table containing data about some hypothetical users:

DROP TABLE IF EXISTS `test`.`users`;

CREATE TABLE `test`.`users` (

`id` INT(4) UNSIGNED NOT NULL AUTO_INCREMENT,

`fname` VARCHAR(100) NOT NULL,

`lname` VARCHAR(100) NOT NULL,

`email` VARCHAR(100) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

That was really easy to grasp, right? So feel free to move on and look at the following picture, which shows the above table already populated with a few trivial records:

Now that the "users" table contains a group of user-related rows, the next thing you need to see is the source code of the PHP class responsible for querying the table. Here it is:

(MySQLAdapter.php)

 

<?php

 

class MySQLAdapter

{

private $_config = array();

private $_link = null;

private $_result = null;

private static $_instance = null;

private static $_connected = false;

 

// return Singleton instance of MySQLAdapter class

public static function getInstance(array $config = array())

{

if (self::$_instance === null)

{

self::$_instance = new self($config);

}

return self::$_instance;

}

 

// private constructor

private function __construct(array $config)

{

if (count($config) < 4)

{

throw new MySQLAdapterException('Invalid number of connection parameters');

}

$this->_config = $config;

}

 

// prevent cloning class instance

private function __clone(){}

 

// connect to MySQL

private function connect()

{

// connect only once

if (self::$_connected === false)

{

list($host, $user, $password, $database) = $this->_config;

if ((!$this->_link = mysqli_connect($host, $user, $password, $database)))

{

throw new MySQLAdapterException('Error connecting to MySQL : ' . mysqli_connect_error());

}

self::$_connected = true;

unset($host, $user, $password, $database);

}

}

 

// perform query

public function query($query)

{

if (is_string($query) and !empty($query))

{

// lazy connect to MySQL

$this->connect();

if ((!$this->_result = mysqli_query($this->_link, $query)))

{

throw new MySQLAdapterException('Error performing query ' . $query . ' Error : ' . mysqli_error($this->_link));

}

}

}

 

// fetch row from result set

public function fetch()

{

if ((!$row = mysqli_fetch_object($this->_result)))

{

mysqli_free_result($this->_result);

return false;

}

return $row;

}

 

// get insertion ID

public function getInsertID()

{

if ($this->_link !== null)

{

return mysqli_insert_id($this->_link);

}

return null;

}

 

// count rows in result set

public function countRows()

{

if ($this->_result !== null)

{

return mysqli_num_rows($this->_result);

}

return 0;

}

 

// close the database connection

function __destruct()

{

is_resource($this->_link) and mysqli_close($this->_link);

}

}

 

(MySQLAdapterException.php)

 

<?php

 

class MySQLAdapterException extends Exception{}

Despite its rather lengthy definition, the underlying logic implemented by many of the methods of the "MySQLAdapter" class is fairly straightforward, trust me. Essentially, all that this class does is expose to client code a clean interface. This interface is useful for connecting to the database server, executing hard-coded SQL queries and retrieving database rows. Its functionality is that simple.

And now that you understand how this MySQL abstraction class does its thing, it's time to show the static sections of the web page that will display the previous user-related database records in a paginated fashion. These will render the header and footer areas of the pertinent web page. Their respective definitions are shown below:

(header.htm)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Paginating database records with jQuery Quick Pagination plug-in</title>

<style type="text/css">

body {

padding: 0;

margin: 0;

background: #fff;

font: 1em Arial, Helvetica, sans-serif;

color: #000;

}

#wrapper {

width: 960px;

margin: 0 auto;

background: #f0f0f0;

}

#header, #content, #footer {

padding: 30px;

}

#users {

padding: 30px;

background: #c0c0ff;

border: 1px solid #000;

}

</style>

</head>

<body>

<div id="wrapper">

<div id="header">

<h1>Paginating database records with jQuery Quick Pagination plug-in</h1>

<h2>Header section</h2>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

</div>

 

(footer.htm)

 

<div id="footer">

<h2>Footer section</h2>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

</div>

</div>

</body>

</html>

Well, now that I've shown you the corresponding header and footer sections of this example web page, I've finished my summary. At this stage, the functionality of this PHP-driven application is extremely limited, as its building blocks look very disconnected. Therefore, it's necessary to start creating some additional files that put all of these elements to work together in beautiful harmony.

With that premise in mind, in the next segment I'm going to build a simple page controller in PHP. It will add a more dynamic behavior to the application, since it will perform two well-differentiated tasks: first, it will fetch user-related data from the associated MySQL table, and then it will display this information in the form of fancy HTML divs.

To learn more about how this page controller will be developed, click on the link below and read the section to come.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials