[eluser]Fielder[/eluser]
Let me set this up..
- My database has 2 tables; business and states.
- The states table has id(pk), state, and state_abbrev fields (and various others of no importance right now).
- The business table has id(pk), state_abbrev(fk), and business number fields (and various others of no importance right now).
So when i query for a particular business number from the business table, obviously the state_abbrev field returns the abbreviation. But in my view at the <select> element i don't want to show the abbreviation, I want to show the entire state name.
Code:
<select name="bus_state" style="width:150px;" class="required">
<?php
//need to add code here to show queried state as selected by default.
//display all states from getAllStates function
foreach($states as $row)
{
echo '<option value="'.$row['state_abbreviation'].'">'.$row['state_name'].'</option>';
}
?>
</select>
Do i run a secondary query through my method retrieving the state name associated with that abbreviation, before my final output $this->load->view(business_show.php, $data);?
**Keep in mind that I want the state from the query as the <selected> item in the drop down, while still populating the rest of the dropdown with my getAllStates function
Code:
$data['states'] = $this->States->getAllStates();
Code:
function getAllStates()
{
$this->db->select('state_abbreviation, state_name');
$query = $this->db->get('state');
if ($query->num_rows() > 0)
{
return $query->result_array();
}
}
I understand that this is a common database schema utilizing foreign-keys like this, so I imagine there's already a common practice for this.