tags:

views:

25

answers:

1
<?php
$header = array('header1','header2','header3');
?>
<table>
<thead>
    <tr>
<?php 
    for($i=0;$i<=count($header);$i++){?>
        <th ><?php echo $header[i];?></th>
    <?php } ?>
    </tr>
</thead>
<?php foreach($result_from_db_query as $key=>$val){?>
 <tr><td><?php echo $val['Column1']?></td><td><?php echo $val['Column2']?></td><td><?php echo $val['Column3']?></td></tr>
?>
</table>
Want to dynamic the row display depend on order of table header
for example if 
$header = array('header1','header2','header3')
the output should like this
<table>
<thead>
    <tr>
        <th >Header 1</th>
        <th >Header 2</th>
        <th >Header 3</th>
    </tr>
</thead>
<tr><td>Column11</td><td>Column21</td><td>Column31</td></tr>
<tr><td>Column12</td><td>Column22</td><td>Column32</td></tr>
<tr><td>Column13</td><td>Column23</td><td>Column33</td></tr>
<tr><td>Column14</td><td>Column24</td><td>Column34</td></tr>
</table>
BUT 
if 
$header = array('header3','header2','header1')

the output should like this

<table>
<thead>
    <tr>
        <th >Header 1</th>
        <th >Header 2</th>
        <th >Header 3</th>
    </tr>
</thead>
<tr><td>Column31</td><td>Column21</td><td>Column11</td></tr>
<tr><td>Column32</td><td>Column22</td><td>Column12</td></tr>
<tr><td>Column33</td><td>Column23</td><td>Column13</td></tr>
<tr><td>Column34</td><td>Column24</td><td>Column14</td></tr>
</table>

And so On. Anybody know how defind the the data structure with php code.

+1  A: 
$header = array('header1','header2','header3');

foreach($result_from_db_query as $val) {
   echo '<tr>';
   foreach($header as $column) { 
      echo '<td>',$val[$column],'</td>';
   }
   echo '</tr>';
} 
Mark Baker
in this case my reuturn $result_from_db_query=array(1=>array('header1'=>'aaa','header2'=>'bbb','header3'=>'ccc'), 2=>array('header1'=>'aaa2','header2'=>'bbb2','header3'=>'ccc2'));
python
So try again with this code
Mark Baker
@Mark Baker! Nice thanks for short code.it works now,
python