使用PHP和HTML显示关联数组的麻烦

时间:2010-05-22 15:51:56

标签: php

我有以下HTML代码:

<div id="newsTicker">
    <span class="icon news"></span>
        <ul>
            [repeating structure]
            <li>
                <ul>
                    <li><a href="#">News 1</a></li>
                    <li><a href="#">News 2</a></li>
                    <li><a href="#">News 3</a></li>
                </ul>
            </li>               
            [/repeating structure]          
        </ul>
</div><!--/#newsTicker-->

我还有一个PHP数组:

Array
(
    [0] => Array
        (
            [id] => 5
            [title] => Concert Aria Urbana
            [link] => http://www.searadeseara.ro/detalii-eveniment/aria-urbana-9.html
            [date] => 2010-05-22 14:59:20
        )

    [1] => Array
        (
            [id] => 4
            [title] => Poze Tequila Party
            [link] => http://www.searadeseara.ro/detalii-galerie/tequila-party-3/
            [date] => 2010-05-22 14:58:56
        )

    [2] => Array
        (
            [id] => 3
            [title] => Psyche este utilizatorul saptamanii
            [link] => http://www.searadeseara.ro/profil/1-Psyche.html
            [date] => 2010-05-22 14:48:03
        )

    [3] => Array
        (
            [id] => 2
            [title] => Galerie foto Summer Party
            [link] => http://www.searadeseara.ro/detalii-galerie/summer-party-8/
            [date] => 2010-05-22 14:25:13
        )

    [4] => Array
        (
            [id] => 1
            [title] => Concert Massive Attack
            [link] => http://www.searadeseara.ro/detalii-eveniment/massive-attack-revin-la-bucuresti-13.html
            [date] => 2010-05-22 14:24:37
        )

)

我想知道如何在[重复结构]中重复该代码,并为每个重复结构显示3个不同的新闻。

输出必须如下:

<div id="newsTicker">
    <span class="icon news"></span>
        <ul>            
            <li>
                <ul>
                    <li><a href="#">Concert Aria Urbana</a></li>
                    <li><a href="#">Poze Tequila Party</a></li>
                    <li><a href="#">Psyche este utilizatorul saptamanii</a></li>
                </ul>
            </li>               
            <li>
                <ul>
                    <li><a href="#">Galerie foto Summer Party</a></li>
                    <li><a href="#">Concert Massive Attack</a></li>                    
                </ul>
            </li>

        </ul>
</div><!--/#newsTicker-->

谢谢。

4 个答案:

答案 0 :(得分:1)

将数据拆分为由3个元素组成的数组:

$news = // Your big array
define('NUM_NEWS_PER_GROUP', 3);

$newsGroups = array();
for ($i = 0; $i < count($news); $i += NUM_NEWS_PER_GROUP) {
     $newsGroups[] = array_slice($news, $i, NUM_NEWS_PER_GROUP);
}

然后,显示它:

<ul>            
<?php foreach ($newsGroups as $newsGroup) : ?>
    <li>
        <ul>
        <?php foreach ($newsGroup as $item) : ?>
            <li><a href="#"><?php echo $item['title'] ?></a></li>
        <?php endforeach; ?>
        </ul>
     </li>
 <?php endforeach; ?>           
 </ul>

这样,当你改变主意并想要每组4条新闻时,你就会改变常数值。

答案 1 :(得分:0)

你的问题确实没有意义;但要获得阵列中的新闻项列表,您需要做类似的事情

<div id="newsTicker">
    <span class="icon news"></span>
        <ul>
            <li>
                <ul>
        <?php
        foreach ($newsArray as $newsItem ){
            echo '<li><a href="'.$newsItem['link'].'">'.$newsItem['title'].'</a></li>';
        }
        ?> </ul>
        </li>
    </ul>
</div>

答案 2 :(得分:0)

你没有回答我关于你的数据缺乏第二维度的问题 - 我想你可能希望将平面列表分成三个部分(最后从“新闻自动收报机”的评论中提示有点温和的帮助)。如果是这样的话,只要$ items是一个真正的数组(即连续索引从0开始),这应该有效:

<div id="newsTicker">
    <span class="icon news"></span>
        <ul>
<?php
  $outer = 0;
  $count = count($items);
  while ($outer < count($items)) {
?>
            <li>
                <ul>
<?php
    for ($inner = 0; $inner < 3 && $outer < $count; $inner++, $outer++) {
        echo '<li><a href="' 
            . $items[$outer]['link'] . '">'
            . $items[$outer]['title'] . "</a></li>\n";
    }
?>
                </ul>
            </li>
<?php
  }
?>
        </ul>
</div><!--/#newsTicker-->

答案 3 :(得分:0)

假设你有一个带数字键的数组,试试这个:

<ul>
<?php
foreach ($arr as $key => $item) {
    // start new list if $key is divisible by 3
    if ($key % 3 == 0) {
        echo '<li><ul>';
    }

    echo '<li><a href="…">…</a></li>';

    // end list if the next $key will start a new list
    if (($key + 1) % 3 == 0) {
        echo '</ul></li>';
    }
}

// end list if the last iteration did not do it
if (($key + 1) % 3 != 0) {
        echo '</ul></li>';
}
?>
</ul>