• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing Multople Checkbox Values in Database

#1
[eluser]xitclub[/eluser]
Hi all,
This is my first post in community.
The problem is i wanted to store multiple checkbox values in database but its storing only first value in database.
my code is

Form
Code:
<div class="row ex-row">
   <div class="col-lg-3 col-md-3 col-sm-3 ex-col">
     <h4>Area of Expertise</h4>
   </div>
   <div class="col-lg-9 col-md-9 col-sm-9 ex-chks">
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex1" value="iOS" name="expertise[]"&gt; iOS (Native)
     </label>
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex2" value="Android" name="expertise[]"&gt; Android (Native)
     </label>
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex3" value="HTML5" name="expertise[]"&gt; HTML5
     </label>
     <br />
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex4" value="Corona" name="expertise[]"&gt; Corona
     </label>
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex5" value="Titanium" name="expertise[]"&gt; Titanium
     </label>
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex6" value="BlackBerry" name="expertise[]"&gt; BlackBerry
     </label>
     <br />
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex7" value="PhoneGap" name="expertise[]"&gt; PhoneGap
     </label>
     <label class="checkbox-inline">
       &lt;input type="checkbox" id="ex8" value="Unity" name="expertise[]"&gt; Unity
     </label>
   </div>

Model Code
Code:
public function UpdateExpertise($id) {
$user_id = $id;
$position = $this->security->xss_clean($this->input->post('position'));
$expertise = $this->security->xss_clean($this->input->post('expertise'));
     foreach($expertise as  $value){
                $ex_val = $value.",";
     }
$experience = $this->security->xss_clean($this->input->post('experience'));
$freelaning = $this->security->xss_clean($this->input->post('freelancing'));
if(isset($_POST['submit'])){
     $data = array(
     'user_position' => $position,
     'user_expertise' => $ex_val,
     'user_experience' => $experience,
     'freelancing' => $freelaning
  );
     $this->db->where('user_id', $user_id);;
     $query = $this->db->update('users', $data);
     if($query){
  return TRUE;
     }
     else {
  return FALSE;
  echo $this->db->display_errors();
     }
}

    }

#2
[eluser]InsiteFX[/eluser]
you need to check each checkbox, If the checkbox is not checked then it will not return it's emtpy.
If no checkboxes are checked, the checkbox will not be set, so use the “empty” function to check for this case.

so check to see if it is checked use isset() if not you need to assign a value to it.

Code:
// Just ruff code you can modify it. This is for an array of checkboxes.
public function is_checked($chk_name, $value)
    {
        if ( ! empty($this->input->post($chk_name))
        {
            foreach ($this->input->post($chk_name]) as $chk_val)
            {
                if ($chk_val == $value)
                {
                    return true;
                }
            }
        }
        return false;
    }


#3
[eluser]Tim Brownlaw[/eluser]
Just some ideas for you...

In your Model you can replace this section

Code:
foreach($expertise as  $value) {
$ex_val = $value.",";
}

This has the small issue of appending a comma at the end of the string... which makes exploding it a lil tricky... There are of course ways around that but you might be better off using the php implode and explode functions ( read up on that in php.net)
with something like...

Code:
// You will want to check that $expertise is an Array with values
// A Form Validation check should check that for us... So we know something has been checked!
  $ex_val = implode(',',$expertise); // This replaces the foreach loop used above

So you could do a callback on the form validation for 'expertise' to ensure that at least one entry has been checked before getting to the model call... ie check that it's an array and that it's count($expertise) > 0
Read the user guide on form validation callbacks...

As always, it's a good thing to do a var_dump($_POST) or you can use the built in CI $this->input->post()by way of var_dump($this->input->post()); Same difference!

I'd also recommend a read of https://ellislab.com/codeigniter/user-gu...input.html
AND https://ellislab.com/codeigniter/user-gu...ation.html
The user guide is your friend!

Cheers

#4
[eluser]xitclub[/eluser]
Hi, Thanks guys for solutions, this solved my problem Smile

#5
[eluser]Tim Brownlaw[/eluser]
Glad to hear it and you are welcome Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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