Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:14:54+00:00 2026-06-10T16:14:54+00:00

I am new to CodeIgniter and working with arrays. I am trying to get

  • 0

I am new to CodeIgniter and working with arrays. I am trying to get the value of each check box into the same row in the database. I know I need to loop through the array, but then I am not sure how to tell it to write the values under their corresponding headings in the database. This is what I have tried but its writing 0 to the database for all options. Thanks to anyone taking the time to awnser my question.

//view

<input type="text" name="name" /> Name
<input type="checkbox" value="1" name="options[]" /> Option 1
<input type="checkbox" value="2" name="options[]" /> Option 2
<input type="checkbox" value="3" name="options[]" /> Option 3
<input type="checkbox" value="4" name="options[]" /> Option 4

//controller

//require at least 1 checkbox
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('options[]', 'Options', 'required');

//not sure how to do this part, 
//some how get values to the corresponding "option"
$name = $this->input->post('name');
$option1 = $this->input->post('options[0]');
$option2 = $this->input->post('options[1]');
$option3 = $this->input->post('options[2]');
$option4 = $this->input->post('options[3]');

//insert to database
$this->load->model('add', 'add_model');
$this->add_model->create_person($name, $option1, $option2, $option3, $option4);

//database

| id | name | option1 | option2 | option3 | option4 |

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:14:56+00:00Added an answer on June 10, 2026 at 4:14 pm

    I personally would have different named checkboxes and would validate by jQuery in the view before sending the form, but here’s the code that will point you in a right direction.

    When you submit your form options array will have as many elements as you have selected on the form. If no checkbox has been selected the array would be empty so your code:

    $option3 = $this->input->post('options[2]');
    

    would be trying to get the third element of an empty array.

    You need to loop through existing elements and assign those values to your variables.
    You can do that by using this code:

    $myArr = array(1 => 0, 2 => 0, 3 => 0, 4 => 0);   //set all the variables to 0
    foreach($_POST['options'] as $checked)          //loop throught the selected options
    {
        $myArr[$checked] = $checked;                     //assign to corresponding array element                            
    }
    
    $this->add_model->create_person($name, $myArr[1], $myArr[2], $myArr[3], $myArr[4]);
    

    The code above isn’t particularly elegant but I hope it will help you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to MVC and Codeigniter and am trying to get a class working.
I'm pretty new to codeigniter and I am trying to get to know databases
I am still new into Codeigniter framework. Today I read about Database Caching http://codeigniter.com/user_guide/database/caching.html
I am new to Codeigniter but for some reason I can't get it working.
I'm working on a new website based on CodeIgniter. I'm trying to hide some
I'm new to CodeIgniter and have come into an issue with the insertion of
I am using Codeigniter and I am trying to upload multiple files into a
I am trying to get started with the PHP MVC framework, CodeIgniter. I am
I'm working on a new php project with CodeIgniter and I have few questions
I am trying to fit a stylesheet into view with CodeIgniter, please bare in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.