• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set array value

#1
[eluser]Unknown[/eluser]
I have a form with multiple select box which is allow user to grab several options, I was stumbled on how to insert multiple selected values into database, the argument to hold multiple values is $category, currently the table is only inserted the last value from what I were selected.

here is my code:

Model:

Code:
function save_listing($user_id, $cname, $reg_code, $desc, $category)
{

    $data = array(
        'user_id' => $user_id,
        'company_name' => ucwords(strtolower($cname)),
        'register_code' => $reg_code,
        'descriptions' => $desc,
        'category_id' => $category
    );

    $this->db->insert('listing', $data);

    return TRUE;

}


Controller

Code:
public function create()
{
    if(!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login');

    }else{

        $this->form_validation->set_rules('cname', 'Company Name', 'trim|required|xss_clean|min_length[5]|max_length[50]|is_unique[listing.company_name]');
        $this->form_validation->set_rules('reg_code', 'Company No.', 'trim|xss_clean|min_length[5]|max_length[10]');
        $this->form_validation->set_rules('desc', 'Descriptions', 'trim|xss_clean|min_length[25]|max_length[500]');
        $this->form_validation->set_rules('category[]', 'Category', 'required');


        if($this->form_validation->run()) { // validation ok

            if(!is_null($data = $this->db_model->save_listing(
                $user_id,
                $this->form_validation->set_value('cname'),
                $this->form_validation->set_value('reg_code'),
                $this->form_validation->set_value('desc'),
                $this->form_validation->set_value('category[]')
            ))) { // success

                $this->session->set_flashdata('msg', 'Saved!');
                redirect(current_url());
            }

        }else{

            $errors = $this->tank_auth->get_error_message();
            foreach($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
        }

        $data['catOpts'] = $this->db_model->getCategories();
        $data['s_cate'] = $this->input->post('category');

        $this->load->view('header', $data);
        $this->load->view('create_new', $data);
        $this->load->view('footer', $data);

    }

}

View

Code:
$category = array(
'name' => 'category',
'id' => 'category'
);

<select name="&lt;?php echo $category['name'] ?&gt;[]" id="&lt;?php echo $category['id'] ?&gt;" multiple="multiple">
                        &lt;?php
                        foreach($catOpts as $catOpt)
                        {
      
                         echo '<option value="'.$catOpt-&gt;category_id.'">'.$catOpt->category_name.'</option>';
                        }
                        ?&gt;
                    </select>

I been tried to echo $this->form_validation->set_value('category[]'); but it still show up only the first selected value even there are several are selected and submit.

I've tried print_r($s_cate), it returned me:

Code:
Array ( [0] => 1 [1] => 5 [2] => 7 )

I needed someone to help me about on how to insert array values into database.

Thanks.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.