Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

PHP help - display 1 product image!?!? 
 
toms77
Jr. Member
 
Total Posts:  16
Joined:  2010-05-04
 

Hi
How do I edited the following code, the make it display only 1 image at the time!?!

<?php $products $this->getProducts() ?>
<?php 
if (count($products) > 0): ?>
<?php 
foreach($products as $product): ?>
<div class="associated-left">
<
a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $product->getName() ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(180) ?>" width="180" height="180" alt="<?php echo $product->getName() ?>" /></a>
</
div>
<?php endforeach; ?>
<?php 
endif; ?>

My head is spinning, please help

Thx.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

What do you mean by ‘one image at the time’?
Your code should display one image for each product in the $products collection.
Can you five an example on what you are trying to achieve?

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
toms77
Jr. Member
 
Total Posts:  16
Joined:  2010-05-04
 

The code is in use with the fishpig/wordpress plugin. It let\’s me assosiatede products with WP-post.

My code works great if I only associate 1 product, If 2 products is associated both images will be displayed at the same time.

Is the anyway to change this show only 1 image (random) is displayed?!?

Is it possible to -> limetCount(1)->display(rand) !?!

Hope it makes more sense

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Try this code:

<?php
$prodArray 
= array();
foreach (
$this->getProducts() as $product{
    $prodArray[] 
$product;
}
shuffle
($prodArray);?>
<?php 
if (isset($prodArray[0])) : >
<?php $product $prodArray[0];?>
<div class="associated-left">
<
a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $product->getName() ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(180) ?>" width="180" height="180" alt="<?php echo $product->getName() ?>" /></a>
</
div>
<?php endif;?>

Let me know if it works.
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
toms77
Jr. Member
 
Total Posts:  16
Joined:  2010-05-04
 

Hi Marius and thx 4 the effort.

Unfortunately the code only gave an 500 internal sever error

Tom

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

oh...sorry about that.
I didn’t test the code because I don’t know where $this->getProducts() comes from. I assumed it’s an array or a collection of products.
I can figure out why do you get such an error. I still think it should work
Make sure there are no syntax errors.
Try removing the shuffle statement and see if it works.

 
Magento Community Magento Community
Magento Community
Magento Community
 
toms77
Jr. Member
 
Total Posts:  16
Joined:  2010-05-04
 

Hi....no worries wink

shuffle statement removed ->still dosn’t work.

All I wanna do is show 1 random image from one specific category.

I’m on the verge of giving up the fishpig wp blog.

Tom

 
Magento Community Magento Community
Magento Community
Magento Community
 
toms77
Jr. Member
 
Total Posts:  16
Joined:  2010-05-04
 

This does the trick. Displays 1 random image from one specific category! 

<?php
$_productCollection
=$this->getLoadedProductCollection();
$categoryID 7;
//$categoryID = $this->getCategoryId();
if($categoryID){
  $category 
= new Mage_Catalog_Model_Category();
  
$category->load($categoryID); // this is category id
  
$collection $category->getProductCollection();
} else{
  $collection 
Mage::getResourceModel('catalog/product_collection');
}
Mage
::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts $this->getNumProducts() ? $this->getNumProducts() : 1;
$collection->setPage(1$numProducts)->load();
$_productCollection $this->_productCollection $collection;
?>
<?php 
if(!$_productCollection->count()):?>
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection. Please provide a category ID.')?>
</div>
<?php else:?>
<ul>
<?php $_collectionSize $_productCollection->count()?>
<?php 
foreach ($_productCollection as $_product):?>
<li>
<
div class="associated-left">
<
a href="<?php echo $_product->getProductUrl()?>" title="<?php echo $this->htmlEscape($_product->getName())?>">
<
img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(180, 180); ?>" width="180" height="180" alt="<?php echo $this->htmlEscape($_product->getName())?>"</img>
</
div>
</
a>
</
li>
<?php endforeach?>
</ul>
<?php endif;?>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top