PHP - array_chunk - 无法使其适应工作

时间:2012-12-17 14:55:11

标签: php

大家好,我在php世界中还是新手还在学习,我完全迷失了。我已经尝试了所有内容并阅读了很多博客,但没有什么可以帮助我弄清楚如何去做。 我有这个代码(下面)完美地工作..由于<ul>&amp; <li>

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

但是当我尝试将它与我的其他代码(下面)合并在一起时,它就停止工作了。它只显示<ul>,并且没有在<li>中放置的数据甚至不显示。

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

这是我尝试的变种之一(不会向我显示500错误的变种)

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

我知道我正在做的事情是完全错误的,但正如我所说,我还是新学习(我还有很长的路要走,我知道。)

任何帮助都不仅仅是欣赏。提前谢谢!

编辑:     数组([value] =&gt; 15 [标签] =&gt;乔丹)     数组([value] =&gt; 19 [标签] =&gt; Maison Scotch)     数组([value] =&gt; 5 [标签] =&gt;博物馆)     数组([value] =&gt; 17 [标签] =&gt;耐克)     数组([值] =&gt; 16 [标签] =&gt;耐克篮)     数组([value] =&gt; 11 [标签] =&gt;巡回演出)     数组([value] =&gt; 7 [标签] =&gt; PeB)     数组([value] =&gt; 14 [标签] =&gt; Penfiled)     数组([value] =&gt; 4 [标签] =&gt;总统)     数组([value] =&gt; 23 [标签] =&gt; Scotch&amp; Soda)     数组([value] =&gt; 12 [标签] =&gt; Solovair)     数组([value] =&gt; 13 [标签] =&gt;上)     数组([value] =&gt; 20 [标签] =&gt; Vans)     数组([value] =&gt; 18 [label] =&gt; Wood Wood)

1 个答案:

答案 0 :(得分:1)

混合你的变量。可能你想要这样的东西:

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

注意变量是如何在两个foreach循环中级联的。