Juje007 » Blog


LinkedIn

Uncategorized • October 12, 2010 • By Juje007 @ 9:45 pm • • 0 Comments


Hey everyone,

Today I registered on LinkedIn and that’s why I added it to my contact page. So if you want to be a friend than just ask it :) .

My LinkedIn link is: http://be.linkedin.com/in/juje007

~Juje007

PHP: Table Class

Uncategorized • October 1, 2010 • By Juje007 @ 8:49 pm • • 0 Comments


After some playing with PHP, I wanted to create a new class but I didn’t really know what. So after some time I founded a nice task to do :) . So here is my “Table Class”. It’s a easy way to create a HTML table from a PHP array.

The code:


<?php

class Table {
    function __construct() {
        $this->output = "<table>\r";
    }

    function addSingleRow($arr) {
        if(is_array($arr)){
            $output = "\t<tr>\r";
            foreach($arr as $item) {
                $output .= "\t\t<td>" . $item . "</td>\r";
            }
            $this->output .= $output . "\t</tr>\r";
        } else {
            $this->output .= "\t<tr>\r\t\t<td>" . $arr . "</td>\r\t</tr>\r";
        }
    }

    function addMultiRow($arr) {
        foreach($arr as $item) {
        if(is_array($item)){
            $output = "\t<tr>\r";
            foreach($item as $sItem) {
                $output .= "\t\t<td>" . $sItem . "</td>\r";
            }
            $this->output .= $output . "\t</tr>\r";
        } else {
            $this->output .= "\t<tr>\r\t\t<td>" . $item . "</td>\r\t</tr>\r";
        }
        }
    }

    function render() {
        echo $this->output . "</table>";
    }
}

?>

Usage:


<?php

require("Table.class.php"); // Get the class (the name may be different)

$table = new Table; // Load the Table Class

$table->addSingleRow("1 row"); // Adds 1 row and column
$table->addSingleRow(Array("First column", "Second column"); // Adds 1 row and 2 columns

$table->addMultiRow(Array("First row", "Second row")); // Adds 2 rows with 1 column
$table->addMultiRow(Array(Array("First row and column", "second column"), Array(Array("Second row and first column", "second column"); // Adds 2 rows and 2 columns

echo $table->render(); // Output everything

?>

This is a very simple Table Class because you can’t define attributes like title, class, id, etc. But you are free to use and to change off course :) .

~Juje007

Recent Posts

Archives