Welcome Guest, Not a member yet? Register   Sign In
Displaying Query Result ids in Select box
#1

[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">
                        &lt;?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>';
                        }
                        ?&gt;
                    </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.


Messages In This Thread
Displaying Query Result ids in Select box - by El Forum - 04-26-2009, 10:51 AM
Displaying Query Result ids in Select box - by El Forum - 04-26-2009, 12:29 PM
Displaying Query Result ids in Select box - by El Forum - 04-26-2009, 06:05 PM
Displaying Query Result ids in Select box - by El Forum - 04-26-2009, 07:06 PM
Displaying Query Result ids in Select box - by El Forum - 04-26-2009, 08:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB