Welcome Guest, Not a member yet? Register   Sign In
matrix type of checkbox insert
#1

I am having multiple checkboxes like matrix formatted frames with checkboxex. each checkboxes have one value. 
when i submit the button i want to insert that checkbox values and that corresponding regno in DB.

Regno            checkbox values 

1001010        5001,5002,5003,5004,5005

I want to insert the tables in the format of
insert into stud values( 1001010,5001);
insert into stud values( 1001010,5002);
insert into stud values( 1001010,5003);
insert into stud values( 1001010,5004);
insert into stud values( 1001010,5005);

here checkbox is the post item
how do i get that corresponding regno as Post item?
Need help?

Attached Files Thumbnail(s)
   
Reply
#2

@kvanaraj,

There are many ways you can approach this:

One way would be to have a hidden value (in each row) that is updated whenever a checkbox is checked in a specific row. You will have to use javascript to accomplish this.

Another way is to generate a unique name for each checkbox that corresponds to the Regno. Then when the form is submitted all you have to do is verify if the checkbox is checked.

and there are more but this should help to get you started.
Reply
#3

(This post was last modified: 07-05-2018, 08:36 AM by jreklund.)

You already got a thread regarding this. And you need to read up on SQL Injection, you are an easy target.
https://forum.codeigniter.com/thread-71053.html

But here it goes. As this is just one question and easier yo understand. :-)

https://www.codeigniter.com/user_guide/h...m_checkbox
PHP Code:
echo form_checkbox('matrix[]','1001010_5001',TRUE// TRUE = Checked
echo form_checkbox('matrix[]','1001010_5002',TRUE// TRUE = Checked
... 

PHP Code:
$matrix $this->input->post('matrix[]');
$insert = array();
foreach(
$matrix as m) {
 
   list($regno,$id) = explode('_',$m);
 
   $insert[] = array(
 
       'regno' => intval($regno);
 
       'id' => intval($id);
 
   );


Now $insert looks like this.

PHP Code:
array(
 
   array(
 
       'regno' => 1001010 
        
'id' => 5001
    
),
    array(
        'regno' => 1001010 
        
'id' => 5002
    
)
...
); 

Use insert_batch to insert it into the database.
https://www.codeigniter.com/user_guide/d...rting-data
PHP Code:
$this->db->insert_batch('stud'$insert);
// Produces: INSERT INTO stud (regno, id) VALUES ('1001010', '5001'),  ('1001010', '5002') 
Reply
#4

my view to getting checkbox value
************************************

<td align="center"> <input type="checkbox" name="friend_id[]" value="<?php echo $row1['markid'];?>_<?php echo $row['regno'];?>" >
My controller
*************
function add_attend_studreg()
{
$this->load->library('form_validation');
$this->load->library('session');
$this->load->helper('url');
$this->form_validation->set_rules('friend_id','friend_id','required');
$matrix = $this->input->post('friend_id');

if(isset($_POST['Submit']))
{

$insert = array();
foreach($matrix as $m)
{
list($regno,$id) = explode ('_',$m);

//print_r($regno,$id);
$insert[] = array(
'item_id' =>intval($regno),
'user_id' =>intval($id)
);

$this->load->model('User_Model');
$this->User_model->studreginsert($insert);



}
}
}

My model
********
public function studreginsert($insert)
{
echo '<pre>';
print_r($insert);
echo '</pre>';
// echo "hai";
//$this->db->insert_batch('stud', $insert);
}

I got output as
****************


Array
(
[0] => Array
(
[item_id] => 50009
[user_id] => 2147483647
)

)
Array
(
[0] => Array
(
[item_id] => 50009
[user_id] => 2147483647
)

[1] => Array
(
[item_id] => 50011
[user_id] => 2147483647
)

)

2147483647 this type of value is not in my table. whats the problem in my query.kindly check it
Reply
#5

(This post was last modified: 07-05-2018, 11:34 PM by jreklund.)

Do you get correct values here?
PHP Code:
$matrix $this->input->post('friend_id');
echo 
'<pre>';
print_r($matrix);
echo 
'</pre>'

If so you need to check each line.
PHP Code:
foreach($matrix as $m)
{
echo 
'<pre>';
print_r($m);
echo 
'</pre>';
list(
$regno,$id) = explode ('_',$m);
echo 
'<pre>';
print_r($regno);
print_r($id);
echo 
'</pre>';
$insert[] = array(
'item_id' =>intval($regno),
'user_id' =>intval($id)
);
echo 
'<pre>';
print_r($insert);
echo 
'</pre>';


If you still got a problem, it's in the view.
Reply
#6

thank u very much . i changed my query now its working.

one more clarification

my original table like
main_tab (regno,id, ass1,att1,ass2,att2,ass3,att3)
I want to insert only regno,id these values only
In model how should i written
Reply
#7

(This post was last modified: 07-06-2018, 03:42 AM by jreklund.)

I would just declare them (ass1,att1,ass2,att2,ass3,att3) as NULL or set a default value in my table definition. (In MySQL)

So that you don't need to insert a default value from Codeigniter.
Reply
#8

once again i thank you. i just change my column values in the controller . it's perfectly working
Reply




Theme © iAndrew 2016 - Forum software by © MyBB