2017-09-27 26 views
-1
<?php 
$repeat_days = "0,1,2"; 
$repeat_array = explode(",", $repeat_days); 
foreach ($repeat_array as $repeat_array1): ?> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="0">SUN 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="1">MON 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="2">TUE 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="3">WED 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="4">THU 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="5">FRI 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="6">SAT 
    </label> 
<?php endforeach; ?> 

我需要在php中編輯時自動檢查複選框。在這裏我輸入了一個像0,1,2一樣的靜態值。 0-> SUN,1-> MON,2-> TUE。在這種情況下,如果foreach值等於複選框值,則複選框會自動檢查。我該如何解決這個朋友?複選框在php中編輯時檢查

請指引我

感謝

+0

您的代碼創建21的複選框。他們應該檢查哪些? –

回答

2
<?php 
$repeat_days = "0,1,2"; 
$repeat_array = explode(",", $repeat_days); 

    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="0" if(in_arrray('0',$repeat_array)){ echo 'checked';} >SUN 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="1" if(in_arrray('1',$repeat_array)){ echo 'checked';} >MON 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="2" if(in_arrray('2',$repeat_array)){ echo 'checked';} >TUE 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="3" if(in_arrray('3',$repeat_array)){ echo 'checked'; }>WED 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="4" if(in_arrray('4',$repeat_array)){ echo 'checked';} >THU 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="5" if(in_arrray('5',$repeat_array)){ echo 'checked';} >FRI 
    </label> 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="6" if(in_arrray('6',$repeat_array)){ echo 'checked';} >SAT 
    </label> 
+0

我想你的意思是'in_array()'(只有兩個r)。 –

+1

他是爆炸的字符串,然後它返回一個數組,它會檢查值是否可用。 –

+1

我的觀點是您的示例解決方案中存在錯誤(拼寫錯誤)。 –

1
$days = array("SUN","MON","TUE","WED","THU","FRI","SAT"); 
$checked = array(0,1,2); 


foreach ($days as $key => $day) { 
    if (in_array($key, $checked)){ 
     $status = "checked"; 
    } else { 
     $status = ""; 
    } 

    $checkbox = ' 
    <label class="checkbox-inline"> 
     <input type="checkbox" name="day[]" value="'.$key.'" '.$status.'>'.$day.' 
    </label> 
    '; 
    echo $checkbox; 
} 
+0

嘿你的編碼對我有用,謝謝 –

+0

太好了,謝謝你讓我知道。當你有機會時接受答案。 – dontanios

1
<?php 

$days = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'); 

$repeat_days = "0,1,2"; 
$repeat_array = explode(",", $repeat_days); 

foreach($days as $index => $day) { 
    echo '<label class="checkbox-inline"><input type="checkbox" name="day[]" value="'.$index.'"'.(in_array($index,$repeat_array)? ' checked' :'').'>'.$day.'</label>'; 
}