PHP
  Home arrow PHP arrow Page 6 - Back To Class
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Back To Class
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2000-10-05

    Table of Contents:
  • Back To Class
  • Who Needs Class, Anyway?
  • If Wishes Were Prancing Horses...
  • New Cars For Old
  • Ford's Law
  • Turning The Tables
  • Under Construction
  • Extending Yourself
  • Be My Guest

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Back To Class - Turning The Tables


    (Page 6 of 9 )

    Let's take a look at an example which illustrates how useful classes can actually be. This next class definition allows you to specify the number of rows and columns you would like, and then creates an HTML table for you on the basis of your selections.

    <? class Table { // set the rows and columns function setGrid($rows, $columns) { $this->rows = $rows; $this->columns = $columns; } // set the colours and fonts function setInterface($bcolor, $fcolor, $font) { $this->bcolor = $bcolor; $this->fcolor = $fcolor; $this->font = $font; } // draw the table function drawTable() { echo "<table border=1 bgcolor=" . $this->bcolor . ">"; for ($x=1; $x<=$this->rows;$x++) { echo "<tr>"; for ($y=1; $y<=$this->columns;$y++) { echo "<td><font face=\"" . $this->font . "\" color=" . $this->fcolor . ">" . $x . ", " . $y . "</font></td>"; } echo "</tr>"; } echo "</table>"; } } ?>
    If you take a close look, you'll see that this class contains three functions - one to set the number of rows and columns, one to decide the foreground and background colours and the font, and one to actually draw the table. Note the frequent usage of the $this keyword to reference local variables within the class.

    Now, to use this, all you need to do is include this class definition (which you could save to a separate file named "tables.inc") within your PHP script.

    <? // this is a PHP page which will contain many tables // include the class definition include("tables.inc"); ?> <html> <head> <basefont face=Arial> </head> <body>
    Here is the first table.

    <? // first table $alpha = new Table; $alpha->setGrid(4,4); $alpha->setInterface("green", "white", "Arial"); $alpha->drawTable(); ?> <p>
    And here's the second.

    <? // second table $beta = new Table; $beta->setGrid(1,9); $beta->setInterface("blue", "white", "Arial Black"); $beta->drawTable(); ?> <p>
    Third and fourth go together.

    <? // third table $gamma = new Table; $gamma->setGrid(2,4); $gamma->setInterface("black", "white", "Verdana"); $gamma->drawTable(); // fourth table $omega = new Table; $omega->setGrid(4,2); $omega->setInterface("white", "black", "Trebuchet MS"); $omega->drawTable(); ?> </body> </html>
    Pretty cool, huh?

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT