在 codeigniter 中映射数据

我有一个数据存储在数组中


 $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']));

  }

}

希望有人能帮助我


holdtom
浏览 140回答 2
2回答

翻翻过去那场雪

我写了一段代码可以帮助你解决这个问题,必要时会提到注释。希望对你有帮助。foreach ($roles as $role) {&nbsp;&nbsp; &nbsp; if ($role == 2) {&nbsp; &nbsp; &nbsp; $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&nbsp; &nbsp; &nbsp; var_dump(count($data['need_app']));&nbsp; &nbsp; }else if ($role == 3) {&nbsp; &nbsp; &nbsp; $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&nbsp; &nbsp; }else if ($role == 4) {&nbsp; &nbsp; &nbsp; $data['need_app'][] =$this->Hire_model->need_approval_hr(); // ...&nbsp; &nbsp; }else if ($role == 5) {&nbsp; &nbsp; &nbsp; $data['need_app'][] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id); // ...&nbsp; &nbsp; }&nbsp; &nbsp; // var_dump($data['need_app']);}

元芳怎么了

&nbsp;foreach ($roles as $role) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($role == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //your logic here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($role == 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //your logic here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($role == WHAT_EVER_YOU_WANTS) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Logic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //your logic here&nbsp; &nbsp; &nbsp; &nbsp; }所以根据你的问题foreach ($roles as $role) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($role == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['need_app'] = $this->Hire_model->need_approval_req($OrganizationID, $requestor_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($role == 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['need_app'] = $this->Hire_model->need_approval_recruiter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($role == 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['need_app'] = $this->Hire_model->need_approval_hr();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($role == 5) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['need_app'] = $this->Hire_model->need_approval_cc($PositionID, $OrganizationID, $requestor_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }而不是使用类似$role == 4在函数内创建一个常量或另一个变量,并使用该变量值循环条件。这对以后的修改很有帮助。像这样的东西$loopValues = [1,2,3,4,5,6] // Or&nbsp; &nbsp; $loopValues = [['val' => 1, 'function' => functionName()]] <- I prefer this one all the time然后像下面这样循环&nbsp;foreach ($roles as $role) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($role == $loopValues[0]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //your logic here}}
打开App,查看更多内容
随时随地看视频慕课网APP