简体   繁体   English

从控制器传递值以在Codeigniter中查看

[英]Passing a value from controller to view in codeigniter

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. $ data包含一系列用户权限。

     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. 但是问题是,当我回显$ Admin或$ CreateUser时,我看不到复选框的正确状态(无论是否选中)。 I want to pass the value as it is in controller, to view. 我想传递它在控制器中的值以进行查看。

Any help will be highly appreciated. 任何帮助将不胜感激。

I figured out my mistake. 我发现了我的错误。 The $pieces is can not be accessed globally because it is declared within a loop. $ pieces无法全局访问,因为它是在循环中声明的。 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数组,而不是编写逻辑。

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

Now I can get the correct status correctly. 现在,我可以正确获取正确的状态了。

<?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 尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM