codeigniter中的mysql group_by

时间:2015-04-04 13:12:00

标签: php mysql codeigniter

我有下表

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

我的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>

我想要什么

我想要相同batch_id&amp;的时间相同的branch_id将在相应的日期显示

如上表所示,我有两个相同batch_id的记录,即 1 我希望星期一列中的数据将显示在星期一列中,星期二的数据将显示在星期二列中相同的batch_id&amp; same branch_id。

感谢您的帮助

更新 检查图像 enter image description here

1 个答案:

答案 0 :(得分:1)

我假设您将结果集设置为以下数组。试试这个

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