dongren1353 2016-12-24 07:09
浏览 141
已采纳

检查foreach中的每个if语句

Why my if-statement not checking each array inside foreach ? If someone already choose same kind of value, I want to disable it from select2 value, here's my code looks like :

function get_status_cab()
    {
        $status=$_POST['search'];
        echo '<label for="username" class="col-sm-3 control-label">Status</label>';
        echo '<div class="col-sm-9">';
        echo '<select class="form-control select2" name="status">';
            $cek=$this->db_umum->select("SELECT id,status FROM lembaga where id='$status'");
            foreach($cek as $cek){
                $statusnya = $cek->status;
            }
            $hasil=array(
                "pusat" => "Pusat",
                "cabang" => "Cabang",   
                "unit" => "Unit",
                "proyek" => "Proyek",
                "subproyek" => "Subproyek"
            );
        foreach($hasil as $value => $label)
        {
            $no=0;
            if($value = $statusnya){
                $disable[] = "disabled='disabled'";
            }else{
                $disable[] = "";
            }
            echo "<option value='".$value."' $disable[$no]>".$label."</option>";
            $no++;
        }
        echo '</select></div><br/>'; 
    }

The result is if $value = $statusnya , my whole select2 will disabled. But, It is not what I want. I want only value which is exact with $statusnya is disabled and the other is available. Any advice ?

  • 写回答

4条回答 默认 最新

  • dpw5865 2016-12-24 07:14
    关注

    You are using single equal(=) thats not correct you should use == for compare.

    Replace this line

    if($value = $statusnya){
    

    with

     if($value == $statusnya){
    

    also define $no outside of foreach

    Example:-

    $no=0;
    foreach($hasil as $value => $label)
    {                
        if($value == $statusnya){
            $disable[] = "disabled='disabled'";
        }else{
            $disable[] = "";
        }
        echo "<option value='".$value."' $disable[$no]>".$label."</option>";
        $no++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置