Re: SQL Result Set -> HTML Table Fragment (but Simple)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ok, I've been tinkering a little. This isn't "simple" but it's not a
lot of code either.

Basically the user supplies an array of "cell renderers" associated
with column names. Each renderer is a function name that will be called
to modify an array() representing an HTML TD element. The result is
printed to yield an HTML table. I think this is pretty extensible. You
can render columns or individual cells differently depending on the text
or position of the coloumn or cell. You can format numbers and dates,
render buttons, ... whatever you want.

The first column renderer below renders every odd column a light shade
of grey. The second renderer demonstrates formmating a timestamp. The
third renderer colors the background of an individual cell green if the
numeric value is greater than 1000.

I'm a C person myself. Can someone clean this up / improve on it (e.g. how
can I pass parameters to renderers?).

Mike

function db_cr_shade_odd($td, $i) {
    if (($i % 2) == 0) { 
        $td['style'] = "background-color: #c0c0c0;";
    } 
    return $td;
}     
function db_cr_unix_ts_mdty($td, $i) {
    if (isset($td['#text'])) {
        $td['#text'] = date("M j, Y g:i a", $td['#text']);
    }
    return $td;
}    
function db_result_print($result, $names, $renderers, $default_renderer) {
    echo "<tr>\n";
    $i = 0; 
    while ($i < mysql_num_fields($result)) {
        $meta = mysql_fetch_field($result, $i);
        $name = $meta->name;
        if (isset($renderers[$name])) {
            $renderers[$i] = $renderers[$name];
        }
        if (isset($names[$name])) {
            $name = $names[$name];
        }
        echo "<th>$name</td>\n";
        $i++;
    }
    echo "</tr>\n";
     
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "<tr>"; 
        $i = 0; 
        foreach ($row as $str) { 
            $td = array('#text' => $str);
            if ($default_renderer) {
                $td = call_user_func($default_renderer, $td, $i);
            }       
            if (isset($renderers[$i])) {
                $td = call_user_func($renderers[$i], $td, $i);
            }
            echo "<td";
            foreach ($td as $attr => $value) {
                if (substr($attr, 0, 1) != '#') {
                    echo " $attr=\"$value\"";
                }       
            }
            echo ">" . $td['#text'] . "</td>";
            $i++;
        }
        echo "</tr>\n";
    }
}    

// EXAMPLE USAGE

// custom renderer specific to invoice report
function db_cr_gt_1000($td, $i) {
    $amount = $td['#text'];
    if ($amount > 1000.00) {
        $td['style'] = "background-color: #00ff00;";
    }
    return $td;
}

// pretty column names
$names = array(
    "invoice_id" => "ID",
    "invoice_date" => "Date", 
    "invoice_amount" => "Amount",
    "invoice_transaction_id" => "Txn. ID",
    "invoice_approval_code" => "App. Code",
    "invoice_name" => "Name", 
    "invoice_email" => "Email",
    "invoice_company" => "Company");
// renderers
$renderers = array(
	"invoice_date" => "db_cr_mysql_ts_mdyt",
	"invoice_amount" => "db_cr_gt_1000");

echo "<table class=\"d\" border='0' cellpadding=\"3\">\n";
db_result_print($result, $names, $renderers, "db_cr_shade_odd");
echo "</table>\n";

-- 
Michael B Allen
PHP Extension for SSO w/ Windows Group Authorization
http://www.ioplex.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux