| px | top | add code | search | signup | login | help |
<?php
/*
format_page.php
version 1.0 2000/09/27 18:00:00
by operator@superprivate.com
send me a mail if you like this code
http://dvd.superprivate.com -> DeCSS, css-auth, cyberpatrol4break mirror
Example of this code at http://superprivate.com/format.phtml
The license for this software is free, just keep this header in place, ok?
Get $10/ month PHP4/MySQL hosting with SSH, telnet 100 megs of disk at http://missoulaweb.com
*/
class format_page {
// define properties
var $tablewidth = "80%";
var $border = 0;
var $cellspacing = 0;
var $cellpadding = 8;
var $tablealign = "center";
var $tablecolor = "#FFFFFF";
var $tdcolor = "#FFFFFF";
var $rowcolor = "#FFFFFF";
var $tdalign = "center";
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 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 bgcolor='". $this->rowcolor ."'>\n";
$ret .= " <td width=". ceil(100 / $this->cellsperrow) .
"% valign=middle align=". $this->tdalign ." 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 bgcolor=". $this->tdcolor ."> </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
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.