在数组中存储多个记录,多个,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遍历并记录最后一个worker[]。我希望我解释得好。

在foreach循环foreach ($this->input->post('worker') as $worker) {的每次迭代中,您都在用新值覆盖$data中的现有值。您需要将$key添加到$data数组中,以便能够存储所有活动和所有工作者,类似于以下内容:

$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'],所以你可能不得不将其移动到控制器中的foreach循环中,作为$data['reports'][$key]['type'],这取决于你如何使用它。(

相关内容

  • 没有找到相关文章

最新更新