我是PHP的新手。任何人都可以告诉我如何在foreach之外访问foreach循环变量。请在下面按代码查找。
<?php $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');
foreach ($categories as $category):
$categorySize = $category->getSize_chart();
print_r ($categorySize);
endforeach;
?>
我在以下html img标签的src属性中需要它。
<div class="SizeChat"><p>close</p><div class="Padd"><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$categorySize ?>"></div></div>
答案 0 :(得分:3)
首先定义循环上面的变量
$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