how to use an optgroup in a foreach? - php

In my select box I am trying to do this:
Maincategory name
subcat
subcat
subcat
anothermain category
subcat
subcat
This is the code I have at the moment:
<select name="categories" class="form-control col-sm-12">
<?php foreach(ProductModel::categories() as $category): ?>
<option value="<?=System::escape($category->sub_category); ?>"><?=System::escape($category->sub_category); ?></option>
<?php endforeach; ?>
</select>
I don't want to show the main cate name on all the sub cats like
maincate:subcat
maincate:subcat
maincate:subcat
This is my table:
How would I go about doing this? I cant do if there's 5 inside the foreach show cat name because I'll never have the same amount of subcats in each main cats

If I'm understanding correctly, before your for each loop, you could assign a variable to the current main category name (starting it as null), then check it every time you iterate to see if it matches the current entry's main category. If it doesn't, have it close the previous optgroup and open a new optgroup for the main category and then another for the current sub category. Then assign the variable to the current main category value and you should be good to go. You'd probably have it skip the close tag if the variable was undefined (i.e., the first time 'round).
Something like this:
So:
<select name="categories" class="form-control col-sm-12">
<?php
$i = 0;
$mainCatName = '';
foreach(ProductModel::categories() as $category):
if($category->main_category != $mainCatName {
$mainCatName = $category->main_category;
if($i = 0) { ?>
<optgroup label="<?php echo $category->main_category ?>">
<?php } else {
</optgroup>
<optgroup label="<?php echo $category->main_category ?>">
<?php }
}
?>
<option value="<?=System::escape($category->sub_category); ?>"><?=System::escape($category->sub_category); ?></option>
<?php if($i = count(count(ProductModel::categories()) { ?>
</optgroup>
<?php }
$i++;?>
<?php endforeach; ?>
</select>

Related

adding category and its subcategories in html select

i am trying to add father categories then its subcategories as options in select , so i did the following
<select class="form-control" name="select">
<?php foreach($father as $father) : ?>
<option value="<?php echo $father->id; ?>">
*Category: <?php echo $father->name." :-"; ?>
</option><?php foreach($child as $child) : ?><?php if($child->category_id==$father->id){?>
<option value="<?php echo $child->id; ?>">
-Sub-Category: <?php echo $child->name; ?>
</option><?php } endforeach; ?><?php endforeach; ?>
</select>
here is my controller
public function addg()
{
$this->load->model('catagories_model');
$data['userdata'] = $this->session->all_userdata();
$data['father'] = $this->catagories_model->get_catagories();
$data['child'] = $this->catagories_model->get_catagories_child();
$this->load->view('catagories/grand/add', $data);
}
but it loops into the first father category and display its children and just displays the other father categories without going into the if statement what could be wrong ?
This line isn't doing anything:
<?php foreach($child as $child) : ?>
It should be:
<?php foreach($childs as $child) : ?>
Assuming $childs is the array where you're storing all the subcategories.

How to create custom attribute to be a child of other attribute as dropdown catalog input type

I'm trying to create something like part finder (product finder).
I was thinking to edit layered navigation.
So now I have drop-boxes instead of list using this code for
app/design/frontend/default/carpoint/template/catalog/layer/filter.phtml
<select onchange="setLocation(this.value)">
<?php $count = 0; ?>
<?php foreach ($this->getItems() as $_item): ?>
<?php $count++; ?>
<?php if($count == 1): ?>
<option value='' disabled selected style='display:none;'>Choose <?php echo $attribute_code = $_item->getFilter()->getName();?> </option>
<?php endif; ?>
<option <?php if ($_item->getCount() > 0): ?> value="<?php echo $this->urlEscape($_item->getUrl()) ?>">
<?php echo $_item->getLabel() ?> <?php else: echo '>' . $_item->getLabel() ?> <?php endif; ?> (<?php echo $_item->getCount() ?>) </option>
<?php endforeach; ?>
</select>
How can I edit custom attribute eg "Type" so he is associated with parent attribute eg "Brand".
I something like this:

Optgroups are closed after 1 item. Dynamic list

I have a select form object in index.php. The items are gotten from mySQL. What im trying is to achieve is: Create an optgroup whenever catID = NULL. In this case Section1 and Section2 should be optgroups. But the optgroups are closed after 1 option. Any tips? screenshot:
database->table:
catId catName
NULL --Section1--
1010 Bilar
1020 Bildelar & tillbehör
1030 Motorcycklar
1040 Motorcycklar delar & tillbehör
1050 Moped
1070 Lastbil & Truck
NULL --Section2--
2010 Bygg och Trädgår
2020 Möbler & Heminredning
2030 Husgeråd & Vitvaror
2040 Verktyg
HTML:
<?php $count = 0; ?>
<?php foreach($categories as $category ): $count++;?>
<?php if($category['catId']==NULL){?>
<optgroup label="<?php $category['catName']?>">
<?php } else{ ?>
<option value="<?php echo $category['catId'];?>"><?php echo $category['catName'];?></option>
<?php
$temp = $categories[($count+1)];
if($temp['catId']==NULL)?>
</optgroup>
<?php } ?>
<?php endforeach;?>
</select>
Try this :
<?php $first_opt = TRUE; ?>
<?php foreach($categories as $category ): ?>
<?php if($category['catId']==NULL){?>
<?php if($first_opt):?>
<optgroup label="<?php echo $category['catName']?>">
<?php $first_opt = FALSE;?>
<?else:?>
</optgroup><optgroup label="<?php echo $category['catName']?>">
<?endif;?>
<?php } else{ ?>
<option value="<?php echo $category['catId'];?>"><?php echo $category['catName'];?></option>
<?php } ?>
<?php endforeach;?>
</optgroup>
</select>

Magento - displaying parent and child categories in a dropdown menu

We're currently working on our category pages within Magento and have the following code to provide a list of child categories for the category we are in.
We have the following setup:
Store
- Cat 1 - Gloss Colours (Parent)
-- Sub cat 1 - Zurfiz (Child)
-- Sub cat 2 - Parapan (Child)
Screenshot of dropdown: http://awesomescreenshot.com/048kga35a
We are looking to modify the code to show a option of "Show All" and link to the parent category within the dropdown menu as well as the child categories, does anyone know what code we should pull up for this?
From app\design\frontend\default[our template]\template\catalog\navigation\left.phtml
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories=$this->getCurrentChildCategories() ?>
<?php
$oLayer = null;
$oCat = $this->getCurrentCategory();
# level 3 categories display their category siblings
if (3 === (int)$oCat->getData('level')) {
# get the layer object
$oLayer = Mage::getSingleton('catalog/layer');
$oParentCategory = $oCat->getParentCategory();
# set the parent category as the current category
$oLayer->setData('current_category',$oParentCategory);
# load the child categories
$_categories = $this->getCurrentChildCategories();
}
?>
<?php if($_categories->count()): ?>
<div class="box layered-nav">
<?php
# switch back the current category
if (null !== $oLayer) $oLayer->setData('current_category',$oCat);
?>
<script language="javascript" type="text/javascript" >
function jumpto(x){
if (document.form1.jumpmenu.value != "null") {
document.location.href = x
}
}
</script>
<div class="cat-dropdown">
<form name="catlist">
<select name="jumpmenu" onChange="jumpto(document.catlist.jumpmenu.options[document.catlist.jumpmenu.options.selectedIndex].value)">
<option value="#">Choose a brand</option>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<option value="<?php echo $this->getCategoryUrl($_category) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> (<?php echo $_category->getProductCount() ?>)</option>
<?php endif; ?>
<?php endforeach ?>
</select>
</form>
</div>
</div>
<?php endif; ?>
Use this code..
<?php
//$id = whatever
$subcats = Mage::getModel('catalog/category')->load($id)->getAllChildren();
?>
<select name="jumpmenu">
<option value="#">Choose a brand</option>
<?php
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
//print_r($_category);
?>
<option value="<?php echo $_category->getCategoryUrl() ?>"><?php echo htmlEscape($_category->getName()) ?></option>
<?php } ?>
</select>

Brand wise searching in product orderer report

I am trying to extend product order
report and add one Attribute of
product brand and put into
grid.phtml.
when i select brand and click on
refresh button.
report will not display brand wise
searching what can i do?
product order report image link
Visit
http://imageshack.us/photo/my-images/405/productorder.jpg/
grid.phtm Code
<?php
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('brand')
->getFirstItem();
$attributeOptions = $attributeInfo->getSource()->getAllOptions(false);
?>
</div>
--><div class="f-left">
<?php echo $this->__('Brand') ?>:
<select name="brand" id="brand" style="width:6em;">
<?php foreach ($attributeOptions as $_value=>$_label): ?>
<option value="<?php echo $_value ?>" <?php if($this->getFilter('brand')==$_value): ?> selected<?php endif; ?>><?php echo $_label['label'] ?></option>
<?php //error_log("Lable".print_r($_label,TRUE));?>
<?php endforeach; ?>
</select>
</div>

Categories