将多个记录存储在数组中,多个存储在Codeigniter中

时间:2018-08-07 20:56:07

标签: php codeigniter

我有以下代码,其中显示了某个日期范围内工作人员的活动。我想选择多个工人,并获得此报告并在屏幕上显示。 使用此代码,我只能显示我从multi select

中选择的最后一个

控制器

$this->form_validation->set_rules('worker[]', 'worker','required|trim',array(
        'required' => 'Select one'
    ));

        foreach ($this->input->post('worker') as $worker) {
        $start =$this->input->post('start');
        $end =$this->input->post('end');

        $data['start'] = $start;
        $data['end'] = $end;

            $data['type'] = $this->input->post('customRadio');
            $data['worker'] =$this->m->verifyCode($worker);
            $data['activities']= $this->report->getactivities($start,$end,$worker);
        }

        $this->load->view('report/person-result',$data);

视图

<?php if ($worker): ?>
    <?php echo $worker->Name; ?>
    <?php endif;?> </h4>

<?php $dates = array(); ?>

<?php if ($activities): ?>
<?php


    foreach ($activities as $activitie) {
        $dates[$activitie->dateActivities][] = $activitie;

    }
?>
      <?php  foreach ($dates as $date):?>

<table class="table table-hover dataTable no-footer">
    <thead>
        <tr>
            <th>date</th>

        </tr>
    </thead>

    <?php foreach ($date as $activitie): ?>
    <tr>
        <td><?php echo $activitie->date; ?></td>
    </tr>

    <?php endforeach; ?>
</table>

<?php endforeach; ?>

<?php endif; ?>

我想要这样的东西

Woker Name one
 ---------------------------------------------------  
|id|work       |start   |end  |worker|date        |  
|--|-----------|--------|-----|------|------------|  
|1 |caja       |8:30    |8:32 |1     |2018-07-13  |  
|2 |baul       |8:35    |8:40 |1     |2018-07-13  |  
|3 |cofre      |8:50    |9:30 |2     |2018-07-14  |  



Woker Name two
 ---------------------------------------------------  
|id|work       |start   |end  |worker|date        |  
|--|-----------|--------|-----|------|------------|  
|1 |cofe       |8:30    |8:32 |1     |2018-07-13  |  
|2 |cofe       |8:35    |8:40 |1     |2018-07-13  |  
|3 |cofe       |8:50    |9:30 |2     |2018-07-14  |  

使用代码我只能显示最后一个,因为foreach会记录最后一个工作程序[]。我希望我能解释清楚。

1 个答案:

答案 0 :(得分:1)

在foreach循环foreach ($this->input->post('worker') as $worker) {的每次迭代中,您都用新值覆盖$ data中的现有值。您需要在$ data数组中添加一个$ key,以便能够存储所有活动和所有工作程序,如下所示:

$this->form_validation->set_rules('worker[]', 'worker','required|trim',array(
    'required' => 'Select one'
));

$start = $this->input->post('start');
$end = $this->input->post('end');
$data['type'] = $this->input->post('customRadio');

foreach ($this->input->post('worker') as $key => $worker) {
    $data['reports'][$key]['worker'] = $this->m->verifyCode($worker);
    $data['reports'][$key]['activities']= $this->report->getactivities($start,$end,$worker);
}

$this->load->view('report/person-result',$data);

视图:

<?php foreach ($reports as $report) : ?>

    <?php if ($report['worker']): ?>
        <?php echo $report['worker']->Name; ?>
    <?php endif; ?> </h4>

    <?php $dates = array(); ?>

    <?php if ($report['activities']): ?>

        <?php foreach ($report['activities'] as $activitie) {
            $dates[$activitie->dateActivities][] = $activitie;
        } ?>

        <?php  foreach ($dates as $date):?>

          <table class="table table-hover dataTable no-footer">
            <thead>
              <tr>
                <th>date</th>
              </tr>
            </thead>

            <?php foreach ($date as $activitie): ?>
              <tr>
                <td><?php echo $activitie->date; ?></td>
              </tr>
            <?php endforeach; ?>

          </table>

        <?php endforeach; ?>

    <?php endif; ?>

<?php endforeach; ?>

(我不确定您在哪里使用$data['type'],因此您可能必须将其作为$data['reports'][$key]['type']移入控制器的foreach循环中,具体取决于您的使用方式。)< / p>