To begin with, let’s set up a simple database to store the name of a few articles, and include one simple table. This table will contain three basic fields, called “articleid”, “name” and “site’, which reference respectively, the ID for each article, the story name, and finally the name of the site where the article was published. Sounds simple, doesn’t it? In first place, let’s create the table “articles”: CREATE TABLE articles ( articleidINTUNSIGNED NOT NULLAUTO_INCREMENT PRIMARYKEY, name VARCHAR(60) NOT NULL, site VARCHAR(60) NOT NULL ); As you can see, the table definition shows a simple structure where the first field is the table’s primary key, specified as being of the AUTO_INCREMENT type. The subsequent fields are defined to hold string values, as mentioned before: the article’s name and the site where it was originally published. What’s our next step? We need to populate the table with some data related to the articles, so let’s perform a multi-row INSERT operation, in order to fill the database table with records: INSERT INTO articles VALUES (NULL,"Regular Expressions in JavaScript","Devarticles.com"), (NULL,"Preloading HTML content withCSS","Devarticles.com"), (NULL,"Handling Events with theDOM- Part 1","Devarticles.com"), (NULL,"Handling Events with theDOM- Part 2","Devarticles.com"), (NULL,"Handling Events with theDOM- Part 3","Devarticles.com"), (NULL,"Output Caching with PHP","Devshed.com"), (NULL,"Introduction toCSSPositioning Properties Part 1","Devarticles.com"), (NULL,"Introduction toCSSPositioning Properties Part 2","Devarticles.com"), (NULL,"Introduction toCSSPositioning Properties Part 3","Devarticles.com"), (NULL,"Matching Div heights withCSSand JavaScript","Devarticles.com"), (NULL,"Customizing Styles: User-controlled Style Sheets Part 1","Devarticles.com"), (NULL,"Customizing Styles: User-controlled Style Sheets Part 2","Devarticles.com"), (NULL,"Customizing Styles: User-controlled Style Sheets Part 3","Devarticles.com"), (NULL,"Div-based layout withCSS","Devarticles.com"), (NULL,"Building friendly pop-up windows","Devarticles.com"), (NULL,"Building accessible web forms","Devarticles.com"), (NULL,"Building a Template Parser class with PHP - Part 1","Devshed.com"), (NULL,"Building a Template Parser class with PHP - Part 2","Devshed.com"), (NULL,"A quick look at Cross-Site Scripting","Devshed.com"), (NULL,"Email Address verification with PHP","Devshed.com"), (NULL,"CSSshorthand at a glance","Devarticles.com"), (NULL,"Creating pop-up notes withCSSand JavaScript","Devarticles.com"), (NULL,"Controllable Navigation bars with JavaSCript - Part 1","Devarticles.com"), (NULL,"Controllable Navigation bars with JavaSCript - Part 2","Devarticles.com"); Okay, we’ve populated some records specifying NULL values for the “articleid” field, adding the corresponding story name, as well as the name of the site where the article was posted. Now, we have some data to play with. Let’s put the classes together to display some paged results. It’s as simple as this: // include the classes require_once 'mysqlclass.php'; require_once 'pagerclass.php'; // instantiate a MySQLConnector object $db=&new MySQLConnector('host','user','password','articles'); // build query $sql="SELECT articleid,name FROM articles WHERE site='Devarticles.com'"; // instantiate a Pager object that aggregates the “MySQLConnector object $pg=&new Pager($db,$sql); // display paged result set echo $pg->displayRecords($_GET['page']); With just a few lines of code we’re in business, displaying nicely paged records. If you look at the code above, you can see that we first included the class files, and then instantiated a “MySQLConnector” object, which handles all of the operations related to MySQL. After that, we build a regular SELECT statement to retrieve all of the articles that were published at “Devarticles.com”, and instantiate a “Pager” object, passing to it the query itself and the “MySQLConnector” object. Doing so, the second object aggregates the first one, for performing internally the given query and returning a paged result. By spicing up the output with some CSS declarations, this is what I get on my browser, after executing the above script:
Probably this is not the “coolest” visual presentation, but the classes are doing their jobs quite well. We’ve obtained a decent paged result set in conjunction with all of the paging links. Isn’t aggregation remarkably powerful? I’m sure you’ll agree. Conclusion That’s all for now. Hopefully, our round trip exploring the core concepts and practical application of aggregation in PHP has been highly rewarding, and given you a more intimate grounding in the subject. However, this is only the start. There is plenty of room to experiment and find the right way to implement a powerful, well-defined object interaction in the real world. The more you learn about Object Oriented Programming, the faster your classes will get connected properly, saving you from the hard work of rewriting code. Once you have a few classes worked out, it’s as easy as building a wall with bricks. See you soon!
blog comments powered by Disqus |
|
|
|
|
|
|
|