doutuo7156 2016-02-16 18:30 采纳率: 100%
浏览 79
已采纳

需要在HTML表格中以特定格式显示mysql表格数据

I am stuck into a problem in PHP. Basically I need to show data fetched from mysql table in a HTML table in certain format. Am unable to figure out how I can do it. Below is my query and PHP code:

$array = array();

$query = mysql_query("select name, townid, entry_date from tbl_entry  where month(entry_date) = '02' and year(entry_date) = '2016' group by townid, name ");

while($row = mysql_fetch_array($query)) {
        $array[$row['name']][$row['townid']]= $row['entry_date'];
}   

With this, var_dump on $array variable shows correct mysql results that is expected. Here is the content of $array:

array (size=2)
  B => 
    array (size=2)
      3 => string '2016-02-16' (length=10)
      6 => string '2016-02-16' (length=10)
  A => 
    array (size=1)
      3 => string '2016-02-16' (length=10)

I need to display this in a HTML table in below format:

Serial Number | Date         |     A    |     B
-------------------------------------------------------
 1            | 2016-02-16   |     3    | 3     
 2            | 2016-02-16   |          | 6     

Code tried so far:

<thead>
                    <tr>
                    <th>Sr No</th>
                    <th>Date</th>
                    <?php
                        foreach($demo3 as $srid=>$array) {
                            $SRname = GetSingleRow(ADMIN,$srid);
                            echo "<th>".$SRname['name']."</th>";    
                        }


                    ?>
                    </tr>
                </thead>
                <tbody> 
                    <?php 
                        $count = 1;
                        foreach($array as $key=>$val) {
                            echo "<tr>";
                            echo "<td>$count</td>";
                            foreach($val as $t =>$d) {
                                echo "<td>".$d[$t]."</td>";
                            }
                            echo "</tr>";
                            $count++;
                        }
                    ?>
                </tbody>

But am not getting expected output. Please help me in this.

  • 写回答

1条回答 默认 最新

  • duanduji2986 2016-02-16 18:58
    关注

    Try read somesthing about PDO — its improve ypu skills with DB work. Try this code:

    <?php
    $rowArray = array();
    foreach( $array AS $key => $val )
    {
        $i = 0;
        foreach( $val AS $key2 => $val2 )
        {
            $rowArray[ $i ][ 'date' ] = $val2;
            $rowArray[ $i ][ $key ] = $key2;
            $i++;
        }
    }
    ?>
    <thead>
        <tr>
        <th>Sr No</th>
        <th>Date</th>
        <?php
            foreach($demo3 as $srid=>$array) {
                $SRname = GetSingleRow(ADMIN,$srid);
                echo "<th>".$SRname['name']."</th>";    
            }
    
    
        ?>
        </tr>
    </thead>
    <tbody> 
        <?php 
            $i = 1;
            foreach( $rowArray AS $row )
            {
                echo '<tr>';
                echo '<td>' . $i . '</td>';
                foreach( $row AS $rowData )
                {
                    echo '<td>' . $rowData . '</td>';
                }
                echo '</tr>';
                $i++;
            }
        ?>
    </tbody>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看