2012-09-18 34 views
0

我製作了一個小型的magento(1.7)模板文件,用於顯示類別中的所有產品。但是,它們只能顯示在單個列中。我想顯示在2列。用於2列的塊的magento模板

此執行從頭版塊:

{{block type="catalog/product" name="msc.specials" template="mylib/featuredlist.phtml"}} 

這是featuredlist.phtml -

<?php 
//$_categoryId = $this->getCategoryId(); 

$productCollection = Mage::getModel('catalog/category')->load(4) 
    ->getProductCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter('status', 1) 
    ->addAttributeToFilter('visibility', 4) 
    ->setOrder('price', 'ASC'); 
    $cartHelper = Mage::helper('checkout/cart'); 
?> 

<div class='block block-list'> 
<div class='block-title'><strong><span><?php echo $this->__('SPECIALS') ?></span></strong></div> 
    <div class='block-content'> 
     <ul> 
      <h2><?php echo $this->__($this->getLabel()); ?></h2> 
      <?php foreach ($productCollection as $product): ?> 
       <div class="item"> 
        <a class="product-image" href="<?php echo $product->getProductUrl() ?>">     
         <img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" /> 
        </a> 
        <a class="product-name" href="<?php echo $product->getProductUrl() ?>"><?php echo $this->htmlEscape($product->getName()) ?></a>      
        <?php echo $this->getPriceHtml($product, true) ?>      
       </div> 

       <div class="cms-price-box" style=" text-align:center;"></div> 
       <div class="button-row" style="text-align:center;">     
        <button class="button btn-cart" type="button" onclick="setLocation('<?php echo $this->getUrl('')."checkout/cart/add?product=".$product->getId()."&qty=1" ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button> 
       </div> 
       <br/><br/>    
      <?php endforeach ?> 
     </ul> 
    </div> 
</div> 

回答

0

這實際上是將是一個有點複雜。也許我已經達到了目標,但你需要做一些事情。它使它成爲一個單獨的項目列,因爲它正在做一個foreach,只是把每個項目吐出來。爲了使它做兩列,你需要計算列表中的項目併除以二。從那裏你可以使用一些迭代將它分成兩個數組,然後對兩個結果數組進行foreach。

對於每個數組,你會希望它在一個div的css風格的左或右列。此外,您應該決定是否需要在中間拆分項目,第一部分進入第1列,後一部分進入第2列。或者您可能更喜歡在第1列中出現奇數項目,甚至第2列中出現的項目都會出現。

如果您正在尋找更詳細的答案,我可以給你一個,但它不是一個簡單的答案。如果您對此感到滿意,肯定會稍微重新編碼。

這裏是你可以做什麼概述:How to display two table columns per row in php loop

+0

我有一些困難夾緊集合,所以我用低科技(($ x%2)== 0)。看到我的答案在下面... –

0

這是基於內特的建議和參考的解決方案。會對其他方式感興趣

<?php 
$_categoryId = $this->getCategoryId(); 

$productCollection = Mage::getModel('catalog/category')->load($_categoryId) 
    ->getProductCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter('status', 1) 
    ->addAttributeToFilter('visibility', 4) 
    ->setOrder('price', 'ASC'); 
    $cartHelper = Mage::helper('checkout/cart'); 

    $x = 1; 
?> 

<div class='block block-list'> 
<div class='block-title'><strong><span><?php echo $this->__('FEATURED PRODUCTS') ?></span></strong></div> 
    <div class='block-content'> 
     <ul> 
      <h2><?php echo $this->__($this->getLabel()); ?></h2> 
      <table> 
      <tr>    
      <?php foreach ($productCollection as $product): ?> 
      <?php   
       $x++; 
       if (($x % 2) == 0){ 
        echo "</tr><tr>"; 
       } 
      ?>   
      <td>     
       <div class="item"> 
        <a class="product-image" href="<?php echo $product->getProductUrl() ?>"> 
         <img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(180); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" /> 
        </a> 
        <br/> 
        <a class="product-name" href="<?php echo $product->getProductUrl() ?>"><?php echo $this->htmlEscape($product->getName()) ?></a>      
        <?php echo $this->getPriceHtml($product, true) ?>      
       </div> 

       <div class="cms-price-box" style=" text-align:center;"></div> 
       <div class="button-row" style="text-align:center;">     
        <button class="button btn-cart" type="button" onclick="setLocation('<?php echo $this->getUrl('')."checkout/cart/add?product=".$product->getId()."&qty=1" ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button> 
       </div> 
      </td> 
      <?php endforeach ?> 
      </tr> 
      </table> 
     </ul> 
    </div> 
</div>