我使用foreach将动态参数传递给模型以检索数据。我正在尝试在视图中显示结果,但很少出现错误。请建议是否有更好的方法来应对这种情况。
控制器:
$part_ids = $this->COC_model->getDesignParts($batch,$sr_no);
$parts_activities = [];
foreach ($part_ids as $current_part_id) {
$parts_activities['part_activities'] = ['part_id' => $current_part_id->id,
'activities' =>$this->COC_model->getActivities($batch,$sr_no,$current_part_id->id),
'row'=>$this->VDIR_model->get_batch_details($batch,$sr_no,$current_part_id->id)];
}
$this->load->view('QC/COC',$parts_activities);
型号:
function getDesignParts($batch,$sr_no){
$query = $this->db->query("SELECT
id,part_name,part_no
FROM
design
WHERE
batch_id='$batch' AND
sr_no='$sr_no'");
return $query->result();
}
function getActivities($batch,$sr_no,$part_id){
$query=$this->db->query("select activities,pi_activities.id as char_activity_id,specification_from,specification_to
from pi_activities
where batch='$batch' and sr_no='$sr_no' and part_id='$part_id'");
return $query->result();
}
function get_batch_details($batch,$sr_no,$part_id){
$query=$this->db->query("select id,test_field
from test
where batch='$batch' and sr_no='$sr_no' and part_id='$part_id'");
return $query->row();
}
视图:
foreach ($part_activities as $getActivity):
echo $getActivity['part_id'];
?>
<table class="table-bordered table-responsive table">
<tr class="set-width1">
<?php foreach ($getActivity['activities'] as $key => $activity) {?>
<td><?= $activity->activities;?></td>
<?php }?>
</tr>
</table>
<?php endforeach;?>
错误:
Message: Illegal string offset 'part_id'
Message: Invalid argument supplied for foreach()
我不知道为什么会出现错误,但应该加入表,而不是创建新表。正确代码:
<table class="table-bordered table-responsive table">
<tr class="set-width1">
<?php foreach ($getActivity['activities'] as $key => $activity) { ?>
<td><?php echo $activity->activities; ?></td>
<?php } ?>
</tr>
推送到数组的日期不正确应该是
$part_ids = $this->COC_model->getDesignParts($batch,$sr_no);
$parts_activities = [];
foreach ($part_ids as $current_part_id) {
$parts_activities['part_activities'][] = ['part_id' => $current_part_id->id,
'activities' =>$this->COC_model->getActivities($batch,$sr_no,$current_part_id->id)];
}
$this->load->view('QC/COC',$parts_activities);