我無法從數組創建foreach請求數據。無法循環在PHP中使用Foreach的數組
實例從Magento的請求名稱,URL和圖像:
<?php
$cat_id = array(268, 269, 270, 271, 272, 273, 274, 275, 276);
$helper = Mage::helper('catalog/category');
?>
<div class="main">
<?php foreach ($cat_id as $id):?>
<div class="category">
<a href="<?php echo $helper->getCategoryUrl($id); ?>">
<img src="<?php echo $helper->getThumbnailUrl($id); ?>" title="<?php echo $helper->getName($id); ?>"/>
<div class="text"><h2><?php echo $helper->getName($id); ?></h2></div>
</a>
</div>
<?php endforeach; ?>
</div>
的問題是,我可以找出什麼,並沒有錯誤付出,我很感激,如果有人可以告訴我什麼是錯了嗎?
解決方案弗拉基米爾Boliev提供:
<?php
$cat_id = array(268, 269, 270, 271, 272, 273, 274, 275, 276);
?>
<div class="main">
<?php foreach ($cat_id as $id):?>
<?php $cur_category = Mage::getModel('catalog/category')->load($id);
$url = $cur_category->getUrl();
$name = $cur_category->getName();
$img = $cur_category->getThumbnailUrl();
?>
<div class="category">
<a href="<?php echo $url; ?>">
<img src="<?php echo $img; ?>" title="<?php echo $name; ?>"/>
<div class="text"><h2><?php $name; ?></h2></div>
</a>
</div>
<?php endforeach; ?>
</div>
在這一行可能會出現錯誤:$ helper = Mage :: helper('catalog/category');你可以var_dump($ helper);看看它是否應該分配,但你可能有一個錯誤... –