对于每个循环不工作
我有一些主类别和用户组,用户组被分配给主类别。我正在使用以下代码,但它只显示表中的最后一条记录,而我需要显示所有匹配的记录。
控制器中的功能:
function listdesignation($id) {
$res = $this->designation_model->GetDesignation($id);
$this->data['test'] = $res;
if ($res) {
foreach ($res as $row) {
$this->data['selected_designation'] = $this->designation_model->GetDesignation_Names($row['designation_id']);
}
if ($this->data['selected_designation']) {
$this->data['enable_view'] = true;
}
}
}
模型中的功能:
function GetDesignation_Names($id) {
$designation = array();
$this->db->select('*');
$this->db->from('delta_designation');
$this->db->where('designation_id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$designation[$row->designation_id]['designation_id'] = $row->designation_id;
$designation[$row->designation_id]['designation'] = $row->designation;
}
return $designation;
}
return false;
}
I've some master categories and user groups, user groups are assigned to master categories. I am using the following code but it only display the last record in the table while I need to display all matching records.
Function in controller:
function listdesignation($id) {
$res = $this->designation_model->GetDesignation($id);
$this->data['test'] = $res;
if ($res) {
foreach ($res as $row) {
$this->data['selected_designation'] = $this->designation_model->GetDesignation_Names($row['designation_id']);
}
if ($this->data['selected_designation']) {
$this->data['enable_view'] = true;
}
}
}
Function in model:
function GetDesignation_Names($id) {
$designation = array();
$this->db->select('*');
$this->db->from('delta_designation');
$this->db->where('designation_id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$designation[$row->designation_id]['designation_id'] = $row->designation_id;
$designation[$row->designation_id]['designation'] = $row->designation;
}
return $designation;
}
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将在
foreach
循环的每次迭代中覆盖变量。您可能想改为构建一个数组。例如,而不是
尝试这个:
那么你可以:
You're overwriting your variable with each iteration through your
foreach
loop. You probably want to build up an array instead.E.g. instead of
Try this:
Then you can: