Reputation: 73
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
Upvotes: 0
Views: 628
Reputation: 2567
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
}
}
Upvotes: 0
Reputation: 1961
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']);
}
Upvotes: 1