0

How to get Selected session in under foreach loop select option

<?php $tree = $this->helper('techinflo_shop')->getCategoriesDropdown(); ?>

          <?php 
              $categoryvalue = Mage::getSingleton('core/session')->getCategoryValue();
              $attributeValue = Mage::getSingleton('core/session')->getAttributeValue();
              $homecityvalue = Mage::getSingleton('core/session')->getHomeCityValue();
              $subcategoryvalue = Mage::getSingleton('core/session')->getSubCategoryValue();
          ?> 
<script type="text/javascript">
    var children = $H(<?php echo json_encode($tree['children']) ?>);
    function showCat(obj, level) {
        //hideContent();
        var catId = obj.value;
        level += 1;
        if ($('cat_container_' + level)) {
            $('cat_container_' + level).remove();
        }
        if (children.get(catId)) {
            var options = children.get(catId);
            var html = '<select class="form-control" name="subcategory" required="required" id="cat_' + catId + '" onchange="showCat(this, ' + level + ')">';
            html += '<option value=""disabled selected> Select </option>';
            for (var i = 0; i < options.length; i++) {
                html += '<option value="' + options[i].entity_id + '" >' + options[i].name + '</option>';
            }
            html += '</select>';
            html = '<div id="cat_container_' + level + '">' + html + '</div>';

            $('sub_cat').insert(html);
        }
    }
    /*window.onload=showContent();  
     function showContent(){
        document.getElementById("default-content").style.display="block";
    }
    function hideContent(){
        document.getElementById("default-content").style.display="none";
    }*/
</script>


<!-- <form  action="<?php echo $this->getUrl('catalogsearch/advanced/') ?>" method="post"> -->
<form action="<?php echo Mage::getUrl('location/index'); ?>" method="post" id="form-validate"> 
          <input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /> 


          <div class="brand">          
            <?php 
            $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'brand'); 
            $allOptions = $attribute->getSource()->getAllOptions(true, true); 
            ?> 
              <select class="attribute-brand form-control" id="attribute" required="required" name="attribute"> 
                  <option value="" disabled selected>Select</option> 
                  <?php foreach ($allOptions as $instance) {?> 
                  <option value="<?php echo $id = $instance["label"];?>"<?php echo ($attributeValue==$instance["label"])?'selected':'' ?>> 
                  <?php echo $value = $instance["label"];?> 
                  </option> 

              <?php }?> 
              </select> 
          </div> 


          <div class="category">
              <select id="first_cat" onchange="showCat(this, 2)" class="form-control" required="required" name="parentcate">
                  <option value="" disabled selected>Select</option>
                    <?php foreach ($tree['first'] as $cat): ?>
                      <option value="<?php echo $cat->getId() ?>" <?php echo ($categoryvalue==$cat->getId())?'selected':'' ?>><?php echo $cat->getName() ?>
                      </option>
                    <?php endforeach ?>
              </select>
          </div>

          <div class="select-city">
            <select class="form-control" id="mytext1" required="required" name="homecity">
                <option value="" disabled selected>Select</option>
                <option value="Bangalore" <?php echo ($homecityvalue=='Bangalore')?'selected':'' ?>>Bangalore</option>
                <option value="Calcutta" <?php echo ($homecityvalue=='Calcutta')?'selected':'' ?>>Calcutta</option>
                <option value="Chennai" <?php echo ($homecityvalue=='Chennai')?'selected':'' ?>>Chennai</option>
            </select>
        </div>

        <div id="sub_cat"></div>
        <!-- <div id="default-content">
    <div id="cat_container_va">
          <select class="form-control" id="mytext">
            <option disabled selected>Select</option>
          </select> 
      </div>
        </div> -->


          <button class="btn btn-success btn-block let-shop" type="submit">Let's Shop</button> 
</form>

it always display last value only of that dropdown

7
  • are u trying this <?php $attributevalue = Mage::getModel('core/cookie')->get('attribute'); $attribute = $attributevalue; echo $attribute;?>
    – Abdul
    Jul 29, 2016 at 4:53
  • Where i need to use
    – Magento 2
    Jul 29, 2016 at 4:53
  • can you send session code?
    – Abdul
    Jul 29, 2016 at 4:56
  • actually i wnat to under select option out side of that it will print but not in select option
    – Magento 2
    Jul 29, 2016 at 4:56
  • are you set data in custom session?
    – Abdul
    Jul 29, 2016 at 4:57

1 Answer 1

1

try this code

<form action="<?php echo Mage::getUrl('location/index'); ?>" method="post" id="form-validate"> 
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /> 
<div class="brand"> 
<?php $attributeValue = Mage::getSingleton('core/session')->getAttributeValue();?> 
<?php 
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'brand'); 
$allOptions = $attribute->getSource()->getAllOptions(true, true); 
?> 
<select class="attribute-brand form-control" id="attribute" required="required" name="attribute"> 
<option value="" disabled selected>Select</option> 
<?php foreach ($allOptions as $instance) {?> 
<option value="<?php echo $id = $instance["label"];?>"<?php echo ($attributeValue==$instance["label"])?'selected':'' ?>> 
<?php echo $value = $instance["label"];?> 
</option> 

<?php }?> 
</select> 
</div> 
<button class="btn btn-success btn-block let-shop" type="submit">Let's Shop</button> 
</form>
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.