• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1332
  • Last Modified:

Show multiple products from category using foreach loop?

Hi guys,

I'm really trying to finish this bit of coding for a Magento website but have been stuck on more than a few things.

I'm trying to more than one product from the same category in the right-sidebar but at the moment it will only show one!

I'm not sure how to modify the code to execute a foreach and would appreciate some guidance ...?

Here's the code:
<?php 
$category_id = "25"; // category_id for "Featured Products"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
  ->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
  ->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
 
<?php if($_productCollection->count()): ?>
 
  <?php 
  $products = array();
  foreach ($_productCollection as $_product) { array_push($products, $_product); }
  $_product = $products[rand(0,count($products)-1)];
  ?>
<div class="block block-featured-product">
  <div class="block-title">
    <h2>Featured Product</h2>
  </div>
  <div class="block-content">

    <ul id="featured-product-list">
      <li class="featured-product">
        
          <h4><?php echo $this->htmlEscape($_product->getName())?></h4>
          <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
            <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(117, 117); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
          </a>
          <a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">View Item</a>

      </li>
    </ul>
  </div>
</div>
<?php endif; ?>

Open in new window


It works but only shows 1 product, would really like it to show upto 4 or 5 please. HELP!
0
rafique12
Asked:
rafique12
1 Solution
 
teebonCommented:
<?php 
$category_id = "25"; // category_id for "Featured Products"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
  ->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
  ->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
 
<?php if($_productCollection->count()): ?>
 
 
<div class="block block-featured-product">
  <div class="block-title">
    <h2>Featured Product</h2>
  </div>
  <div class="block-content">

    <ul id="featured-product-list">

 <?php 
  $index = 0;
  $products = array();
  foreach ($_productCollection as $_product) { 
  ?>
      <li class="featured-product">
        
          <h4><?php echo $this->htmlEscape($_product->getName())?></h4>
          <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
            <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(117, 117); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
          </a>
          <a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">View Item</a>

      </li>
<?php
    $index++;
    if($index>5)
      break;
 } ?>
    </ul>
  </div>
</div>
<?php endif; ?>

Open in new window

0
 
rafique12Author Commented:
Thanks dude!

Just saved my life!!!

Do you think you could help with the other two questions that I have open? That would really be helping me...
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now