开发者

Troubles displaying an associative array with PHP and HTML

开发者 https://www.devze.com 2022-12-31 21:34 出处:网络
I have the following HTML code: <div id=\"newsTicker\"> <span class=\"icon news\"></span>

I have the following HTML code:

<div id="newsTicker">
    <span class="icon news"></span>
        <ul>
            [repeating structure]
            <li>
                <ul>
                    <li><a href="#"&g开发者_运维知识库t;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-->

I also have a PHP array:

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
        )

)

I would like to know how can I repeat that code inside [repeating structure] and display 3 different news for each repeating structure.

The output must be like this:

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

Thank you.


Split the data into an array of arrays of 3 elements :

$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);
}

Then, to display it :

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

This way, when you change your mind and want 4 news per group, you juste change the constant value.


Your questions doesn't really make sense; but to get a list of the news items in your array you'd want to do something like this

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


You did not answer my question about the lack of the second dimension in your data - I figured you might be wishing to slice the flat list into chunks of three (a hint from "news ticker" comment at the end was mildly helpful). If that's so, this should work as long as $items is a real array (i.e. has consecutive indices starting from 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-->


Assuming that you’re having an array with numeric keys, try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号