phphtml-tablecodeigniter-2

How to show MySql vertical table to horizontal in Codeigniter?


I want to fetch the data from MYSQL Table and display the data in the HTML table as shown in the "HTML table" picture.

MySql Table MySql Table

HTML Table HTML Table


Solution

  • Assume data is

    $data = array(
                array(
                'id'=>1,
                'name'=>'s1',
                'product_name'=>'p1',
                'month'=>'jan,2018',
                'quantity'=>12
                ),
                array(
                'id'=>2,
                'name'=>'s2',
                'product_name'=>'p2',
                'month'=>'jan,2018',
                'quantity'=>1
                ),
                array(
                'id'=>3,
                'name'=>'s3',
                'product_name'=>'p3',
                'month'=>'feb,2018',
                'quantity'=>12
                ),
                array(
                'id'=>4,
                'name'=>'s4',
                'product_name'=>'p4',
                'month'=>'feb,2018',
                'quantity'=>12
                ),
                array(
                'id'=>4,
                'name'=>'s4',
                'product_name'=>'p4',
                'month'=>'Mar,2018',
                'quantity'=>12
                )
                );
    

    Create Dynamic Header

    $header = array();
    foreach($data as $val){
    $header = array_unique(array_values(array_merge($header,array_keys($val))));
    }
    

    Add key month value as key with quantity value

    foreach($data as $key=>$val){
            $data[$key][$val['month']] = $val['quantity'];
        }