Re: Generating Tables
| From: | Brad Hubbard | Date: | Sun, 10 Dec 2000 03:19:11 +0000 |
| Subject: | Re: Generating Tables | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-29532@lists.php.net to get a copy of this message | ||
----- Original Message -----
From: "Jeff Lacy" <jeffdl@mn.mediaone.net>
To: <php-general@lists.php.net>
Sent: Saturday, December 09, 2000 19:08
Subject: [PHP] Generating Tables
> Hello World,
>
> Does anyone know of or have a script that will generate a table? That
> would be really helpful to me right now. Thanks a lot :-)
The following may be of use, I only wrote it this morning so it will
probably "mutate" somewhat as I use it more.
Anyway, enjoy.
Brad
Congo Systems
12 Northgate Drive, Thomastown, VIC 3074
Phone (03) 9464 5981 Fax (03) 9464 5982
Mobile 0419 107 559 Email brad@congosystems.com
Website www.congosystems.com
<?php
// Table creation class
class Congo_table
{
var $m_table_format, $m_title_row_format, $m_row_array, $m_column_array;
var $m_buffer, $m_data, $m_num_data_rows, $m_num_columns = 0,
$m_num_rows = 0;
function Congo_table( $format_string, $data, $num_data_rows )
{
$this->m_table_format = $format_string;
$this->m_data = $data;
$this->m_num_data_rows = $num_data_rows;
}
function add_column( $heading, $format_string )
{
$this->m_column_array[$this->m_num_columns] = array( "heading" =>
$heading, "format" => $format_string );
$this->m_num_columns++;
}
function set_title_row_format( $format_string )
{
$this->m_title_row_format = $format_string;
}
function add_row_format( $format_string )
{
$this->m_row_array[$this->m_num_rows] = $format_string;
$this->m_num_rows++;
}
function render()
{
print "<table ".$this->m_table_format.">".
"<tr ".$this->m_title_row_format.">";
for( $i = 0; $i < count( $this->m_column_array ); $i++ )
{
print "<td
".$this->m_column_array[$i]["format"].">".
$this->m_column_array[$i]["heading"].
"</td>";
}
print "</tr>";
$rowtoggle = 0;
for( $i = 0; $i < $this->m_num_data_rows; $i++ )
{
print "<tr ".$this->m_row_array[$rowtoggle].">";
// Get a "line" from the dataset
$arr = pg_fetch_array( $this->m_data, $i );
for( $n = 0; $n < $this->m_num_columns; $n++ )
{
print "<td>".$arr[$n]."</td>";
}
print "</tr>";
// Alternate between available row formats
( $rowtoggle >= $this->m_num_rows-1 ) ? $rowtoggle = 0 :
$rowtoggle++;
}
print "</table>";
}
} // end of class Congo_table
// Usage.....
$conn = pg_connect( "host=$server port=$port user=$user password=$password
dbname=$database" );
$query = "SELECT last_updated, current_quantity, minimum_quantity,
\"re-order_quantity\", description, part_number FROM stock"
$result = pg_exec( $conn, $query );
$num = pg_numrows( $result );
// Create an instance of the class
$table = new Congo_table( $table_format_string, $result, $num );
// Define your column headings and format strings
$table->add_column( "<br>Date", "width=\"15%\"" );
$table->add_column( "Current<br>Level", "width=\"10%\"" );
$table->add_column( "Minimum<br>Level", "width=\"10%\"" );
$table->add_column( "Re-order<br>Level", "width=\"10%\"" );
$table->add_column( "<br>Description", "width=\"35%\"" );
$table->add_column( "<br>Part No.", "width=\"15%\"" );
// Define distinct title row formatting [optional]
$table->set_title_row_format( "class=\"toprow1\"" );
// Define first row formatting [optional]
$table->add_row_format( "class=\"midrow1\"" );
// Define second row formatting [optional]
$table->add_row_format( "class=\"midrow2\"" );
// Define additional row formatting...........
// Draw table
$table->render();
?>