Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access variable outside foreach loop PHP

I'm new to PHP. Can anyone tell me how to access the foreach loop variable outside foreach. Please find below by code.

  <?php  $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*'); 
         foreach ($categories as $category): 
         $categorySize = $category->getSize_chart(); 
         print_r ($categorySize); 
         endforeach;
    ?>

I need it in the following html img tag's src attribute.

<div class="SizeChat"><p>close</p><div class="Padd"><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$categorySize ?>"></div></div>
like image 605
Swetha Avatar asked Sep 26 '15 07:09

Swetha


1 Answers

First define the variable above of the loop

$categorySize = array();

<?php  $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*'); 
         foreach ($categories as $category): 
         $categorySize = $category->getSize_chart(); 
         print_r ($categorySize); 
         endforeach;
    ?>

print_r($categorySize) //Now you can get it outside the loop 
like image 121
Man Programmer Avatar answered Oct 15 '22 23:10

Man Programmer