Login   Register  
PHP Classes
elePHPant
Icontem

File: format_page.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of creep  >  format_page  >  format_page.php  >  Download  
File: format_page.php
Role: ???
Content type: text/plain
Description: This is the class itself. That's right, the class itself.
Class: format_page
format stuff in columns
Author: By
Last change:
Date: 12 years ago
Size: 4,851 bytes
 

Contents

Class file image Download
<?php
/*
  format_page.php
  by operator@superprivate.com   
  send me a mail if you like this code
  http://dvd.superprivate.com   -> DeCSS, css-auth, cyberpatrol4break mirror

  The license for this software is GPL


Changes:
  v.1.0 
  20000927 18:00:00 

  v.1.01
  20000930 . added 'background' property to have table image background (like clear pixels)
           . removed all default row or cell bgcoloring
           . made it so no "bgcolor=" will be printed at all if the 'tablecolor', 'rowcolor', 
             or 'tdcolor' prop is unset  
  v.1.02
  20001008 . added 'valign' property so u can line things up vertically in td cells across a row

*/

class format_page {


// define properties
var $tablewidth = "80%";
var $tablebackground;
var $tablecolor;
var $tablealign = "center";
var $border = 0;
var $cellspacing = 0;
var $cellpadding = 8;
var $rowcolor;
var $tdcolor;
var $tdalign = "center";
var $tdvalign;
var $cellsperrow = 2;
var $arrayofthingstoformat;
var $tdstofinish;

/* 
Define the methods

   1st method is the constructor, which has the same name
   as the class and is called on every instantiation of the class

   pass in the array of stuff to format and integer how many columns u want it in   
   like:   $f->format_page(array("a", "b", "c"), 7);
*/

function format_page($arr, $cols)  {

   $this->arrayofthingstoformat = $arr;

   if(empty($cols)) $cols = $this->cellsperrow;
   $this->cellsperrow = $cols;

   // stop if any necessary properties of this class are fubar
   $err = $this->check_fubar();
   if($err) { echo $err; return; }

   // figure out how many td cells will have to be added in when u run out of things to   
   // format, so your html table isn't broken if u want 4 columns and have 7 things.  
   // if the number of coulmns divides evenly into the number of things you have, there are
   // 0 td cells to finish.

   $this->tdstofinish = (sizeof($this->arrayofthingstoformat) % $this->cellsperrow == 0 ? 
                         0 : 
                         $this->cellsperrow - 
                               (sizeof($this->arrayofthingstoformat) % $this->cellsperrow));
   
   }  // end constructor method


function check_fubar() {

   if(!$this->arrayofthingstoformat || !is_array($this->arrayofthingstoformat) || 
      !sizeof($this->arrayofthingstoformat) ) 
      return "Error: Bad or empty array of things to format.";
                            
   if(!is_integer($this->cellsperrow) || $this->cellsperrow < 1) 
      return "Error: Non-integer, zero or negative columns argument. Try positive integers.";

   }


function printout() {

   // stop if any necessary properties of this class are fubar
   $err = $this->check_fubar();
   if($err) { echo $err; return; }

   $arr = $this->arrayofthingstoformat; // just easier to type

   $ret = "<table ".
          (empty($this->tablebackground) ? "" : "background='". $this->tablebackground ."' ").
          (empty($this->tablecolor) ? "" : "bgcolor='". $this->tablecolor."' ").
          "width='". $this->tablewidth ."' ".
          "border='". $this->border ."' ".
          "cellspacing='". $this->cellspacing ."' ".
          "cellpadding='". $this->cellpadding ."' ".
          "align='". $this->tablealign ."'>\n";

   $tdcount = 1;
   $totalcount = 0;

   // loop through all the things to format, making the TRs and TDs as you go
   for($i=0; $i<sizeof($arr); $i++) {
         if($tdcount == 1) $ret .= "   <tr ". (isset($this->rowcolor) ?
                                               "bgcolor='". $this->rowcolor ."'" : ""). ">\n";
       
         $ret .= "      <td width=". ceil(100 / $this->cellsperrow) . "% ".                
                 (empty($this->tdvalign) ? "" : "valign='". $this->tdvalign."' "). 
                 "align=". $this->tdalign ." ".
                 (empty($this->tdcolor) ? "" : "bgcolor='". $this->tdcolor."'"). ">".
                 $arr[$i] ."</td>\n";


         $tdcount++; // cell count
         $totalcount++; // running count of things to get formatted

         if($totalcount == sizeof($arr)) {
              // if ran out of things to format, make all remaining td's in this row
              for($t = 0; $t < $this->tdstofinish; $t++) 
                   $ret .= "      <td ".
                                        (empty($this->tdcolor) ? "" : "bgcolor='". $this->tdcolor."'").
                           ">&nbsp;</td>\n";
              }

         if($tdcount == ($this->cellsperrow + 1)) {
              $ret .= "   </tr>\n";
              $tdcount = 1;
              }
         } // end loop through things to format
   
   $ret .= "</table>";
   echo $ret;
   } // end printout method


} // end class definition
?>