[英]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. 当他们“应用”时,此应用程序存储在数据库表“应用程序”中,其中的“ job_id”列存储了“作业”数据库表中作业的“ id”。
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. 但是,我无法遍历每个应用程序并找到与该应用程序相对应的作业,然后将row_array()添加到更大的数组中,然后我将通过foreach循环输出作业。
Essentially I am asking how do I add an array to an array and then output the full array? 本质上,我在问如何将一个数组添加到一个数组,然后输出完整的数组?
appliedfor.php (CONTROLLER) Appliedfor.php (控制器)
$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) Appliedfor.php (查看)
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']
. " </div><div class=\"floatLeft\">in</div><div class=\"blue floatLeft\"> "
. $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. 我目前遇到2个错误:未定义的索引:作业,并且该错误显然在foreach控制器中的此行上$data['jobs']
。
The other error is the foreach within the view file but that is basically triggered by the first error. 另一个错误是视图文件中的foreach,但基本上是由第一个错误触发的。
Thanks for your help. 谢谢你的帮助。
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 建立一个作业数组,然后用foreach()或最合适的输出
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.