检查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 ?

为什么我的if语句没有检查foreach中的每个数组? 如果某人已经选择了相同类型的值,我想从select2值中禁用它,这里的代码如下所示: p>

  function get_status_cab()
 {
 $ status =  $ _POST ['search']; 
 echo'&lt; label for =“username”class =“col-sm-3 control-label”&gt; Status&lt; / label&gt;'; 
 echo'&lt; div class =  “col-sm-9”&gt;'; 
 echo'&lt; select class =“form-control select2”name =“status”&gt;'; 
 $ cek = $ this-&gt; db_umum-&gt; select  (“SELECT id,status FROM lembaga,其中id ='$ status'”); 
 foreach($ cek as $ cek){
 $ statusnya = $ cek-&gt; status; 
} 
 $ hasil = array  (
“pusat”=&gt;“Pusat”,
“cabang”=&gt;“Cabang”,
“unit”=&gt;“单位”,
“proyek”=&gt;“Proyek”,\  n“subproyek”=&gt;“Subproyek”
); 
 foreach($ hasil as $ value =&gt; $ label)
 {
 $ no = 0; 
 if($ value = $ statusnya){  
  $ disable [] =“disabled ='disabled'”; 
} else {
 $ disable [] =“”; 
} 
 echo“&lt; option value ='”。$ value。“'$ disable  [$ no]&gt;“。$ label。”&lt; / option&gt;“; 
 $ no ++; 
} 
 echo'&lt; / select&gt;&lt; / div&gt;&lt; br /&gt;';  
} 
  code>  pre> 
 
 

结果是如果$ value = $ statusnya,我的整个select2将被禁用。 但是,这不是我想要的。 我只想要与$ statusnya完全相同的值被禁用而另一个可用。 任何建议? p> div>

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++;
}

$disable = arrary();
foreach($hasil as $value => $label)
        {
            $no=0;
            if($value = $statusnya){
                $disable[$no] = "disabled='disabled'";
            }else{
                $disable[] = "";
            }
            echo "<option value='".$value."' $disable[$no]>".$label."</option>";
            $no++;
        }

$disable = array();
foreach($hasil as $value => $label)
{
    $no=0;
    if($value == $statusnya){
        $disable[$no] = "disabled='disabled'";
    }else{
        $disable[] = "";
    }
    echo "<option value='".$value."' $disable[$no]>".$label."</option>";
    $no++;
}

I know you already accepted an answer, and that this isn't codereview.stackexchange.com but I hope you will consider the information below.

Use the equality operator (I.e. ==) instead of the assignment operator (I.e. =) to check if $value is equal to $statusnya. Also you don't need to use an array to store the disabled attribute (unless you need to retain information about each option being disabled later).

foreach($hasil as $value => $label)
{       
      $disabled = ''; // empty string -> not disabled         
     if($value == $statusnya){
           $disabled = "disabled='disabled'";
     }
     echo "<option value='".$value."' $disabled>".$label."</option>";
 }

The array storage requires more memory on the server compared to the string so as your page/application grows it would require more resources. Take a look at this phpfiddle and see How big are arrays?.