Sarfaraz bheda Sarfaraz bheda - 3 months ago 29
PHP Question

Loading issue while fetching values of dropdown attribute in magento

There is one attribute named "Model" whose type is drop down. Attribute code is

wheel_model
.
This drop down have 2585 options.

I have used this drop down on many pages to allow user to select wheel model and filter product list.

Below is the code to fetch drop down value from database:

$arrval = array();
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'wheel_model');
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
$arrval[$option['value']] = $option['label'];
}


I am getting all drop down values in array and I am using it in phtml as follow:

<select name='wheel_model'>
<option>Select Model</option>
<?php
foreach ($arrval as $value => $label) {
echo "<option value='" . $value . "'>" . $label . "<option>";
}
?>
</select>


Its taking too much time to load drop down values. Is there any way to store this drop down in cache and retrieve from cache whenever required?

Answer

Use the code below to store data in the cache and that’s all.

    $cacheId = 'my_cache_id';
        if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
            $data = unserialize($data);
        } else {
            $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'wheel_model');
            foreach ($attribute->getSource()->getAllOptions(false) as $option) {
                $data[$option['value']] = $option['label'];
            }
            Mage::app()->getCache()->save(serialize($data), $cacheId);
        }