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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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