Postingan lainnya
view multiple checkbox
bagaimana value multiple checkbox tercentang dari database pada codeigniter
<?php
foreach($spesifikasi_komputer as $k){ ?>
<div class="form-group">
<label class="col-sm-3 control-label">Software <b style="color:red;">*</b></label>
<div class="col-sm-9">
<label><input type="checkbox" class="form-control" name="software[]" alt="Checkbox" value="<?php echo $k->software;?>" required> <?php echo $k->software;?></label>
<?php foreach($software as $s){ ?>
<label><input type="checkbox" class="form-control" name="software[]" alt="Checkbox" value="<?php echo $s->nama;?>" required> <?php echo $s->nama;?></label>
<?php } ?>
</div>
</div>
<?php } ?>
1 Jawaban:
<div>gunakan function in_array pada php, contoh :<br><br></div><pre><?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?></pre><div><br>contoh penerapan pada checkbox menggunakan in array & ternary operator :</div><pre><input type="checkbox" value="<?php echo $row->id; ?>" <?php (in_array($row-id, $software) ? 'checked' : ''); ?>></pre>