Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Re-populating a select/dropdown list generated from a database table using set_value()
#1

[eluser]edjon2000[/eluser]
Hello all Smile

First of all, I have read through some similar topics on this subject, some of which helped partially with generating the dropdown list in the first place, but I couldn't find an example of re-populating the fields that worked for me, which leads me to suspect that it is probably something I am doing wrong.

This is part of a project I am working on for a client at this moment, so any help with this would be greatly appreciated, I am quite new to CodeIgniter so please be kind Wink

Ok, first of all I needed to generate a list of say, vacancies from a vacancy_list table but for the purposes of the subsequent search on the main table I needed the option values and labels to be the same, after trying various combinations, in my model I used this

Code:
/**
     * The generate_vacancy_list method generates the data to populate
     * the vacancy dropdown menu for the vacancy search form
     *
     * Returned object (array of)
     * --------------------------
     * vac_list_name
     *
     * @return array
     */
    function generate_vacancy_list() {
        /**
         * Generate return array for vacancy field dropdown options
         */
                $vacancy_keys = array();
                $vacancy_keys_arr = array();
                $vacancy_vals_arr = array();

        $vacancy_keys = $this->db->query('SELECT DISTINCT(vac_list_name) FROM vacancy_list ORDER BY vac_list_id');

        foreach ($vacancy_keys->result_array() as $vacancy_key) {
            $vacancy_keys_arr[] = $vacancy_key['vac_list_name'];
            $vacancy_vals_arr[] = $vacancy_key['vac_list_name'];
        }

        $query = array_combine($vacancy_keys_arr, $vacancy_vals_arr);

        return $query;
    }

Now I think there could ba a problem in there, maybe in the way I am constructing the returned results, this is handled by the controller as follows

Code:
/**
     * Vacancy Functions
     */
    function add_vacancy()
    {
        /** rules for form validation **/
        $this->form_validation->set_error_delimiters('<span class="flashError">', '</span>');

        $this->form_validation->set_rules('client_name', 'Client\'s Name', 'trim|required|min_length[3]|max_length[50]');
        $this->form_validation->set_rules('client_address', 'address', 'trim|max_length[1024]');
        $this->form_validation->set_rules('client_phone', 'phone number', 'trim||min_length[3]|max_length[50]');
        $this->form_validation->set_rules('client_email', 'email address', 'trim|min_length[3]|max_length[50]|valid_email');
        $this->form_validation->set_rules('client_postcode', 'postcode', 'trim|min_length[3]|max_length[10]');
        $this->form_validation->set_rules('vacancy_name', 'vacancy', 'trim|required');
        $this->form_validation->set_rules('vacancy_salary', 'salary', 'trim|required');
        $this->form_validation->set_rules('vacancy_sector', 'sector', 'trim|required');
        $this->form_validation->set_rules('vacancy_area', 'location', 'trim|required');
        $this->form_validation->set_rules('vacancy_desc', 'description', 'trim|max_length[5000]');
        $this->form_validation->set_rules('vacancy_active', 'please select an option', 'required');
        $this->form_validation->set_rules('vacancy_featured', 'please select an option', 'required');

        if($this->form_validation->run())
        {
            //load the check vacancy page
        }



        /** rebuild add vacancy page view with errors if unsuccessful **/
        $data = array();
        $data['page_title'] = 'Add New Vacancy';
        $data['main_content_1'] = 'admin_views/admin_add_vac_view';

        $data['vac_name'] = $this->vacancy_model->generate_vacancy_list();

        $data['sec_name'] = $this->vacancy_model->generate_sector_list();
        $data['sal_amount'] = $this->vacancy_model->generate_salary_list();

        $this->load->view('includes/template', $data);
        //echo "<h1>Here is the add new vacancy page</h1>";
    }

Which, in turn, is passed to the view

Code:
<label for="vacancy">Vacancy Name<span class="req">required</span></label>
            &lt;?php    echo form_dropdown('vacancy', $vac_name, set_value('vacancy'), 'id="vacancy"');    ?&gt;

But, for some reason, I cannot get it to re-display the selected field, the actual generated dropdown list works fine and all of the other non-dropdown fields re-display correctly i.e. input text and input textarea fields worked fine, here's an example of the source listing

Code:
<label for="vacancy">Vacancy Name<span class="req">required</span></label>
            <select name="vacancy" id="vacancy">
<option value="All Specialists">All Specialists</option>
...
</select>

My apologies for the long post, but I wanted to supply as much information as possible.

I look forward to your responses


Messages In This Thread
[SOLVED] Re-populating a select/dropdown list generated from a database table using set_value() - by El Forum - 12-27-2010, 09:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB