对于每个循环不工作

发布于 2024-12-06 22:59:27 字数 1170 浏览 0 评论 0原文

我有一些主类别和用户组,用户组被分配给主类别。我正在使用以下代码,但它只显示表中的最后一条记录,而我需要显示所有匹配的记录。

控制器中的功能:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-12-13 22:59:27

您将在 foreach 循环的每次迭代中覆盖变量。您可能想改为构建一个数组。

例如,而不是

foreach (...) {
    $myvar = $row->field;
}

尝试这个:

$myresult = array();
foreach (...) {
    $myvar = $row->field;
    $myresult[]= $myvar;  // (append to array)
}

那么你可以:

foreach ($myresult as $onerow) {
    ...
}

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

foreach (...) {
    $myvar = $row->field;
}

Try this:

$myresult = array();
foreach (...) {
    $myvar = $row->field;
    $myresult[]= $myvar;  // (append to array)
}

Then you can:

foreach ($myresult as $onerow) {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文