learn
learn

Reputation: 300

Adding row_array to result_array foreach loop with codeigniter?

I currently have a function on a web app which I'm building where a jobseeker can view the jobs they have applied for, very simple.

When they "apply" this application is stored in the database table 'applications' with a 'job_id' column which stores the 'id' of the job from the 'jobs' database table.

At the moment I am able to pull each application the said jobseeker has made.

Though, I am unable to loop through each application and find the job which corresponds to that application and then add the row_array() to larger array which I will then output the jobs with a foreach loop.

Essentially I am asking how do I add an array to an array and then output the full array?

appliedfor.php (CONTROLLER)

$applications_query = $this->db->get_where(
    'applications', array('jobseeker_profile_id' => $user['id'])
);
$applications = $applications_query->result_array();

$data[] = array();

foreach ($applications as $application) {
    $job_id = $application['job_id'];
    $data['job'] = $this->db->get_where('jobs', array('id' => $job_id))->row_array();
    $data['jobs'] .= $data['job'];
}

$data['jobs'];

$this->load->view('header');
$this->load->view('appliedfor', $data); 

appliedfor.php (VIEW)

foreach ($jobs as $job) {
    $single_job_id = $job['id'];

    echo "<br>";
    echo form_open('job/view' . '" id="eachJob');

    echo "<div id=\"leftContain\" class=\"floatLeft\">";
    echo "<h4 class=\"green\">" . $job['role'] . "</h4>";
    echo "<div class=\"italic\"><div class=\"blue floatLeft\">" . $job['company']
        . " &nbsp; </div><div class=\"floatLeft\">in</div><div class=\"blue floatLeft\"> &nbsp; "
        . $job['location'] . "</div></div><br><br>";
    echo "</div>";

    echo "<div id=\"rightContain\" class=\"floatLeft\">";
    echo "<input type=\"hidden\" name=\"job_id\" value=\"" . $single_job_id . "\">";
    echo form_submit('submit' . '" class="jobButton floatRight"', 'View Job');
    echo "</div>";

    echo form_close();
}

I am currently getting 2 errors: Undefined index: jobs and the error is on this line apparently $data['jobs'] in the controller within the foreach.

The other error is the foreach within the view file but that is basically triggered by the first error.

Thanks for your help.

Upvotes: 1

Views: 5251

Answers (2)

Harry S
Harry S

Reputation: 256

change your controller code :

$applications_query = $this->db->get_where(
    'applications', array('jobseeker_profile_id' => $user['id'])
);
$applications = $applications_query->result_array();

$data = array(); //declare and init $data, an empty array

foreach ($applications as $job_id=>$application) {
   // list jobs for job_id, you can using $job_id as array-key
    $data['job'][$job_id] = $this->db->get_where('jobs', array('id' => $job_id))->row_array(); 
}


$this->load->view('header');
$this->load->view('appliedfor', $data); 

Upvotes: 0

jmadsen
jmadsen

Reputation: 3675

You are correct:

$data['jobs'] .= $data['job'];

concatenates strings, not arrays, and will never work.

Instead, try something like:

$data['jobs'][] = $data['job'];

to build up an array of the jobs, then output with a foreach() or whatever is most suitable

Upvotes: 1

Related Questions