0

HELLO I am trying to create a foreach loop which will cycle through a given list of magento products to get their attributes in order to populate a JW Player playlist. The structure of JW Player's syntax allows for the inclusion of multiple product attributes values to build a playlist, however I need to break into the foreach when it reaches the final item in order to close the playlist config.

When not being dynamically generated, the JW Player syntax looks so:

<script type="text/javascript">
jwplayer('media1').setup({
'flashplayer': 'mediaappearance/jwplayer.flash.swf',
'width': '700',
'height': '400',
playlist: [{
    file: 'video for magento product 1',
    image: 'image for magento product 1',
    title: 'name for magento product 1',
    description: 'description for magento product 1'                                                                    
},{
    file: 'video for magento product 2',
    image: 'image for magento product 2',
    title: 'name for magento product 2',
    description: 'description for magento product 2'                                

}]                                              
  });
</script>

With this products collection, I want to get attributes for each product to populate the playlist:

<div id="tab-one" class="tabset_content">
<?php
$productCollection = Mage::getModel('catalog/product')->getCollection();
$product_file =  $item->getMediaUrl();              
$product_name =  $item->getName();
$product_img = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(113, 113);
$product_description = Mage::helper('catalog/output')->productAttribute($product, $product->getShortDescription(), 'short_description');                
?>


<?php foreach ( $productCollection as $_product ) : ?> 

<div class="casestudies">
        <div class="jwbox" style="cursor: pointer;">                    
        <div class="jwbox_hidden">
                        <div class="jwbox_content">
                            <div id='media1'></div>
                            <script type="text/javascript">
                                jwplayer('media1').setup({
                                'flashplayer': 'mediaappearance/jwplayer.flash.swf',
                                'width': '700',
                                'height': '400',
                                <?php
                                $playlist = array();
                                foreach($productCollection as $_product) {
                                $playlist = array();
                                $playlist[] = array(
                                            'file' => $product_file ,
                                            'title' => $product_name,
                                            'image' => $product_img,
                                            'description' => $product_description
                                     );
                                 echo json_encode($playlist);
                                }
                                ?>                                          
                              });
                            </script>
                        </div>
                    </div>
                    </div>                                                                  
            </div>  
<?php endforeach; ?> 

But the foreach loop show only 1 product attributes. I want it show for all products in the array $productCollection . Help please.

1 Answer 1

0

Use

<div id="tab-one" class="tabset_content">
<?php
$productCollection = Mage::getModel('catalog/product')->getCollection();

?>


<?php foreach ( $productCollection as $_product ) : ?> 
<?php 
$product_file =  $_product->getMediaUrl();              
$product_name =  $_product->getName();
$product_img = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(113, 113);
$product_description = Mage::helper('catalog/output')->productAttribute(_product,
$product->getShortDescription(), 'short_description');      
?>
<div class="casestudies">
        <div class="jwbox" style="cursor: pointer;">                    
        <div class="jwbox_hidden">
                        <div class="jwbox_content">
                            <div id='media1'></div>
                            <script type="text/javascript">
                                jwplayer('media1').setup({
                                'flashplayer': 'mediaappearance/jwplayer.flash.swf',
                                'width': '700',
                                'height': '400',
                                <?php


                                $playlist = array(
                                            'file' => $product_file ,
                                            'title' => $product_name,
                                            'image' => $product_img,
                                            'description' => $product_description
                                     );
                                 echo json_encode($playlist);

                                ?>                                          
                              });
                            </script>
                        </div>
                    </div>
                    </div>                                                                  
            </div>  
<?php endforeach; ?> 
4
  • Hello Mbalparda, thanks for your help. When I use your solution, the loop dont return data so it show nothing in the player. I think there is a problem with the array variable. With my former trying the loop show only one product instead all in the collection. Aug 24, 2014 at 11:10
  • You lack of basic knoledge in PHP and in Magento, the error you were having was that the vars were created before the foreach and not inside it.
    – mbalparda
    Aug 24, 2014 at 14:32
  • Hi Mbalparda, I am not a PHP Programmer nor a Magento specialist, I am learning how to modify my site. Help please Aug 24, 2014 at 14:58
  • Hi Mbalparda, I try again, your solution works very well!!! Thank you for help Aug 24, 2014 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.