爆炸并设置条件php

时间:2018-11-12 06:06:00

标签: php codeigniter

Array ( [0] => 1 [1] => 2 )

我正在尝试根据用户权限设置if条件,其想法是根据给定值显示菜单,如果为1,将显示一个菜单,将显示2秒菜单,如果两者都显示,则将显示所有值。到目前为止,对于单个值,我已经做对了,但是如何对值数组进行操作 这是我的代码

<?php
   $userid = $this->phpsession->get("user_id");
   $userrole = $this->phpsession->get("user_type");

   $query = $this->db->select("role_empid,role_permissions")->from('hw_role')->where('role_empid', $userid)->get()->result();
   $data = $query[0]->role_permissions;

   if($data == 1){
?>
      <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
      <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
<?php }else if($data == 2){ ?>
       <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
<?php } ?>
</ul>

3 个答案:

答案 0 :(得分:0)

如果role_permission包含json对象,该对象首先将其转换为数组,例如

    @Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Test App';
  constructor(private authService:AuthService){}
  public userName;
  public permission;
  public jwtHelper = new JwtHelperService();
  public menuItems=[];

 setMenu(){
    if(this.authService.loggedIn()){
        const token = localStorage.getItem('token');
        const decodedToken = this.jwtHelper.decodeToken(token);
        this.userName = decodedToken.userName;
        this.hospitalName = decodedToken.hospitalName;
        this.permission = decodedToken.permission;
        if(this.permission == 'admin'){
            this.menuItems = this.adminMenuItems;
        }
        else{
            this.menuItems = this.nurseMenuItems;
        }
    }

  }

否则直接检查条件

$data = $query[0]->role_permissions;
$data=json_decode($data);

答案 1 :(得分:0)

如果$query[0]->role_permissions是具有不同值的数组,则可以尝试:

       <ul>
       <?php for($i = 0; $i<count($data);$i++) {
       if($data[$i] == 1){ ?>

       <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
       <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>

       <?php }
        if($data[$i] == 2){ ?>

       <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>

       <?php }} ?>
       </ul>

答案 2 :(得分:0)

尝试一下

<?php
  if(in_array(1,$data)){
?>
     <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
     <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
<?php }
  if(in_array(2,$data)){ 
?>
     <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
<?php } ?>