为了账号安全,请及时绑定邮箱和手机立即绑定

在 codeigniter 中映射数据

在 codeigniter 中映射数据

PHP
holdtom 2023-04-02 14:53:06
我有一个数据存储在数组中 $roles = [2,4];我想将我在 $roles 中的数据与数据库中的数据进行映射。如果我像上面那样在 $roles 中有多个数据,我不知道如何映射数据,而且我不知道如何在表视图中显示所有数据。我试试这个 if (count($roles) == 1) {  if ($roles[0] == 2) {    $data['need_app'] = $this->Hire_model->need_approval_req($OrganizationID, $requestor_id);  }else if ($roles[0] == 3) {    $data['need_app'] = $this->Hire_model->need_approval_recruiter();  }else if ($roles[0] == 4) {    $data['need_app']=$this->Hire_model->need_approval_hr();  }else if ($roles[0] == 5) {    $data['need_app'] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id);  }}else{   for ($i=0; $i < count($roles)-1 ; $i++) {     if ($roles[0] == 2) {      $data['need_app'] = $this->Hire_model->need_approval_req($OrganizationID, $requestor_id);      var_dump(count($data['need_app']));    }else if ($roles[0] == 3) {      $data['need_app'] = $this->Hire_model->need_approval_recruiter();    }else if ($roles[0] == 4) {      $data['need_app']=$this->Hire_model->need_approval_hr();    }else if ($roles[0] == 5) {      $data['need_app'] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id);      var_dump(count($data['need_app']));    }    // var_dump(count($data['need_app']));  }}希望有人能帮助我
查看完整描述

2 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

我写了一段代码可以帮助你解决这个问题,必要时会提到注释。希望对你有帮助。


foreach ($roles as $role) { 

    if ($role == 2) {

      $data['need_app'][] = $this->Hire_model->need_approval_req($OrganizationID, $requestor_id); // you need to make an array here otherwise the current value will over-write the previous one

      var_dump(count($data['need_app']));

    }else if ($role == 3) {

      $data['need_app'][] = $this->Hire_model->need_approval_recruiter(); // you need to make an array here otherwise the current value will over-write the previous one

    }else if ($role == 4) {

      $data['need_app'][] =$this->Hire_model->need_approval_hr(); // ...

    }else if ($role == 5) {

      $data['need_app'][] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id); // ...

    }

    // var_dump($data['need_app']);

}


查看完整回答
反对 回复 2023-04-02
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

 foreach ($roles as $role) {


            if ($role == 2) {


                //your logic here


            } else if ($role == 4) {


                //your logic here


            } else if ($role == WHAT_EVER_YOU_WANTS) {

                //Logic

            }


            //your logic here

        }

所以根据你的问题


foreach ($roles as $role) {

            if ($role == 2) {

                $data['need_app'] = $this->Hire_model->need_approval_req($OrganizationID, $requestor_id);

            } else if ($role == 3) {

                $data['need_app'] = $this->Hire_model->need_approval_recruiter();

            } else if ($role == 4) {

                $data['need_app'] = $this->Hire_model->need_approval_hr();

            } else if ($role == 5) {

                $data['need_app'] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id);

            }

        }

而不是使用类似$role == 4


在函数内创建一个常量或另一个变量,并使用该变量值循环条件。这对以后的修改很有帮助。


像这样的东西


$loopValues = [1,2,3,4,5,6] // Or    $loopValues = [['val' => 1, 'function' => functionName()]] <- I prefer this one all the time

然后像下面这样循环


 foreach ($roles as $role) {


            if ($role == $loopValues[0]) {


                //your logic here

}

}


查看完整回答
反对 回复 2023-04-02
  • 2 回答
  • 0 关注
  • 139 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信