Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selected = selected in Codeigniter PHP

I am trying to set selected in <option> tag.

What is wrong with my code?

My Code is:

<select required  name="site-list"   class="form-control" style="margin: 9px 0px 0px 0px;">

<option value="">Select</option>  

<?php foreach($MLCSites as $site) { ?>

<option id="emp"  value="<?php echo $site->site_key;?>" <?php if($site->id == $site->site_key){ echo "selected";}   ?>><?php echo $site->site_name;?></option>
      <?php } ?>

 </select>

Genrated HTML:

<select required="" name="site-list" id="site-list" class="form-control" style="margin: 9px 0px 0px 0px;">        
<option value="">Select  Site</option>  
<option value="HT45-YT6T">bizRTC</option>
<option value="EB22-0309">RTCBiz</option>
</select>

What should be my comparison to set selected = selected ??

Table:

Tables

like image 497
Rajan Avatar asked Apr 21 '26 21:04

Rajan


1 Answers

You should use 'selected=selected' and to avoids error, remove id="emp", because id is for one value

foreach ($MLCSites as $site) : ?>

    <option value="<?= $site->site_key; ?>"
        <?php if ($site->id == $site->site_key) :
            echo "selected=selected";
        endif; ?>>
        <?= $site->site_name; ?>
    </option>
<?php endforeach; ?>
like image 116
callmemath Avatar answered Apr 24 '26 11:04

callmemath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!