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){ 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 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){ 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); 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|