3
Contributors
5
Replies
28
Views
3 Years
Discussion Span
Last Post by devianleong
1

Are the number of columns always the same? If yes, you can just do something like this:

<?php
    $header_name_one = // some user input captured through POST or GET
    $header_name_two = // some user input captured through POST or GET
?>

<table>
    <tr>
        <th><?php echo $header_name_one; ?></th>
        <th><?php echo $header_name_two; ?></th>
    </tr>
    <tr>
        // loop through data
    </tr>
</table>
0

You can also try one like this.

First create a function to generate the <th> items. The function can be as simple as this

function create_th($col_header_name){

    if(is_array($col_header_name)){

        return array(true,$col_header_name);
      }

  }

to generate the cols, you call the function like this. There is no limit on how many header cells you can create.

$tbl_cols = create_th(array('Hotel','Class','Single','Adult(RM)','CNB(RM)'));

    if($tbl_cols[0]){


    echo '<table>';
    echo '<tr>';
    foreach($tbl_cols[1] as $th){
        echo '<th>'. $th.'</th>';
    }

    echo '</tr>';
    echo '</table>';

    }

The output should be something like this

<table>
   <tr>
    <th>Hotel</th>
    <th>Class</th>
    <th>Single</th>
    <th>Adult(RM)</th>
    <th>CNB(RM)</th>
   </tr>
</table>

Why use function?
By using function, you can utilize filter_var_array PHP function to filter the items going into the <th>.

that's it..

Edited by veedeoo: info added.

0

if you will be adding values for each column, then a second argument to the function must be added and make sure that the count of the values going underneath are perfectly equal to the number of <th> you need to create.

something like this

 create_th($cols_count, $col_header_name){

  if($cols_count > 0 && (is_array($col_header_name) && (count($col_header_name == $cols_count)))){

         return array(true,$col_header_name);
    }
}

Edited by veedeoo: info added.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.