Welcome Guest, Not a member yet? Register   Sign In
Checkboxes and the database
#1

[eluser]Skoobi[/eluser]
Hi I'm trying to add checkbox values to the database from a form but can seem to get them all in to the same field.

What i want to do is have 5 or more different checkboxes so 'item1', 'item2', 'item3' and so on then if item1 and 3 are selected it will post them to the 'Items' field in the database as they are described in the values so in this example i want it to display in the database like this 'item1, item3' ...

Is this possible or do i need to explode the array before sending it to the database??

Heres my code so far :

View:
Code:
<div class="panel-body">
      <div class="checkbox">
        
          <p><label>&lt;input type="checkbox" name="features[]" value="about"&gt; About</label></p>
        
          <p><label>&lt;input type="checkbox" name="features[]" value="contact"&gt; Contact</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="services"&gt; Services</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="catalog"&gt; Interactive catalogue</label></p>
  
          <p><label>&lt;input type="checkbox" name="features[]" value="newsletter"&gt; Newsletter</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="youtube"&gt; YouTube</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="facebook"&gt; Facebook</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="twitter"&gt; Twitter</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="blog"&gt; Blog</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="directions"&gt; Directions</label></p>

          <p><label>&lt;input type="checkbox" name="features[]" value="images"&gt; Images / Gallery</label></p>
      
      </div>

    </div>


Controller:
Code:
public function newApp()
{
  $this->is_logged_in();

  $this->form_validation->set_rules('customerid', 'Customerid', 'trim|xss_clean|numeric');
  $this->form_validation->set_rules('title', 'Application Title', 'trim|required|max_length[12]');
  $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
  $this->form_validation->set_rules('webaddress', 'Web Address', 'trim|required');
  $this->form_validation->set_rules('businessname', 'Business Name', 'trim|required|max_length[20]');
  $this->form_validation->set_rules('telephone', 'Telephone Number', 'trim|required|max_length[14]|numeric');
  $this->form_validation->set_rules('order_status', 'Order Status', 'numeric');
  $this->form_validation->set_rules('published', 'Published', 'numeric');
  $this->form_validation->set_error_delimiters('  <span class="text text-danger"><small>','</small></span>');

  if ($this->form_validation->run() == FALSE)
  {  
   $this->load->view('admin/includes/header');
   $this->load->view('admin/apps/new');
   $this->load->view('admin/includes/footer');
  }
  else
  {
   $this->admin_model->create_app();
   $this->session->set_flashdata('message', '<div class="alert alert-success"><p>Application created!!!</p></div>');
   redirect('admin/apps', 'refresh');
  }
}

Model:
Code:
public function create_app()
{
  $data = array(
   'customerid' => $this->input->post('customerid'),
   'title' => $this->input->post('title'),
   'email' => $this->input->post('email'),
   'webaddress' => $this->input->post('webaddress'),
   'businessname' => $this->input->post('businessname'),
   'telephone' => $this->input->post('telephone'),
   'features' => $this->input->post('features[]'),
   'order_status' => $this->input->post('order_status'),
   'published' => $this->input->post('published'),

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

Cheers
Chris
#2

[eluser]InsiteFX[/eluser]
Code:
//PHP
json_ecode($features);

// Save it into a text field in your database table

// to get it back
$text = field from your table;
$features = json_decode($text);
#3

[eluser]Skoobi[/eluser]
Brilliant ill give that a go tonight. Many thanks
#4

[eluser]Skoobi[/eluser]
Hi I've tried that but its not working with me I'm sure its me not putting it in the right place. Where should i use the encode ???

Many thanks
#5

[eluser]InsiteFX[/eluser]
You would code it in your form validation if statement if it passes validation.

You will need to load the checkboxes from the database before you display the form.

Code:
if ($this->form_validation->run() == FALSE)
  {  
   $this->load->view('admin/includes/header');
   $this->load->view('admin/apps/new');
   $this->load->view('admin/includes/footer');
  }
  else
  {
       // Place the code for it here.

   $this->admin_model->create_app();
   $this->session->set_flashdata('message', '<div class="alert alert-success"><p>Application created!!!</p></div>');
   redirect('admin/apps', 'refresh');
  }
#6

[eluser]CroNiX[/eluser]
Code:
$data['features'] = implode(', ', $this->input->post('features[]'));
//$data['features'] now = "item1, item2, item3"

$this->db
  ->where('id', $some_id)
  ->update('table', $data);
#7

[eluser]Skoobi[/eluser]
You are an absolute legend many thanks for your help that worked a treat....
#8

[eluser]Skoobi[/eluser]
I spoke too soon lol...

Now that I'm on the editing bit how do i get the values back and check the correct checkboxes??
#9

[eluser]CroNiX[/eluser]
Code:
//controller
$features = $this->db
  ->select('features')
  ->get('table')
  ->where('id', $some_id)
  ->row_array();

$features = explode(', ', $features);
$data = array();
foreach($features as $feature)
{
  $data['features'][] = $feature['feature'];
}

//in view
<p><label>&lt;input type="checkbox" name="features[]" value="about" &lt;?php if (in_array('about', $features)) echo 'checked="checked"'; ?&gt;&gt; About</label></p>

It would be something like that...didn't test it




Theme © iAndrew 2016 - Forum software by © MyBB