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?
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);
}
Related
I would need some assistance on the below problem:
I'm trying to display qty value from db via option tag, but the option tag keeps adding on previous row value onto subsequent option tag row value. Would appreciate any advice/solution on this, thanks.
$arrayqty = $_SESSION['pqty'];
<div id='qty'>
<?php
$options = array();
$options[] = "<option value=''></option>";
foreach($arrayqty as $qty )
{
$sql = mysql_query ("SELECT * FROM products WHERE qty = '$qty'");
$row = mysql_fetch_array($sql);
$nqty = $row['qty'];
for($x=1; $x<=$nqty; $x++)
{
$options[] = "<option value='{$x}'>{$x}</option>";
}
?>
<select class="list" >
<?php echo implode( $options); ?>
</select>
<?php
}
?>
</div>
Try moving your $options array inside of your foreach loop.
foreach($arrayqty as $qty ) {
$options = array();
$options[] = "<option value=''></option>";
In your foreach after echo implode( $options); use:
unset($options);
Or if you want to keep the empty option try this
$options = array();
$options[] = "<option value=''></option>";
I'm pulling 3 colummns from one table and storing them in an array as such to populate a dropdown menu. The ref_code is used to decide which dropdown it will go to ('Module','Customer','Application') while id is the select value and ref_desc is the display text.
foreach ($test as $t) {
$aa[] = array($t->ref_code => array('id'=>$t->id, 'ref_desc'=>$t->ref_desc));
};
I've been trying to retrieve them using $aa['ref_code'] but have not been getting any success. Help please.
This question is similar, but I am unable to retrieve the values I want.
I found a solution on another forum but I will also give your solutions a try later. Thank you very much!
foreach ($aa as $a => $d) {
foreach ($d as $ref_code => $dd) {
echo "<p>". $ref_code ."</p>";
echo "<p>". $dd['ref_desc'] ."</p>";
echo "<p>". $dd['id'] ."</p>";
};
};
Why don't you write it like this? The ref_code (if it's unique) is the key of the array.
foreach ($test as $t) {
$aa[$t->ref_code] = array('id'=>$t->id, 'ref_desc'=>$t->ref_desc);
};
you need a three dimensional array:
dropdown => ids => displaytext
here we go:
// fill in:
$aa = array();
foreach ($test as $t) {
if ( !isset($aa[$t->ref_code]) )
$aa[$t->ref_code] = array();
$aa[$t->ref_code][$t->id] = $t->ref_desc;
};
// have a drop down:
$myDropDown = 'Customer';
foreach ( $aa[$myDropDown] as $id => $displaytext )
{
echo "<option value=\"" . $id . "\">" . $displaytext . "</option>";
}
.... etc.
I have:
$selectedItem = '100,200,300';
And mysql load:
<select multiple="multiple" name="product_id">
<?php
$query = mysql_query("SELECT * FROM products ORDER BY id");
while($row = mysql_fetch_array($query)){
echo '<option value="$row[id]">$name</option>';
}
?>
</select>
And I need $selectedItem (three values) to be:
<option value="$row[id]" selected>$row[id]</option>
How can I do that? I just can't find the solution when the selectedItem more than 1.
You should make your $selectedItem variable an array (and name it plural since it can have multiple values; just my added preference =P):
$selectedItems = array(100, 200, 300);
Doing this, you'll be able to check if the current row's id is in the array or not (with in_array()) and then "select" that row:
while($row = mysql_fetch_array($query)) {
$selected = in_array($row['id'], $selectedItems) ? ' selected="selected"' : '';
echo '<option value="' . $row[id] . '"' . $selected . '>' . $row['id'] . '</option>';
}
Alternatively, if you can't "define" your list of selected items as an array and you will only have a comma-separated string, you can use explode() to turn it into an array:
$selectedItems = explode(',', $selectedItem);
This will split the $selectedItem variable by commas into an array stored into $selectedItems (usable with the above code).
You can do it like this:
while($row = mysql_fetch_array($query)){
if ( str_pos( $selectedItem.',' , $row[id].',' ) !== false) {
$sel = 'selected';
} else {
$sel = '';
}
echo "<option value='$row[id]' $sel>$row[id]</option>";
}
I'm doing this:
$sql_glassware = 'SELECT id, name FROM glassware';
$qry_glassware = $con->query($sql_glassware);
$get_glassware = $qry_glassware->fetchAll(PDO::FETCH_ASSOC);
debug($get_glassware);
debug() is a personal function; It returns the result like this:
$get_glassware = array(19) {
[0]=>array(2) {
["id"]=>string(1) "1"
["name"]=>string(8) "Cocktail"
}
[1]=>array(2) {
["id"]=>string(1) "2"
["name"]=>string(9) "Margarita"
}
[2]=>array(2) {
["id"]=>string(1) "3"
["name"]=>string(8) "Highball"
}
...
}
I'm guessing the first array level is the rows, and the second level is the columns.
don't know why it return the id as strings thoug...
Then I'm using a class to construct a complete form; I have a public function called addSelect() where the first argument takes an array of values to build the options list: array('name0','name1', 'name2','...') and do a foreach()-loop inside:
public function addSelect($opt=array(),$param=array())
$name = $this->useEither($param['name'],'dropdown-list'); // useEither() is a personal function
foreach ($opt as $val => $name){
$options .= '<option value="'.$val.'">'.$name.'</option>';
}
$select = '<select name="'.$name.'" '.$param['string'].'>'.$options.'</select>';
$this->formElements[] = $select; // store the list for use later
}
How can re-write this litte function so I can pass $get_glassware directly into my function first argument and have it output the options like this:
<option value="1">Cocktail</option>
<option value="2">Margarita</option>
<option value="3">Highball</option>
Try this:
public function addSelect($opt=array(),$param=array())
$name = $this->useEither($param['name'],'dropdown-list'); // useEither() is a personal function
foreach ($opt as $option){
$options .= '<option value="'.$option['id'].'">'.$option['name'].'</option>';
}
$select = '<select name="'.$name.'" '.$param['string'].'>'.$options.'</select>';
$this->formElements[] = $select; // store the list for use later
}
I'm not sure why or what do you want to do there but your foreach in addSelect overwrites the $name variable.
But as far as I understand your problem, that should do the trick for you.
foreach ($opt as $val => $name){
$options .= '<option value="' . $name['id'] . '">' . $name['name'] . '</option>';
}
For the debug function... usually print_r is easier to read instead of var_dump but for debugging maybe sometimes still necessary.
I need help with some coding. I need to get a list of All Manufacturers with their corresponding magento ID. Is that possible? Please help. thanks. I tried some mods but only get one or the other. If its possible, pls help w/ this one last thing. I thank you in advance
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'manufacturer');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attributeArray[$option['value']] = $option['label'];
}
foreach($attributeArray as $key=>$val){
echo $val;
}
Not sure exactly what format you require this in but the following example should illustrate how to get to the values you need:
$attribute = Mage::getModel('eav/entity_attribute')
->loadByCode('catalog_product', 'manufacturer');
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getData('attribute_id'))
->setStoreFilter(0, false);
$preparedManufacturers = array();
foreach($valuesCollection as $value) {
$preparedManufacturers[$value->getOptionId()] = $value->getValue();
}
if (count($preparedManufacturers)) {
echo "<h2>Manufacturers</h2><ul>";
foreach($preparedManufacturers as $optionId => $value) {
echo "<li>" . $value . " - (" . $optionId . ")</li>";
}
echo "</ul>";
}