hriy hriy - 3 months ago 10
MySQL Question

SOLVED - Post Multiple Checkbox from Table To Database Using Codeigniter

I've the problem to post my checkbox to database. Please help me...

Here is my view :

<table>
<tr>
<td>
<input type='hidden' name='userid[]' value='1'>
<input type='text' name='username[]' value='username1'>
</td>
<td>
<input type='checkbox' name='as_admin[]' value=1>
</td>
</tr>
<tr>
<td>
<input type='hidden' name='userid[]' value='2'>
<input type='text' name='username[]' value='username2'>
</td>
<td>
<input type='checkbox' name='as_admin[]' value=1>
</td>
</tr>
</table>


This is my controller :

$this->Model_user->insert_user();


And this is my model :

function insert_user(){
$user_count = count($this->input->post('userid'));
$userid = $this->input->post('userid');
$username = $this->input->post('username');
$as_admin = $this->input->post('as_admin');

for ($i=0; $i < $user_count; $i++){
$info_user = array(
'user_id' => $userid[$i],
'user_name' => $username[$i],
'as_admin' => $as_admin[$i],
);
$this->db->insert($info_user);
}
}


And the problem is when 'username2' mark as admin (second row checkbox checked), in the database will be like this :

|user_id|user_name|as_admin|
| 1 |username1| 1 |
| 2 |username2| 0 |


it should be like this :

|user_id|user_name|as_admin|
| 1 |username1| 0 |
| 2 |username2| 1 |


Does anyone now how to save those thing, Please...

Thanks in advance...

Answer

Try this, in your view, use the user id as key for as_admin

<table>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='1'>
            <input type='text' name='username[]' value='username1'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[1]' value=1>
        </td>
    </tr>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='2'>
            <input type='text' name='username[]' value='username2'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[2]' value=1>
        </td>
    </tr>
</table>

in your model

function insert_user(){
    $user_count = count($this->input->post('userid'));
    $userid     = $this->input->post('userid');
    $username   = $this->input->post('username');
    $as_admin   = $this->input->post('as_admin');

    for ($i=0; $i < $user_count; $i++){
        $info_user = array(
            'user_id'   => $userid[$i],
            'user_name' => $username[$i],
            'as_admin'  => isset($as_admin[$userid[$i]]) ? 1 : 0,
        );
        $this->db->insert($info_user);
    }
}

You are having this problem because, want you check only 1 checkbox, only checked value will be passed, so if you count the $as_admin you will get only 1 out of 2 as_admin.

if you make admin the username2 only

then your post will be as below

Array
(
    [userid] => Array
        (
            [0] => 1
            [1] => 2
        )

    [username] => Array
        (
            [0] => username1
            [1] => username2
        )

    [as_admin] => Array
        (
            [2] => 1
        )
)
Comments