View Single Post
Old 05-08-2009, 10:20 PM   #1 (permalink)
frostyboy33
The Visitor
 
Join Date: May 2009
Posts: 2
Thanks: 0
frostyboy33 is on a distinguished road
Default feedback on my class please

Hi guys

this is actually my first ever post on any type of php form so i hope i am doing it right. also i am only fairly new to the php oop style of thing and would appreciate any type of feed back on what i have done thus far. anything that u think could be done to improve my class or make it more "powerful"

quick run down this class is designed to generate a very basic table

also i haven't 100% tested every possible bug just made some changes this morning before i posted

php Code:
class NewCreateTable
{
    //! will hold the current table name of the table being built
    private $current_table;
    //! will hold the number of headings
    private $heading_count;
    //! array holding all errors encountered
    private $errors = array();

    // static variables
    //! Needed in order to make this class a singleton
    private static $CreateTable;

    //! Creates a single instance of the class
    /*!
     * \return The Static member holding this class
     */

    public static function getInstance()
    {
        if(!self::$CreateTable)
        {
            self::$CreateTable = new NewCreateTable;
        }

        return self::$CreateTable;
    }

    //! Ctor only accesible through getInstance()
    /*!
     */

    private function NewCreateTable()
    {

    }

    //! main table function will manage the creation of the table
    /*!
     * \param $table_name: The name of the table
     * \param $table_headings: The table headings single array
     * \param $table_data: The table data either an array or array full of arrays
     * \return the entire table in a string
     */

    public function cBasicTable($table_name, $table_headings, $table_data)
    {
        //**************************
        $table_string = NULL;       //string for the overall table
        //**************************

        // reset particular member properties
        $this->rProperties();

        // start the table
        $this->bStart($table_string,$table_name);

        // build the headings
        $this->bHeadings($table_string, $table_headings);

        // build the data
        $this->bData($table_string, $table_data);

        // end the table
        $this->bEnd($table_string);
       
        // check for errors
        $this->checkError($table_string);

        // return whatever has been created
        return $table_string;
    }

    //! will begin the table
    /*!
     * \param $table_string: The string to create the table
     * \param $table_name: The name of the table
     */

    private function bStart(&$table_string, &$table_name)
    {
        // set the table name
        $this->current_table = $table_name;
        // start the table
        $table_string = "<table name='". $table_name ."'>";       
    }

    //! will end the table
    /*!
     * \param $table_string: The string to create the table
     */

    private function bEnd(&$table_string)
    {
        // end the table
        $table_string = $table_string."</table>";
    }


    //! will build the headings for the table
    /*!
     * \param $table_string: The string to create the table
     * \param $table_headings: Array holding the table headings
     */

    private function bHeadings(&$table_string, &$table_headings)
    {
        //**************************
        $key = NULL;                // looping key
        $val = NULL;                // looping value
        //**************************
       
        if(is_array($table_headings))
        {
            // start the row
            $table_string = $table_string."<tr>";
            // loop through the array continaing the headings
            foreach($table_headings as $key => $val)
            {
                // create the heading
                $table_string = $table_string."<th>".$val."</th>";
                // add one to the headings count
                ++$this->heading_count;
            }
            // end the row
            $table_string = $table_string."</tr>";
        }
        else
        {
            // give a meaningful error message
            $this->errors[] = "ERROR(table ".$this->current_table."): table data is not in a readable format bHeadings()";
        }
    }

    //! will decide how to build the data for the table
    /*!
     * \param $table_string: The string to create the table
     * \param $table_data: either a single array or an array full of arrays containing the data for the table
     */

