Cool Perfectionist
Cool Perfectionist

Reputation: 205

mysql group_by in codeigniter

I have following table

batch_id   time_start    time_end  day      branch_id
1          4:30 PM       5:30 PM   Monday    5
2          5:30 PM       6:30 PM   Monday    4
1          4:00 PM       5:00 PM   Tuesday   5

I have following table code in my php

<table><tr><th><div><?php echo 'Batch Code';?></div></th>
            <th><div><?php echo 'Mon';?></div></th>
            <th><div><?php echo 'Tue';?></div></th>
            <th><div><?php echo 'Wed';?></div></th>
            <th><div><?php echo 'Thu';?></div></th>
            <th><div><?php echo 'Fri';?></div></th>
            <th><div><?php echo 'Sat';?></div></th>
            <th><div><?php echo 'Sun';?></div></th>
            </tr>
<tr>
<td>1</td><td>4:30 PM - 5:30 PM</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td>
</tr>
<tr>
<td>2</td><td>5:30 PM - 6:30 PM</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td>
</tr>
<tr>
<td>1</td><td> </td><td>4:00 PM - 5:00 PM </td><td> </td><td> </td><td> </td><td> </td><td> </td>
</tr>
</table>

What I want

I want that the time for same batch_id & same branch_id will show in corresponding day

As given in above table, I have two records for same batch_id i.e 1 I want data for monday will show in monday column and data for tuesday will show in tuesday column for same batch_id & same branch_id.

Thanks for help

UPDATE check the image enter image description here

Upvotes: 0

Views: 63

Answers (1)

Raja
Raja

Reputation: 608

Let me assume you having the result set as the following array. Try like this

<?php       
    $table = array( 
                        0  => array(
                            'batch_id' => 1 ,
                            'time_start' => '4:30 PM',
                            'time_end' => '5:30 PM',
                            'day' => 'Monday'
                        ),
                        1  => array(
                            'batch_id' => 2 ,
                            'time_start' => '5:30 PM',
                            'time_end' => '6:30 PM',
                            'day' => 'Monday'
                        ),
                        2  => array(
                            'batch_id' => 1 ,
                            'time_start' => '4:00 PM',
                            'time_end' => '5:00 PM',
                            'day' => 'Tuesday'
                        )
        );

    //Arrange the array according to the batch number       
    $arrange_array = array();
        foreach($table as $vals){
            $arrange_array[$vals['batch_id']][] = $vals;            
        }    
?>

<table>
    <tr>
        <th>
            <div><?php echo 'Batch Code'; ?></div>
        </th>
        <th>
            <div><?php echo 'Mon'; ?></div>
        </th>
        <th>
            <div><?php echo 'Tue'; ?></div>
        </th>
        <th>
            <div><?php echo 'Wed'; ?></div>
        </th>
        <th>
            <div><?php echo 'Thu'; ?></div>
        </th>
        <th>
            <div><?php echo 'Fri'; ?></div>
        </th>
        <th>
            <div><?php echo 'Sat'; ?></div>
        </th>
        <th>
            <div><?php echo 'Sun'; ?></div>
        </th>
    </tr> 

    <?php 
        foreach($arrange_array as $key => $days){
            echo '<tr>
                     <td>'.$key.'</td>';                         
                    foreach($days as $batch){
                        echo '<td>'.$batch['time_start'].' - '.$batch['time_end'].'</td>';
                    }                       
            echo  '</tr>';
        }       
    ?>                  
</table>

Upvotes: 1

Related Questions