0

Hi I have a data stored in array

 $roles = [2,4];

I want to mapping the data I have in $roles with the data in the database. I don't know how to mapping data if I have more than one data in $roles like in above and I don't know how to show it all in a table view.

I try this

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

Hope anyone can help me

3
  • have you tried using foreach? Commented May 19, 2020 at 5:10
  • I already use it but it show the last foreach data Commented May 19, 2020 at 5:11
  • share the code where you're using foreach Commented May 19, 2020 at 5:13

2 Answers 2

1

I've written a code which should help you with this case, comments are mentioned wherever necessary. Hope it helps you.

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']);
}
Sign up to request clarification or add additional context in comments.

6 Comments

still can't to combine the data
I'm sorry, I can't understand what you're asking, please clarify.
when I am using it, it still show data for the last for each
The data will be saved as an array so you'll have to use foreach again to get the data. In your view - foreach($need_app as $need){ echo $need;} This will give the value of each element in the array. You can then use it according to your needs.
I get it, but now, how to display it in a desc. I try foreach( $need_app as $row){} n get error.
|
0
 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
        }

SO based on your question

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);
            }
        }

Instead of using something like $role == 4

Make a constant or another variable inside the function and loop the conditions with that variable values. This will be helpful for future modification.

Something like this

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

and then loop it like below

 foreach ($roles as $role) {

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

                //your logic here
}
}

2 Comments

how to combine the data in one variable if I have 2,4 in $roles
You can use an array as I did $loopValues = [1,2,3,4,5,6]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.