    private function bData(&$table_string, &$table_data)
    {
        //**************************
        $test_for_array = NULL;     // testing varriable
        //**************************

        // check to see if $table_data it self is an array
        if(is_array($table_data))
        {
            // create a test variable to see if $table_data is a single array or an array full of arrays
            $test_for_array = $table_data[0];
            if(is_array($test_for_array))
            {
                // if it's an array full of arrays
                $this->bDataArray($table_string, $table_data);
            }
            else
            {
                // if it's a single array
                $this->bDataNotArray($table_string, $table_data);
            }
        }
        else
        {
            // give a meaningful error message
            $this->errors[] = "ERROR(table ".$this->current_table."): table data is not in a readable format bData()";
        }
    }

    //! will build the data for the table with an array holding arrays
    /*! Each sub array will represent a new row
     * \param $table_string: The string to create the table
     * \param $table_data: an array containing arrays with the data
     */

    private function bDataArray(&$table_string, &$table_data)
    {
        //**************************
        $key = NULL;                // looping key
        $row = NULL;                // looping value(is an array)
        $key2 = NULL;               // looping key
        $val2 = NULL;               // looping value
        //**************************

        // cycle through the holding array
        foreach($table_data as $key => $row)
        {
            // will check every row to make sure it will line up with headings
            if(count($row) == $this->heading_count)
            {
                // begin the row for the current record
                $table_string = $table_string."<tr>";
                // cycle through each row for there data
                foreach($row as $key2 => $val2)
                {
                    // create the string for the data
                    $table_string = $table_string."<td>".$val2."</td>";
                }
                // end the row for the current record
                $table_string = $table_string."</tr>";
            }
            else
            {
                // give a meaningful error message
                $this->errors[] = "ERROR(table ".$this->current_table."): Data count does not match heading count in function bDataArray()";
                // exit the current cycle
                break;
            }
        }
    }

    //! will build the data for the table with a single array
    /*! will create a new row after a specified amount
     * \param $table_string: The string to create the table
     * \param $table_data: an array containing the data
     */

    private function bDataNotArray(&$table_string, &$table_data)
    {
        //**************************
        $data_count = NULL;         // holds the count of the $table_data array
        $division = NULL;           // holds the result of a division
        $row = NULL;                // looping counter(overall)
        $data = NULL;               // looping counter
        //**************************


        // see how manyrecords there are all together
        $data_count = count($table_data);
        // divide all records by the number of headings
        $division = $data_count / $this->heading_count;

        // $division should be an int(whole number no decimal) to match up with number of headings
        if(is_int($division))
        {
            // varriable to hold the overall record the loop will be on
            $row = 0;
            // $row need to equal $datacount to ensure all records ahve been displayed
            while($row < $data_count)
            {
                // begin the row for the current record
                $table_string = $table_string."<tr>";
                // internal loop to loop thorugh the row's records
                for($data = 0; $data < $this->heading_count; $data++)
                {
                    // create the string for the data
                    $table_string = $table_string."<td>".$table_data[$row]."</td>";
                    // increase overall record count by one
                    $row++;
                }
                // end the row for the current record
                $table_string = $table_string."</tr>";
            }
        }
        else
        {
            // give a meaningful error message
            $this->errors[] = "ERROR(table ".$this->current_table."): Data count does not match heading count in function bDataNotArray()";
        }
    }

    //! will check to see if there are any errors
    /*! if so the table string will be destroyed
     * \param $table_string: The string to create the table
     */

    private function checkError(&$table_string)
    {
        // check to see if we have any errors
        if($this->error != NULL)
        {
            // if there are unset the current table string
            unset($table_string);
            // set the table string to be the error array
            $table_string = $this->error;
        }
    }

    //! required everytime a new table is to be created
    /*!
     *
     */

    private function rProperties()
    {
        // make sure there is no old table name
        $this->current_table = NULL;
        // set headings count to 0 as every table could have a diffrent amount of headings
        $this->heading_count = 0;
        // make sure there are no left over errors from previous tables
        $this->errors = NULL;
    }
}

Last edited by Wildhoney : 05-09-2009 at 03:18 AM.
frostyboy33 is offline  
Reply With Quote