php - Codeigniter 中的 mysql 分组依据

标签 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和相同的branch_id的时间将显示在相应的日期

如上表所示,我有两个相同batch_id的记录,即1我希望周一的数据将显示在周一列中,周二的数据将显示在周二列中,以获取相同的batch_id和相同的branch_id .

感谢帮助

更新 检查图像 enter image description here

最佳答案

让我假设您的结果集如下数组。尝试这样

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

关于php - Codeigniter 中的 mysql 分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29446821/

相关文章:

javascript - 通过jquery打印更多元素

mysql - 如何减少 MySQL 查询中列中所有单元格的值?

MySQL 错误 1093 - 无法在 FROM 子句中指定要更新的目标表

php - Ajax 调用 Controller 中的 php 函数

php - mysql 查询错误可以由服务器/托管引起吗?

php - 数组上的逗号,除了通过 PHP 的最后一个值

php - 使用 jQuery 的日期选择器

php - foreach INSERT 到 Mysql DB 中非常慢,特别是在插入超过 100K 条记录时

php - 在父页面下移动帖子 - Wordpress

php - MySQL insert_id 总是返回零