Zend - The PHP Company




Text

Add Code


format_page  

Type: class
Added by: flight553
Entered: 28/09/2000
Last modified: 01/12/2000
Rating: **** (3 votes)
Views: 6434
This is a class that will format an array of scalars (like strings) neatly into an arbitrary number of equal sized columns. Uses an HTML table to do this, wrapping from left to right. Modelled after the output from the format_page() SimulEfun in the Nightmare Mudlib.


<?php
/*
  format_page.php
  version 1.02 2000/10/08 22:01:00 
  by operator@superprivate.com   
  http://dvd.superprivate.com   -> DeCSS, css-auth, cyberpatrol4break, cuecat mirror

  The license for this software is free, just keep this header in place, ok?

   PHP4/MySQL hosting $10/ month at http://missoulaweb.com  with SSH, telnet 100 megs of disk 

  Changes:
  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 == 
                         

                         
$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
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories