0

I have created a table from a SQL query and displayed it in the same order as they appear in the table. (Table A in image).

enter image description here

This is working okay.

However it would be great if the data could be clubbed under the member category. As in Table B in image.

SQL Query ...

$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 Answers 2

1

A little update of your code souhld do it:

$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>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi CodiMech25, Thanks for your reply. Let me try this and get back to you.
Hi CodiMech25, That works perfectly. Thanks a tonne.. :)
@sansam I'm glad i could help :)
0

While loop though the data mark on array with group of category name and then print the result

$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 } ?>
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.