My latest challenge is to utilize Codeigniter for some new websites I am working on, things are going well but I am not running into a situation that I am not sure how to resolve.
I retrieve some results from the database (in my model), I can then display those results to the screen using a foreach loop, but what I am really needing to do is populate a dropbox with the results with the following format restraints.
I am retrieving 2 values from the database - number and name
In my dropbox I would like the number to be the value (so I can use it to retrieve more detailed results)
And I would like the name to be a combination of the number AND name from my original results
But in the dropbox (shown to the end user) I would like it to be:
‘2126 - SomeName’
‘3478 - SomeOtherName’
‘7931 - AnotherName’
with just the number as the value of each <option>
Hope I explained that clear enough for someone to assist, I am using CodeIgniter’s form helper (and maybe that is where I need to step back and do it “traditionally” so the call for my dropbox is:
echo form_dropdown(‘num’, $thelist);
Thanks in advance, hopefully someone can point me in a direction to look, I’ve been doing google searches and watching all sorts of things online to no avail thus far.
Here is partially working code, I rethought the problem and decided to CONCAT the two DB values through my DB query, this eliminates the need to try to put it together afterwards and I have a basic array. Now I just need to get it to be in the array format that CodeIgniter is looking for to populate a drop down input field on my form.
Anyone know how to get it to properly output so CodeIgniter can work with it?
Code from my MODEL
public function GetAllJobs() {
// Using ActiveRecords - set fields to retrieve data from
$this->db->select('Job_Number, CONCAT(Job_Number, " - ",Company_Name) as ListJob', FALSE);
// Using ActiveRecords - define the order by criteria
$this->db->order_by('Job_Number', 'asc');
// Using ActiveRecords - process the query against the DB
$query = $this->db->get('Jobs');
if($query->num_rows() > 0):
return $query->result();
else:
return false;
endif;
}
Code from my Controller
public function jobs() {
$this->config->set_item('page_title', 'Jobs Navigation Page');
$this->config->set_item('page_desc', 'View/Edit details pertaining to past and present jobs');
$this->config->set_item('main_content', 'jobs/main_nav');
$this->load->model('Jobs_model');
$JobList['AllJobs'] = $this->Jobs_model->GetAllJobs();
$this->load->view('includes/template', $JobList);
}
Code from my View
<div>
<?php
// THESE LINES OUTPUT THE EXAMPLE BELOW
// WHICH IS EXACTLY WHAT I AM EXPECTING
// JUST NEED TO POPULATE THE DROPDOWN WITH IT
// INSTEAD OF JUST ECHO'ING TO SCREEN
echo "Job List: <br />";
foreach($AllJobs as $EachJob) {
echo $EachJob->Job_Number . ", " . $EachJob->ListJob . "<br />";
}
// END OF CODE JUST TO TEST DATABASE ARRAY VALUES
// NOW START THE "TRUE" CODE FOR THE SITE
echo form_open('jobs/addeditjob');
echo form_label('Job # ', 'jobnum');
//echo form_dropdown('jobnum', $AllJobs); (THIS CODE CAUSES PAGE TO HANG)
echo form_close();
?>
</div>
Output of DB Query from test lines in View
2196, 2196 - Some Company
3991, 3991 - Another Company
4482, 4482 - Another Company
6531, 6531 - Different Business
7943, 7943 - Names Not Important
7982, 7982 - Names Not Important
I found a post online that has now allowed me to populate the dropdown, but it is setting the value and user displayed values, to be the same, here is the updated Model that creates code to populate the dropdown:
public function GetAllJobs() {
// Using ActiveRecords - set fields to retrieve data from
$this->db->select('Job_Number, CONCAT(Job_Number, " - ",Company_Name) as ListJob', FALSE);
// Using ActiveRecords - define the order by criteria
$this->db->order_by('Job_Number', 'asc');
// Using ActiveRecords - process the query against the DB
$query = $this->db->get('Jobs');
if($query->num_rows() > 0):
$JobList = $query->result();
foreach ($JobList as $CurrentJob) {
$ListAllJobs[$CurrentJob->Job_Number] = $CurrentJob->Job_Number; (This line is redundant, I had hoped it would use THIS as the VALUE for the option statements in the compiled HTML)
$ListAllJobs[$CurrentJob->ListJob] = $CurrentJob->ListJob;
}
$ListJobs = $ListAllJobs;
return $ListJobs;
else:
return false;
endif;
}
public function GetAllJobs()
{
$RESULT = array(); # Default
# Using ActiveRecords
# set fields to retrieve data from
$this->db->select('Job_Number, Company_Name', FALSE);
# define the order by criteria
$this->db->order_by('Job_Number', 'asc');
# process the query against the DB
$Jobs = $this->db->get('Jobs');
if($Jobs->num_rows() > 0):
$Jobs = $query->result();
foreach ($Jobs as $Job):
$RESULT[] = $Job->Job_Number => $Job->Job_Number .' - ' .$Job->Company_Name;
endforeach;
endif;
return $RESULT; # maybe empty array()
}