2

I have a database table from which i am getting values and displaying it in a view page.Then the admin will click on the checkbox next to each row in the table.On click of each checkbox i will get the value of that checkbox.the code(view file and js) for it is this one

view file:

<input type="checkbox" name="options" id="options" value="<?php echo $row->slNo?>">
<input type="checkbox" id="selectAll" name="selectAll" />

js:

$('#selectAll').change(function(){
           if($(this).attr('checked'))
       {
        $('[name=options]').attr('checked',true);
        function updateTextArea() 
        {         
              var allVals = $('#rrol :checked').map(function()
            {
                   return $(this).val();
                }).get();
                $('#getrolevalues').val(allVals);
        }
        $(function() 
        {
             $('#rrol input').click(updateTextArea);
                 updateTextArea();
             });
        }
        else 
        {
             $('[name=options]').attr('checked',false);  
             function updateTextArea() 
             {         
             var allVals = $('#rrol :checked').map(function() 
                     {
                  return $(this).val();
              }).get();
              $('#getrolevalues').val(allVals);
         }
         $(function() 
         {
            $('#rrol input').click(updateTextArea);
            updateTextArea();
          });

          }
});

I have saved the values that i got in a hidden text box and im sending it to the controller.code( view file and js) for it is below

view file:

<input type="hidden" id="getrolevalues">

js:

$("#submit").click(function(){

          var js=$('#getrolevalues').val();
      $.post(url+"/roles/tabledata",{"gotoption":js},function(data){
          $('#roleschk').html(data.result);

},"json");

so far everything is working fine.but after sending the values which i have stored in an array im getting an error.i.e in controller i get the array values and then i use explode function to separate each value.then i use for loop to send those values to model and get the values and display the result.the code is below

controller:

function tabledata()
    {
         echo $data=$this->input->post('gotoption');
         $pieces = explode( ",", $data);
        //echo $pieces[1];
        //echo $e=count($pieces);
        $sql=array();
    for($i = 0; $i< count($pieces); $i++)
    { 
        $piec= $pieces[$i];
        $sql[]=$this->rolesmodel->roleselect($piec);
    }
    $sql['dipl']=$sql;

        $slr=$this->load->view('roleschecked',$sql,true);
        $value=array(
            'result'=>$slr
        );
        echo json_encode($value);
}

model:

function roleselect($piec)
    {
        /*for($i = 0; $i< count($pieces); $i++)
        { */
        echo $chkroles="SELECT * FROM dummyroles WHERE slNo='$piec'";

        $qry=$this->db->query($chkroles);
//      }

        return $qry;
    }

also the roleschecked view file where i display the results:

<p>You Have selected the following persons</p>
<table border=2>
<tr>
<th>Sl.No</th>
<th>Name</th>
<th>Roles</th>
</tr>
<?php foreach($dipl as $row)
{?>
<tr>
<td><?php echo $row()->slNo[0]?></td>
</tr>
<?php }?>
</table>

the problem now is that only last value i get when i use like this in controller

.........
.........
$sql['dipl']=$this->rolesmodel->roleselect($piec);
.........

and if i use like this i get an error

.........
$sql[]=$this->rolesmodel->roleselect($piec);
.............
$sql['dipl']=$sql;

please help me with this issue

2
  • all you options have the same id "options". this wrongly wrong (and can cause some mess). Your best option is to remove the id from the "options" (or add a counter after them, or even the value). Commented Jan 3, 2012 at 6:57
  • error is->Fatal error: Call to a member function result() on a non-object in C:\wamp\www\newatmindia\application\views\roleschecked.php on line 9 and the view file showing error is <p>You Have selected the following persons</p> <table border=2> <tr> <th>Sl.No</th> <th>Name</th> <th>Roles</th> </tr> <?php foreach($dipl->result() as $row) {?> <tr> <td><?php echo $row()->slNo?></td> <td><?php echo $row()->name?></td> <td><?php echo $row()->role?></td> </tr> <?php }?> </table> Commented Jan 3, 2012 at 7:01

2 Answers 2

1

Why are you writing all checkboxes comma separated in a hidden field?

You can post them as straight as an array:

<input type="checkbox" name="options[]" value="123" />
<input type="checkbox" name="options[]" value="543" />

in your controller you get the checked boxes as

$options = $this->input->post('options');

So your tabledata() function will look like this:

function tabledata(){

    $options = $this->input->post('options');
    $sql=array();
    foreach($options as $option){
        $sql[]=$this->rolesmodel->roleselect(intval($option));
    }

    // I'm not sure why you are assigning this back into the same array?!
    $sql['dipl']=$sql;
    $slr=$this->load->view('roleschecked',$sql,true);
    $value=array(
        'result'=>$slr
    );
    echo json_encode($value);
}

Please take a look at the commented line, where you assign all SQL statements back into the sql array, that looks odd to me.

Sign up to request clarification or add additional context in comments.

4 Comments

the reason im writing all checkboxes comma separated in a hidden field is because i am getting these chkbox values from a table which i got from model.the view page and model page is below
view page: <body> <div id="rrol"> <table id="roletable" border=1> <tr> <th>SL.NO</th> <th>NAME</th> <th>ROLES</th> </tr> <?php foreach ($display->result() as $row) { ?> <tr id="rowId"> <td class="classID"><?php echo $row->slNo?>.</td> <td class="classID" name="namz"><span><?php echo $row->name?></span></td> <td class="classID"><?php echo $row->role?></td> <td ><input type="checkbox" name="options" id="options" value="<?php echo $row->slNo?>"> </td> </tr> <?php }?> </table> </div>
and the model is function selectroles() { $slct="SELECT * FROM dummyroles"; $qry=$this->db->query($slct); return $qry; }
From the code in your comment it's still possible to use option[]. Also you are assigning the ID option several times which is invalid.
0

For me it looks like konsolenfreddy suggested - look at $sql['dipl'] assignment. When you first time assign values to $sql you have array of query results. Then again you assign $sql, but you do it on the same array - $sql, but to with index 'dipl'. What you get is array of arrays. Look at the example:

$aSql = array();
$aSql[] = 'foo';

$aSql['bar'] = $aSql;

var_dump($aSql);

result >>>

array(2) {
  [0]=>
  string(3) "foo"
  ["bar"]=>
  array(1) {
    [0]=>
    string(3) "foo"
  }
}

Try to rename your inner $sql, like this:

// function tabledata()
$innerSql = array();
for($i = 0; $i < count($pieces); $i++) { 
    $piec = $pieces[$i];
    $innerSql[] = $this->rolesmodel->roleselect($piec);
}

$sql['dipl'] = $innerSql;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.