NYCPHP Meetup

NYPHP.org

[nycphp-talk] PEAR HTML_Table

Jose Villegas jv_nyphp at duikerbok.com
Fri Sep 10 19:57:56 EDT 2004


On Sep 10, 2004, at 7:28 PM, John Corry wrote:

> Can anyone help me with this? I'm struggling through another PEAR 
> package's
> documentation and can't find what I need.
>
> The Table package looks like it will do what I need to do, but I can't 
> find
> an example of quite what I'm looking for.
>
> I have an array of thumbnail images.
>
> I want to build a table 3 columns and however many rows are required.
>
> The reason I am even using PEAR at all is for the autofill 
> capability...I
> was having a hard time figuring out how to deal with a last row that 
> can
> have from 1 to 3 cells.
>
> All the examples I can find show building the table from a 
> multidimensional
> array...is there a way to just specify number of cols, an array of 
> data and
> get back a table?
>
> Thanks,
> John
>

Hey John,

Don't know about PEAR, but below's a class I set up for that purpose. 
It doesn't generate the <table> and </table> tags, just the rows.
When you create the object, give it a cell template, a template for 
empy cells and the max number of columns. Then give it an array of 
values with the "parse" method. I made a few changes before pasting it 
on here, so tell me if you have any problems with it.

Hope that helps,
-jose
www.josevillegasdesign.com

******

Your cell templates could look like this:
$cellTemplate = '<td><img src="images/{image-name}.jpg"><br>
{description}</td>
';
$emptyTemplate = '<td></td>
');

Your array of values would look like this:
$values = array(
	array(
		'image-name' => 'sunset',
		'description' => 'Florida Sunset'
	),
	array(
		'image-name' => 'palm-trees',
		'description' => 'Florida Palm Trees'
	)
);

You would create your rows like this:
$trg = new TableRowGenerator($cellTemplate, $emptyTemplate, 3);
$tableRows = $trg->parse($values);

******

class TableRowGenerator
{
	var $cellTemplate = null;
	var $emptyCellTemplate = null;
	var $maxColumns = 1;
	var $rowPrefix = '<tr>
';
	var $rowSuffix = '</tr>
';
	var $cellDivider = '';
	
	// Private properties.
	var $nextColumn = 1;
	
	function TableRowGenerator($cellTemplate, $emptyCellTemplate, 
$maxColumns)
	{
		$this->cellTemplate = $cellTemplate;
		$this->emptyCellTemplate = $emptyCellTemplate;
		$this->maxColumns = $maxColumns;
	}
	
	function parse($values)
	{
		if (!$values) return;
		$valuesCount = count($values);
		if ($valuesCount == 0) return;

		// Set up tag elements.
		$prefix = '{';
		$suffix = '}';

		$mod = $valuesCount % $this->maxColumns;
		if ($mod != 0)
		{
			$emptyCellCount = $this->maxColumns - $mod;
			for ($i = 0; $i < $emptyCellCount; $i++) $values[] = null;
		}

		$result = '';
		foreach($values as $valueSet)
		{
			// Add row prefix if this is the first cell in the row.
			if ($this->nextColumn == 1) $result .= $this->rowPrefix;
			
			// Assign appropriate cell template.
			if ($valueSet)
			{
				$setResult = $this->cellTemplate;
				foreach($valueSet as $key => $value)
					$setResult = str_replace($prefix . $key . $suffix, $value, 
$setResult);
			}
			else $setResult = $this->emptyCellTemplate;
			$result .= $setResult;

			if ($this->nextColumn == $this->maxColumns)
			{
				$result .= $this->rowSuffix;
				$this->nextColumn = 1;
			}
			else
			{
				$result .= $this->cellDivider;
				$this->nextColumn++;
			}
		}
		return $result;
	}
}




More information about the talk mailing list