Elio Barremax
Elio Barremax

Reputation: 13

PHP - array_chunk - can't adapt it to work properly

Hello everybody I'm new in the php world still learning, and I'm completely lost. I've tried everything and read a lot of blogs but nothing helps me to figure out how to do it. I have this code (below) which works perfectly.. it's dividing a long list in blocks of 5 elements, thanks to <ul> & <li>'s.

<?php
$values = range(1, 31);
$rows = array_chunk($values, 5);


foreach ($rows as $row) {
    print "<ul>";
    foreach ($row as $value) {
        print "<li>" . $value . "</li>";
    }
    print "</ul>";
}
?>

But When I try to merge it together with my other code (below), it stops to work. It only displays the <ul>'s and no data is putted in <li>'s which don't even display.

<?php 
        $product = Mage::getModel('catalog/product');
        $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
        ->setEntityTypeFilter($product->getResource()->getTypeId())
        ->addFieldToFilter('attribute_code', 'manufacturer');
        $attribute = $attributes->getFirstItem()->setEntity($product->getResource());
        $manufacturers = $attribute->getSource()->getAllOptions(false);
?>


<ul> 
<?php foreach ($manufacturers as $manufacturer): ?>
    <li>
        <a href="/manufacturer/<?php echo $manufacturer['label'] ?>">
            <?php echo $manufacturer['label'] ?>
        </a>
    </li>
<?php endforeach; ?>
</ul>

This is one of the variants I've tried (the one that doesn't show me a 500 error)

<?php
$rows = array_chunk($manufacturers, 5);


foreach ($manufacturers as $manufacturer) {
    echo "<ul>";
    foreach ($row as $manufacturer) {
        echo "<li>"  ?> 
                <a href="/manufacturer/<?php echo $manufacturer['label'] ?>"><?php echo $manufacturer['label'] ?></a>
<?php   echo "</li>"; ?>
<?php    }
    echo "</ul>";
}
?>

I know probably what I'm doing is completely wrong, but as I said I'm new still learning (I have a long way to go, I know.)

Any help will be more than appreciate. Thank you in advance!

EDITS: Array ( [value] => 15 [label] => Jordan ) Array ( [value] => 19 [label] => Maison Scotch ) Array ( [value] => 5 [label] => Museum ) Array ( [value] => 17 [label] => Nike ) Array ( [value] => 16 [label] => Nike Basket ) Array ( [value] => 11 [label] => On Tour ) Array ( [value] => 7 [label] => PeB ) Array ( [value] => 14 [label] => Penfiled ) Array ( [value] => 4 [label] => President's ) Array ( [value] => 23 [label] => Scotch & Soda ) Array ( [value] => 12 [label] => Solovair ) Array ( [value] => 13 [label] => Supra ) Array ( [value] => 20 [label] => Vans ) Array ( [value] => 18 [label] => Wood Wood )

Upvotes: 1

Views: 753

Answers (1)

arkascha
arkascha

Reputation: 42935

You mix your variables. Probably you want something like this:

<?php
$rows = array_chunk($manufacturers, 5);

foreach ($rows as $row) {
?>
    <ul>
    <?php foreach ($row as $manufacturer) { ?>
        <li>
            <a href="/manufacturer/<?php echo $manufacturer['label']; ?>">
                this is a link
            </a>
        </li>
    <?php } ?>
    </ul>
<?php } ?>

Note how the variables are cascaded in the two foreach loops.

Upvotes: 1

Related Questions