manufacturer list alphabetical order

in Block File:

<?php
class Bysoft_Mypage_Block_Manufacturer extends Mage_Core_Block_Template  {
	
	public function getAllBrands($cate_id, $first='') {
		
		$is_test = true;//display all brands ignore category
		
		$manu_content = array();
		
		if ($is_test ===false) {
			// filter by category 
			$category           = Mage::getModel('catalog/category')->load($cate_id);
			$layer              = Mage::getSingleton('catalog/layer');
			$layer->setCurrentCategory($category);
			$attributes = $layer->getFilterableAttributes();
			$manufacturers = array();
			foreach ($attributes as $attribute) {
				if ($attribute->getAttributeCode() == 'manufacturer') {
					$filterBlockName = 'catalog/layer_filter_attribute';
					$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
					foreach($result->getItems() as $option) {
						$manufacturers[] = $option->getData();
					}
				}
			}
		} else {
			//get all, ignore category
			$product = Mage::getModel('catalog/product');
			$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
			->setEntityTypeFilter($product->getResource()->getTypeId())
			->addFieldToFilter('attribute_code', 'manufacturer');
			$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
			$manufacturers = $attribute->getSource()->getAllOptions(false);
		}
		
		$this->array_sort_by_column($manufacturers, 'label');
		
		foreach ($manufacturers as $key=>$m) {
			$manufacturers[$key]['first'] = mb_substr($m['label'],0,1,'utf-8');
			if ($first == '1') {
				$number = array('0','1','2','3','4','5','6','7','8','9');
				if (in_array($manufacturers[$key]['first'], $number)) {
					$manu_content[] = $manufacturers[$key];
				}
			} else if ($first) {
				if ($first == $manufacturers[$key]['first']) {
					$manu_content[] = $manufacturers[$key];
				}
			} else if ($first == '') {
				$manu_content[] = $manufacturers[$key];
			}
		}
		return array('title'=>$manufacturers,'content'=>$manu_content);
	}
	

	function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
		$sort_col = array();
		foreach ($arr as $key=> $row) {
			$sort_col[$key] = $row[$col];
		}
		array_multisort($sort_col, $dir, $arr);
	}
	
	public function getAllBrandsByCate($cate_id) {
		$category           = Mage::getModel('catalog/category')->load($cate_id);
		$layer              = Mage::getSingleton('catalog/layer');
		$layer->setCurrentCategory($category);
		$attributes = $layer->getFilterableAttributes();
		$manufacturers = array();
		foreach ($attributes as $attribute) {
			if ($attribute->getAttributeCode() == 'manufacturer') {
				$filterBlockName = 'catalog/layer_filter_attribute';
				$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
				foreach($result->getItems() as $option) {
					$manufacturers[$option->getValue()] = $option;
				}
			}
		}
		return $manufacturers;
		
	}
}

 In phtml File:

 

<?php 
$cate_id = Mage::app()->getRequest()->getParam('cate_id');
$first_url = Mage::app()->getRequest()->getParam('first');
$manus = $this->getAllBrands($cate_id,$first_url);
$sub_cate = Mage::getModel('catalog/category')->load($cate_id);
$first = '';
$first_arr = array();
?>
<div class="index-div">
<a href="<?php echo $this->getUrl('mypage/index/manufacturer',array('cate_id'=>$cate_id));?>"><?php echo $this->__('All')?></a> | 
<a href="<?php echo $this->getUrl('mypage/index/manufacturer',array('cate_id'=>$cate_id,'first'=>'1'));?>"><?php echo $this->__('0 - 9');?></a> | 
<?php foreach ($manus['title'] as $index):?>
	<?php if (($first == '' || $first!=$index['first']) && $index['first'] > '9'):?>
		<?php $first = $index['first'];?>
		<?php $first_arr[] = $index['first'];?>
	<?php endif;?>
<?php endforeach;?>
<?php foreach ($first_arr as $key=>$fa):?>
	<a href="<?php echo $this->getUrl('mypage/index/manufacturer',array('cate_id'=>$cate_id,'first'=>$fa));?>"><?php echo $fa;?></a> 
	<?php if ($key < count($first_arr) -1):?>
	|
	<?php endif;?>
<?php endforeach;?>
</div>
<?php $first = '';?>
<div class="manu-content">
<ul>
<li class="top-letter"><a name="0-9"></a>0-9</li>
<?php foreach ($manus['content'] as $manu):?>
<?php if (($first == '' || $first!=$manu['first']) && $manu['first'] > '9'):?>
	<?php $first = $manu['first'];?>
	<li class="top-letter"><a name="<?php echo $first?>"></a><?php echo $first?></li>
<?php endif;?>
<?php $cate_manu_url = $sub_cate->getUrl().'?manufacturer='.$manu['value'];?>
<li > <a title="<?php echo $manu['label'];?>" href="<?php echo $cate_manu_url;?>"><?php echo $manu['label'];?></a>
</li>
<?php endforeach;?>
</ul>
</div>

 

 

你可能感兴趣的:(Alpha)