简体   繁体   中英

Checkbox checked when edit in php

<?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; ?>

I need to check a checkbox automatically when edit in php. Here i entered a value static like 0,1,2. 0->SUN, 1->MON, 2->TUE. In this case if the foreach value is equal to checkbox value, the checkbox automatically checked. How can i solve this friends?

Please guide me

Thanks

<?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>
$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;
}
<?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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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