-2

I have the following HTML code:

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

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.

6
  • why didn't you post here a real code? May 22, 2010 at 16:03
  • Because I don't know how to write the real code (PHP). That's why I'm asking here.
    – Psyche
    May 22, 2010 at 16:06
  • Indeed - what is the repeating structure for? What is the difference between "News 1" the first time and the second time around? Could you print_r() the structure that you want to display and paste it in the question?
    – Amadan
    May 22, 2010 at 16:09
  • @Amadan: done. I have updated the post with the array I want to display.
    – Psyche
    May 22, 2010 at 16:16
  • Sorry, I still don't get it. The array only has one level; you have two levels in your desired HTML. What would be the output? I guess "News1" = "Poze...", "News2" = "Psyche...", "News3" = "Galerie...", if you're displaying it once. What do you want to display when it repeats?
    – Amadan
    May 22, 2010 at 16:22

4 Answers 4

1

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.

0

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

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-->
1
  • Heh, sorry, I kind of rushed with conclusions :) Yeah, that's the output I figured you wanted. Hope this helps.
    – Amadan
    May 22, 2010 at 16:49
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.