0
votes

I have a checked-box list in my view where I am getting values loaded from database.

    <input type="checkbox"  id="CheckBoxAdmin" name="CheckBoxAdmin" <?php echo $Admin; ?>    />Admin
    <input type="checkbox"  id="CheckBoxCreateUser" name="CheckBoxCreateUser" <?php echo   $CreateUser; ?>  />Create User

I am passing value to this through my controller.

     $this->load->view('edituserview', $data);

$data contains an array of the user privileges.

     for($i=0;$i<$max;$i++)
     {
     if( $pieces[$i]=="Admin"  ) 
      {
         $data['Admin']="checked";
      }
      else
      {
        $data['Admin']="";
      }
      if( $pieces[$i]== "Create User") 
      {
          $data["CreateUser"]="checked";
      }
      else
      {
        $data["CreateUser"]="";
      }

     }

      $this->load->view('edituserview', $data);

But the problem is,I can't see the correct status of checked box (whether it is checked or not) when I echo $Admin or $CreateUser. I want to pass the value as it is in controller, to view.

Any help will be highly appreciated.

2
there probably isn't enough info to help you debug. first ensure that $pieces[$1] has the expected value but echoing it. then ensure $data has the expected values by print_r($data). - PK.
Can I ask what is inside of $pieces and what you're gathering from $i? - James Lalor
@PK I am sorry I didn't include the whole code because it includes model file and some lengthy logic at controller. I got the expected data when I print the $pieces[$i] individually. - Yash
@JamesLalor Inside $pieces I am gathering a set of privileges. For an example , when I echo $pieces[$i];, I can get "Admin". $i is used to iterate through the loop. I am sorry I made you all confused without the full code. - Yash
Well you're overwriting your admin every time - James Lalor

2 Answers

0
votes

I figured out my mistake. The $pieces is can not be accessed globally because it is declared within a loop. In my view file I write the logic as below,

    <?php
    $Admin="";
    $CreateUser="";

    $prv= element('privileges',  $users);
    $pieces = explode(",", $prv);
    $data['priviledges']= str_replace(array('[',']'),'',$pieces);
    $pieces= str_replace(array('[',']'),'',$pieces);
    $max = sizeof($pieces);

     if (in_array('Admin', $pieces, true))
     {
        $Admin="checked";
     }

      if(in_array("Create User", $pieces, true)) 
      {
          $CreateUser="checked";
      }

      ?>

In my controller file I am just passing the $data array rather than writing the logic.

     $data['users'] = $this->Usermodel->RetrieveUsers( $userid);
     $this->load->view('edituserview', $data);

Now I can get the correct status correctly.

0
votes
<?php
  $data['Admin'] = false;
  $data['CreateUser'] = false;
  for($i=0;$i<$max;$i++) {
    if($pieces[$i] == "Admin") {
      $data['Admin'] = true;
    }
    if($pieces[$i] == "Create User") {
      $data['CreateUser'] = true;
    }
  }

  $this->load->view('edituserview', $data);
?>

try this