DevBlog

Coding my way

3 years ago in PHP · 2 MIN READ

Generating HTML tables with PHP

I had some problems with HTML tables and php, and loathed to create a specific table generator each time I needed to output a table, so I've come up with this little thing :)

<?php

class bootTable {

    /**
     * @param array $attributes
     * @return null|string
     */
    static function parseAttributes($attributes = array())
    {

        if (self::checkArray($attributes)) {

            $boolean = array('disabled');
            $excluded = array('iconLeft', 'iconRight', 'head', 'responsive', 'footer');

            $string = null;

            foreach ($attributes AS $k => $v) {

                if (!in_array($k, $excluded)) {
                    if (in_array($k, $boolean)) {
                        $string .= ' ' . $k . ' ';
                    } else {
                        if (!is_null($v)) {
                            $string .= ' ' . $k . '="' . $v . '" ';
                        }
                    }
                }
            }

            return $string;
        }

        return null;

    }

     /**
     * @param string $attributeToAdd
     * @param string $attributeValue
     * @param array $attributes
     * @param bool $overwrite
     * @return array|bool
     */
    private static function addAttribute($attributeToAdd = '', $attributeValue = '', $attributes = array(), $overwrite = true) {
        if (self::checkArray($attributes)) {

            $attributeKeys = array_keys($attributes);

            if (in_array($attributeToAdd,$attributeKeys)) {

                if ($overwrite) {
                    $attributes[$attributeToAdd] = $attributeValue;
                } else {
                    $content = explode(' ',$attributes[$attributeToAdd]);
                    $content[] = $attributeValue;
                    $attributes[$attributeToAdd] = implode(' ',$content);
                }

            } else {
                $attributes[$attributeToAdd] = $attributeValue;
            }

            return $attributes;
        }

        return false;
    }

    /**
     * A function to generate bootstrap tables
     *
     * @param array $heads The table heads, also present in footer if required
     * @param array $content
     * @param array $attributes
     * @return null|string
     */
    public static function createTable($heads = array(), $content = array(), $attributes = array()) {
        if (self::checkArray($heads) && self::checkArray($content)) {

            /* Get all the available table heads defined in $heads */
            $colsInTable = array_keys($heads);

            /* fix in the table class as it's a requirement */

            $attributes = self::addAttribute('class','table',$attributes,false);

            /* Create a var to generate the table */
            $r = null;

            /* Add responsive container if required */
            if (isset($attributes['responsive']) && $attributes['responsive'] == true)
            $r .= '<div class="table-responsive">';

            /* Create the table */
            $r .= '<table ' . self::parseAttributes($attributes) . '>';

            /* Create table head */
            $r .= '<thead>';
            $r .= '<tr>';

            foreach ($heads AS $head) {
                if (self::checkArray($head)) {
                    $r .= '<th ' . self::parseAttributes($head['attr']) . '>';
                    $r .= $head['title'];
                    $r .= '</th>';
                } else {
                    $r .= '<th>';
                    $r .= $head;
                    $r .= '</th>';
                }
            }

            /* Close table head */
            $r .= '</tr>';
            $r .= '</thead>';

            /* Create table body */
            $r .= '<tbody>';

            foreach ($content AS $row) {
                $r .= '<tr' . self::parseAttributes($row['attr']) .'>';

                foreach ($colsInTable AS $colName) {
                    $r .= '<td>';
                    $r .= (isset($row[$colName]) ? $row[$colName] : ' ');
                    $r .= '</td>';
                }

                $r .= '</tr>';
            }

            /* Close table body */
            $r .= '</tbody>';

            /* if footer is required */
            if (isset($attributes['footer']) && $attributes['footer']==true) {
                $r .= '<tfoot>';
                $r .= '<tr>';
                foreach ($heads AS $head) {

                    $r .= '<th>';
                    if (self::checkArray($head)) {
                        $r .= $head['title'];
                    } else {
                        $r .= $head;
                    }

                    $r .= '</th>';
                }
                $r .= '</tr>';
                $r .= '</tfoot>';
            }

            /* Close the table */
            $r .= '</table>';

            /* Close responsive container if present */
            if (isset($attributes['responsive']) && $attributes['responsive'] == true)
            $r .= '</div>';

            return $r;
        }

        return null;
    }

    /**
     * Check the $data param for type and size
     *
     * @param null $data
     *
     * @return bool
     */
    public static function checkArray($data = null) {
        if (is_array($data) AND count($data)>0) {
            return true;
        }

        return false;
    }

    public static function generatePreview() {

        /* Column names for easier naming, possibility of gettext use */
        $heads['index1'] = 'Index 1 title';
        $heads['index2'] = 'Index 2 title';
        $heads['index3'] = 'Index 3 title';
        $heads['index4'] = 'Index 4 title';

        /* the content is in a associative array, as like a PDO result fetch with  the PDO::FETCH_ASSOC option */

        $data[] = array(
            'index1'=>'Value 1',
            'index2'=>'Value 2',
            'index3'=>'Value 3',
            'index4'=>'Value 4'
        );
        $data[] = array(
            'index1'=>'Value 1',
            'index2'=>'Value 2',
            'index3'=>'Value 3',
            'index4'=>'Value 4'
        );
        $data[] = array(
            'index1'=>'Value 1',
            'index2'=>'Value 2',
            'index3'=>'Value 3',
            'index4'=>'Value 4'
        );
        $data[] = array(\   
            'index1'=>'Value 1',
            'index2'=>'Value 2',
            'index3'=>'Value 3',
            'index4'=>'Value 4',
            'attr'=> array(
                'style'=>'font-style: italic; color: red;'
            )
        );

        echo self::createTable($heads,$data,array(
            'footer'=>true
        ));
    }

}

If you would do something the other way, found some coding error, please don't keep it to yourself!

···

Miklós Galicz

I'm a hobbyist who managed to become a professional web developer who specializes in Laravel and Vue.js, also the Spice must flow.
comments powered by Disqus


© 2017 DevBlog