代码之家  ›  专栏  ›  技术社区  ›  tree em

要根据表头的顺序动态显示行吗

php
  •  0
  • tree em  · 技术社区  · 14 年前
    <?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>
    

    1 回复  |  直到 14 年前
        1
  •  1
  •   Mark Baker    14 年前
    $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>';
    }