Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introduction to Aggregation, part 3

Refactoring the “displayRecords()” method - PHP

In the third part of his series, Alejandro Gervasio digs a bit further into object-oriented PHP. He explains the barebones of a paging class and techniques used to aggregate a new “MySQLConnector” class.

TABLE OF CONTENTS:
  1. Object Interaction in PHP: Introduction to Aggregation, part 3
  2. Moving back and forth: building a paging class
  3. Refactoring the “displayRecords()” method
  4. Completing the refactoring process: building the paging links
By: Alejandro Gervasio
Rating: starstarstarstarstar / 18
June 08, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Width no doubt, this method stand on its shoulders the hard work of the class, since it must perform the proper SQL query against the database, retrieve the records for being displayed, and finally create the paging links. What a though work! However, due to the great capabilities that aggregation fully brings to this method, all of those tasks are performed with minor complications. This significantly reduced overload is a straight consequence of having at our disposal the methods offered by the “MySQLConnector” object.

Thus, let’s take a look at the original definition for this method, so we’re able to understand how it does its thing. Here’s our first glance at its structure, to perform the SQL query and generate the dynamic output from the database records:

function displayRecords($page){
  // calculate total number of records using MySQLConnector object
  if(!$totalRecs=$this->db->getNumRows($this->db->performQuery($this->query))){
    die('Cannot retrieve records from database');
    }
  // calculate number of pages
  $numPages=ceil($totalRecs/$this->numRecs);
  // validate page pointer $page
  if(!preg_match("/^\d{1,2}$/",$page)||$page<1||$page>$numPages){
    $page=1;
    }
  // retrieve result set using MySQLConnector object
  $this->db->performQuery($this->query.' LIMIT '.($page-1)*$this->numRecs.','.$this->numRecs);
  while($rows=&$this->db->fetchRow()){
    foreach($rows as $row){
    $this->output.=$row.'&nbsp;';
    }
  $this->output.='<br />';
  }
}

The method accepts a single parameter $page, which simply behaves as a page pointer to determine how to display the paged records and build up the corresponding paging links. Certainly, this logic has already been implemented in previous articles that cover completely paging mechanisms. In order to get a better understanding, you may want to read the previous articles for a full review of this technique.

However, let’s focus our attention now on the rest of the method’s code. The methods really need to calculate the total number of records returned by the query, to create the paging links. Remember that our “MySQLConnector” object is available inside the class? Good, because we use its own methods, that is, “getNumRows()” and “performQuery()”, to calculate the number of records and execute the SQL query. Isn’t it great? Please notice the below lines:

// calculate total number of records using MySQLConnector object
if(!$totalRecs=$this->db->getNumRows($this->db->performQuery($this->query))){
    die('Cannot retrieve records from database');
}

The above code obtains the total number of records, using the methods generously offered by the MySQLConnector object. In first instance, the method perform the SQL query invoking directly to “performQuery()”, as listed below:

$this->db->performQuery($this->query)

And next, the expression is completed by calling the method “getNumRows()”, like this:

$totalRecs=$this->db->getNumRows($this->db->performQuery($this->query))

At this point, I guess you’ve grasped the underlying idea behind Aggregation. In both expressions we’re using the methods of the “MySQLConnector object (referenced as $this->db) to fit the purposes of the a “Pager” object. That’s what I’d call it a good friend!

The next section of the code validates the page pointer to make sure is an appropriate value, avoiding some possible tampering attempts directly manipulating URL’s, in the following manner:

if(!preg_match("/^\d{1,2}$/",$page)||$page<1||$page>$numPages){
    $page=1;
}

The expression checks to see the value of the $page pointer. If its value is lesser than 1 or greater than the number of pages needed to show the whole result set, then it’s simply set to 1.

The rest of the code uses again the methods of the “MySQLConnector” object, “performQuery()” and “fetchRow()” respectively, in order to get a dynamic paged result set adding a LIMIT clause to the original query, lastly looping over the records to generate the final output:

$this->db->performQuery($this->query.' LIMIT '.($page-1)*$this->numRecs.','.$this->numRecs);
while($rows=&$this->db->fetchRow()){
    foreach($rows as $row){
        $this->output.=$row.'&nbsp;';
   
    }
    $this->output.='<br />';
}

Notice that once the records are stored in the class property $this->output, we apply to them some kind of basic visual formatting. However, this might be quickly modified to accept a rather more polished visual presentation.

Okay, let’s hold our breath and recapitulate about what we’ve done until now. The method has already partially generated the dynamic output, by retrieving database records, using only the methods provided by the “MySQLConnector” object. Is there something pendent in our task list? Sure! We need to complete the source code, adding the section that builds the paging links. Let’s not waste more time and face properly that part.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: