Welcome Guest, Not a member yet? Register   Sign In
Generate queries from check boxes and display results [ RESOLVED ]
#1

[eluser]exodus7[/eluser]
Hi,

I seem to have run into a small snag that I can't seem to figure out...

This is what I'm trying to do:

a user has job postings in a database. A view is generated to show jobs along with a check box that stores the ID# from the DB. A user can then check each job posting they want to print or view details about, hit the submit button & have a detailed report of each job posting displayed.


I figured out how to generate the view to show the jobs with the check boxes and to generate the queries for each, but I cant figure out how to display them...

Code:
// from the form with the checkboxes
<input type="checkbox" name="job[]" value="<?=$row->id?>">


Code:
// generate queries

$job = $this->input->post('job');

foreach ($job as $checkbox => $job_id)
{
$this->db->where('id', $job_id);
$jobs[] = $this->db->get('job_postings');
}

So my question is how do I display the results from the queries?

when I do print_r($jobs) is shows:

Code:
Array ( [0] => CI_DB_mysql_result Object ( [conn_id] => Resource id #29 [result_id] => Resource id #44 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 1 [row_data] => ) [1] => CI_DB_mysql_result Object ( [conn_id] => Resource id #29 [result_id] => Resource id #45 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 1 [row_data] => ) [2] => CI_DB_mysql_result Object ( [conn_id] => Resource id #29 [result_id] => Resource id #46 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 1 [row_data] => ) [3] => CI_DB_mysql_result Object ( [conn_id] => Resource id #29 [result_id] => Resource id #47 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 1 [row_data] => ) )

Any help or ideas would be greatly appreciated Smile

Thanks!
#2

[eluser]slowgary[/eluser]
You would need to call the results method on your query, like so:
Code:
$this->db->get('job_postings')->results();  //or ->results_array();

Then you can foreach the results.

I would not recommend running your query like that. You're going to run a bunch of queries in your above foreach loop, one for each job. Instead, you should get all the jobs from one single query. You can use something like where_in() to get all the jobs that match your array of job_ids. If you're using PHP 5, you can also chain the database methods, making things a little cleaner and easier.

Try this:
Code:
$jobs = $this->input->post('job');

foreach($jobs as $key => $job_id)
{
     $job_id_array[] = $job_id;
}

$jobs_info = $this->db->where_in('id', $job_id_array)->get('job_postings')->result_array();

foreach($jobs_info as $job_info)
{
     echo $job_info['job_name'].$job_info['job_desc'].$job_info['job_salary'];
}

This assumes your database table has fields named job_name, job_desc, job_salary. You should break this code up though and put the first foreach in your controller, the database call in your model, and the second foreach in your view.

Good luck.
#3

[eluser]exodus7[/eluser]
This works perfectly - MANY THANKS! Smile

I was unaware of where_in - that makes a big difference especially if there are many ID's.

I'm learning something new every day Smile

Thanks slowgary!
#4

[eluser]slowgary[/eluser]
No problem.




Theme © iAndrew 2016 - Forum software by © MyBB