Welcome Guest, Not a member yet? Register   Sign In
How to parse check boxes in a dynamic html table?
#1

[eluser]moriokasan[/eluser]
I am trying to create a multiple selection using checkboxes inside an HTML table. In this case I am trying to assign multiple roles to a selected user. The table displays fine but I don't know how to parse the user selection from the view in order to call the model (eventualy with an array as parameter) to parse and update the table behind.


This is the controller that launches the view:
Code:
function index($id=0)
    {
                //check session
                $this->load->library('Mysession');
                if ($this->mysession->isInactive($this)) return;
        
    
                $this->load->model('users_roles_assignments_model');
                $data['records'] = $this->users_roles_assignments_model->select_all_for_user($id);
                $data['userid'] = $id;
                
              
                
                //reload page
                $data['main_content'] = 'users/users_roles_form';
                $this->load->view('includes/template',$data);
            
    }
This is the view
Code:
<br><br>
                <h1>Selected roles for current user</h1>
        <br><br>
        
        
        &lt;?php echo form_open('users/users_roles/save'); ?&gt;
        
                <table name="roles" id ="roles" border = 1 cellspacing=2 cellpadding=2 width="500">
            <tr>
                <th align="center">Selected</th>
                <th align="center">Role</th>
            </tr>
                &lt;?php if(isset($records)) : foreach($records as $row) : ?&gt;
            <tr>
                <td align="center">
                                        &lt;?php if ($row->userid != null): ?&gt;
                    &lt;input type="checkbox" name=&lt;?php echo "selected".$row-&gt;roleid; ?&gt; id=&lt;?php echo "selected".$row->roleid; ?&gt; value="true" />
                                        &lt;?php else: ?&gt;
                                        &lt;input type="checkbox" name=&lt;?php echo "selected".$row-&gt;roleid; ?&gt; id=&lt;?php echo "selected".$row->roleid; ?&gt; value="false" />
                                        &lt;?php endif; ?&gt;
                </td>
                <td>
                    &lt;?php echo $row->name; ?&gt;
                </td>
            <tr>
            &lt;?php endforeach; ?&gt;
            &lt;?php else: ?&gt;
                <tr>
                    <td colspan="2">
                        No roles are defined
                    </td>
                </tr>
            &lt;?php endif; ?&gt;
        </table>

                              

                &lt;input type="hidden" name="userid" id="userid" value=&lt;?php echo $userid; ?&gt;&gt;&lt;/input>

        <br>
        <p>
            &lt;input type = "submit" value="Save Changes"/&gt;
        </p>
                <br>
        &lt;?php echo form_close();?&gt;
        <br><br>


I appologize if I screw something up because this is my first post on the forum.
Any suggestion is greately appreciated! Thank you all!
#2

[eluser]toopay[/eluser]
Use php serialize() at the view and unserialize() at the controller, to transport an array.
#3

[eluser]moriokasan[/eluser]
I tried to used serialization and I don't know how to do it. Could you please be more specific? It may be trivial but I have never done it before. From the controller call to the view I have an array of values

Administrator -> false
Guest -> true

And in the view the user adds the administrator role as

Administrator -> true
Guest -> true

So I need this array/object to be sent after it was modified in the view by the user to my model in order to do the changes in the DB. Maybe I can use something like
Code:
$this->input->post('something')
to get the value or something else...
#4

[eluser]moriokasan[/eluser]
Does anyone have an example of this? Either with serialization or with JQuery... just to make it work.

Thanks all!
#5

[eluser]toopay[/eluser]
in your view, at checkbox section :
Code:
&lt;?php if ($row->userid != null): ?&gt;
&lt;input type="checkbox" name=&lt;?php echo "selected".$row-&gt;roleid; ?&gt; id=&lt;?php echo "selected".$row->roleid; ?&gt; value="&lt;?php echo serialize(array('administrator' => false, 'guest' => true)) ?&gt;" />
&lt;?php else: ?&gt;
&lt;input type="checkbox" name=&lt;?php echo "selected".$row-&gt;roleid; ?&gt; id=&lt;?php echo "selected".$row->roleid; ?&gt; value="&lt;?php echo serialize(array('administrator' => true, 'guest' => true)) ?&gt;" />
&lt;?php endif; ?&gt;

Then in your controller, you can either save it as serialized array, or unserialize your post value to array :
Code:
$foo = unserialize($this->input->post('something'));
#6

[eluser]moriokasan[/eluser]
I did exactly what you said and it works.

Thanks a lot for your answer! I was stuck on this for weeks...




Theme © iAndrew 2016 - Forum software by © MyBB