PHP Classes

File: make_table.php

Recommend this page to a friend!
  Packages of Faheem Ahmed   Array to Table Grid   make_table.php   Download  
File: make_table.php
Role: Class source
Content type: text/plain
Description: Its a function only
Class: Array to Table Grid
Display array data in an HTML table
Author: By
Last change: now i have made it available as class
Date: 17 years ago
Size: 2,440 bytes
 

Contents

Class file image Download
<?php
class maketable
{
    function make_table($data,$columns=1)
    {
        #################### REQUIRED INPUT ####################
        /*
        1. REQUIRED
        $data should be an array, and array key must be 
        an integer starting with 0 and must contain 
        further iteration in sequence. For example
        
        $data[0] = "any value";
        $data[1] = "any value";
        $data[2] = "any value";
        $data[3] = "any value";
        
        2. REQUIRED
        $columns must be a variable
        $columns must have integer value greater than 0
        */
        #################### REQUIRED INPUT ####################
    
        $no_of_cells = count($data);
        $no_of_rows = ceil($no_of_cells/$columns);
        $no_of_total_cells = $columns*$no_of_rows;
        $extra_cells = $no_of_total_cells-$no_of_cells;
        
        #################### SUMMARY FOR DEBUGGING ####################
        #    echo "Number of columns: $columns<br>";
        #    echo "Number of rows: $no_of_rows<br>";
        #    echo "Number of data Cells: $no_of_cells<br>";
        #    echo "Number of Extra Cells: $extra_cells<br>";
        #    echo "Number of Total Cells: $no_of_total_cells<br>";
        #################### SUMMARY FOR DEBUGGING ####################
        
        $key = 0;    # THIS VARIABLE WILL BE INCREMENTED ON EARCH CELL
    
        $HTML = "<table class=\"make_table\">";
        for($i=0;$i<$no_of_rows;$i++)    # THIS LOOP WILL GENERATE TABLE ROWS
        {
            $HTML .= "<tr>";    # START TABLE ROW
            
            for($j=0;$j<$columns;$j++)    # THIS LOOP WILL GENERATE TABLE CELLS
            {
                if(isset($data[$key]))    # IF DATA CELL EXISTS
                {
                    $HTML .= "<td class=\"make_table_td\">";    # START TABLE CELL
                    $HTML .= $data[$key];
                    $HTML .= "</td>";    # END TABLE CELL
                }
                else
                {
                    $HTML .= "<td class=\"make_table_td\">";    # START TABLE CELL
                    $HTML .= "&nbsp;";    # $data[$key];
                    $HTML .= "</td>";    # END TABLE CELL            
                }
                $key++;
            }
            
            $HTML .= "</tr>";    # END TABLE ROW
        }
        $HTML .= "</table>";
        
        
        #echo $HTML;
        return $HTML;
    }
}
$data_array[] = "value1";
$data_array[] = "value2";
$data_array[] = "value3";
$data_array[] = "value4";
$data_array[] = "value5";
$data_array[] = "value6";
$data_array[] = "value7";
$data_array[] = "value8";
$data_array[] = "value9";
$data_array[] = "value10";
$data_array[] = "value11";
$data_array[] = "value12";
$data_array[] = "value13";
$data_array[] = "value14";

$make_table_obj = new maketable();
echo $make_table_obj->make_table($data_array, 4);
?>