1

I have trouble creating a foreach request data from an array.

Example to request Name, URL and Image from Magento:

<?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>

The issue is that i can figure out nothing, and no errors giving, i really appreciate if can someone tell me what is wrong ?

Solution provided by Vladimir 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>
1
  • 1
    something might be wrong at this line: $helper = Mage::helper('catalog/category'); you can var_dump($helper); to see if it's assigned as it should but you probably have an error there... Commented Oct 21, 2013 at 11:14

3 Answers 3

1

Your array and foreach is fine, problem in $helper object. try this code:

<?php
$cat_id = array(268, 269, 270, 271, 272, 273, 274, 275, 276);
?>
 <div class="main">
 <?php foreach ($cat_id as $id):?>
   <div class="category">
       <a href="<?php echo $id; ?>">
        <img src="<?php echo $id; ?>" title="<?php echo $id; ?>"/>
        <div class="text"><h2><?php $id; ?></h2></div>
       </a>
    </div>
 <?php endforeach; ?>
 </div>
Sign up to request clarification or add additional context in comments.

1 Comment

You are right, i figured it out, i removed the helper and loaded the info via model.
0
<?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; ?>

to

<?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 } ?>

Comments

0

There is nothing wrong with your foreach code.

What I think it is probably going on is that the id's that you are setting up doesn't exists on this object $helper

So, I recommend you to see if this object has something in it. Use

var_dump($helper);

to see what is going on.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.