来自带有标题的SQL查询的表

时间:2018-08-01 04:19:50

标签: php arrays html-table

我已经通过SQL查询创建了一个表,并按照它们在表中出现的顺序显示了该表。 (图片中的表A)。

enter image description here

这很好,

但是,如果可以将数据汇总到成员类别下,那就太好了。如图片中的表B所示。

SQL查询...

$row = mysqli_num_rows($sql);
if($row > 0) {          
    while ($result = mysqli_fetch_assoc($sql)){
        $category[] = trim($result['category']);
        $name[] = trim($result['f_name']).' '.trim($result['l_name']);
        $memid[] = trim($result1['memid']);
        $addr[] = trim($result['addr']);    
        $phone[] = trim($result['phone']);
    }
} ?>



<table>
    <tr>                
        <th>Category</th>
        <th>Mem ID</th>
        <th>Name</th>
        <th>Address</th>
        <th>Phone</th>                      
    </tr>
    <?php                 
    if ($row>0) {   
        for ($i=0; $i<=$row-1; $i++){ ?>
            <tr>
                <td><?php echo $category[$i]; ?></td>       
                <td><?php echo $memid[$i]; ?></td>
                <td><?php echo $name[$i]; ?> </td>
                <td><?php echo $addr[$i]; ?> </td>  
                <td><?php echo $phone[$i]; ?> </td> 
            </tr>
        <?php } 
    } ?>
</table>

2 个答案:

答案 0 :(得分:1)

您的代码需要做一些更新:

$categories = [];
$row = mysqli_num_rows($sql);

if($row > 0) {          
    while ($result = mysqli_fetch_assoc($sql)) {
        $result_category = trim($result['category']);

        if (!isset($categories[$result_category])) {
            $categories[$result_category] = [];
        }

        $new = [];

        $new['category'] = $result_category;
        $new['name'] = trim($result['f_name']).' '.trim($result['l_name']);
        $new['memid'] = trim($result['memid']);
        $new['addr'] = trim($result['addr']);   
        $new['phone'] = trim($result['phone']);

        $categories[$result_category][] = $new;
    }
} ?>    

    <table>
      <tr>              
        <th>Category</th>
        <th>Name</th>
        <th>Phone</th>                      
      </tr>
<?php                 
if ($row>0) {   
    foreach ($categories as $category_name => $data){ ?>
        <tr>
            <td><?php echo $category_name; ?></td>      
            <td></td>
            <td></td>   
        </tr>
        <?php foreach ($data as $row) {?>
        <tr>
            <td><?php echo $row['memid']; ?></td>
            <td><?php echo $row['name']; ?> </td>
            <td><?php echo $row['phone']; ?> </td>  
        </tr>
    <?php   }
     } 
    } ?>
    </table>

答案 1 :(得分:0)

循环遍历数组中带有类别名称组的数据标记,然后打印结果

$row = mysqli_num_rows($sql);
if($row > 0) {          
    $mainArray = [];
    while ($result = mysqli_fetch_assoc($sql)){
        $category = $result['category'];
        if(isset($mainArray[$category])){
            $mainArray[$category][] = $result;
        } else {
            $mainArray[$category] = $result;
        }
    }
}
foreach($mainArray as $cateName => $data){ ?>
    <tr>
        <td style="text-align:left"><?php echo $cateName; ?></td>      
        <td></td>
        <td></td>   
    </tr>
    <?php
    foreach($data as $row){ ?>
    <tr>
        <td><?php echo $row['memid']; ?></td>      
        <td><?php echo $row['f_name'].' '.$row['l_name']; ?></td>
        <td><?php echo $row['phone']; ?></td>   
    </tr>
    <?php } ?>
}
?>