-1

all categories coming selected which i want to see only which information is coming from database. sorry for my english.

<div class="col-xs-4">
  <label>Categories</label>
    <!-- SEARCH JS find (selectpicker) -->
  <select name="product_cat" id="product_cat" class="form-control selectpicker" data-live-search="true" multiple>
    <?php 

    while($productcatcek=$productcatsor->fetch(PDO::FETCH_ASSOC)) {

    ?>
    
      <option selected="">
        <dl>
          <dt ><?php
          if ($productcatcek['pcat_id']==$productcatcek['pcat_id']) {
             echo $productcatcek['pcat_name'];
           } 
          ?> - </dt>
          <dd <?php echo $productcatcek['pcat_id']==$productcatcek['pcat_id'] ? 'selected=""' : '' ?>><?php echo $productcatcek['spcat_name']; ?></dd>
        </dl>
      </option>
    
  <?php } ?>
  </select>
  <!-- SEARCH Finish -->
</div>

SOLUTION

<option value="<?php echo $productcatcek['spcat_id']; ?>" <?php echo $productcatcek['spcat_id']==$productcek['product_cat'] ? 'selected=""' : '' ?>>
  <dl>
   <dt><?php echo $productcatcek['pcat_name']; ?> - </dt>
   <dd><?php echo $productcatcek['spcat_name']; ?></dd>
  </dl>
 </option>

1 Answers1

0

The selected attribute only needs to exist on your <option> element; it doesn't need to have a value. To not have an <option> selected, simply don't include the attribute

divillysausages
  • 7,883
  • 3
  • 25
  • 39
  • If you check the image I added, It will explain it fully. my all categories coming selected. i just want to see only which category added on my mysql database. – Yusuf Butun Apr 29 '21 at 07:40
  • I tried this :: and this :: – Yusuf Butun Apr 29 '21 at 07:42
  • The code `$productcatcek['pcat_id']==$productcatcek['pcat_id'] ? 'selected=""' : ''` is always going to return `true`, because you're comparing the string to itself, so the `selected` attribute is always going to be present – divillysausages Apr 29 '21 at 08:07