Facyo Kouch
Facyo Kouch

Reputation: 787

Pass checkbox value to database including other form value ?

I have a form with various input fields and 3 checkboxes. How can i store these checkbox value in de database in the same field of the database?

This is my code

The form:

<div class="pro-input pro-field">
    <label for="name" class="label_title">Name</label>
    <?php echo form_input($title); ?>
</div><!--pro-input-->

<div class="pro-text pro-field">
    <label for="name" class="label_text">Text</label>
    <?php echo form_textarea($text); ?>
</div>

<div class="pro-input pro-field">
    <label for="name" class="label_title">URL</label>
    <?php echo form_input($url);?>
</div><!--pro-input-->

<div class="pro-checkbox pro-field">
    <label for="name" class="label_title">Categorie</label>
    <div class="pro-check">
        <?php
        echo form_label('Code', 'code');
        echo form_checkbox('tags[]', 'code');
        echo form_label('design', 'design');
        echo form_checkbox('tags[]', 'design');
        echo form_label('other', 'other');
        echo form_checkbox('tags[]', 'other');
        ?>
    </div>
</div>

The Controller:

public function create()
{
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->form_validation->set_rules('title', 'Title', 'trim|required');
    $this->form_validation->set_rules('text', 'Text', 'trim|required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('admin/header', $data);
        $this->load->view('admin/index', $data);
        $this->load->view('admin/footer', $data);

    }
    else
    {
        $this->portfolio_model->set_project();
        $this->load->view('admin/index', $data);

    }
}

The model:

public function set_project()
{
    $this->load->helper('url');

    $slug = url_title($this->input->post('title'), 'dash', TRUE);

    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'text' => $this->input->post('text'),
        'url' => $this->input->post('url'),
        'tags' => $this->input->post('tags')        
    );

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

The value in the database shows '0'. How can i show the 3 values in the same field if they are all checked?

Any help would be highly appreciated. Thank you

Upvotes: 1

Views: 224

Answers (1)

stormdrain
stormdrain

Reputation: 7895

The only way to store them all in the same db field would be to concatenate the values and store that:

$tags_value = implode("|", $this->input->post('tags');
//should produce something like "code|design|other"

Or you could create additional fields for the tags and use those (this will save having to process the concatenated string on the other end, as will the last option):

projects_table

id|slug|text|url|tag_code|tag_design|tag_other
----------------------------------------------
 1|hi  |coo |z.c|       0|         1|        1

Additionally, you could create another table and use that:

tags_table

id|project_id|tag
----------------------
 1|       123|design
 2|       123|code
 3|       123|other

Upvotes: 2

Related Questions