Separate content from an RSS into two Ivs

Asked

Viewed 76 times

1

I have a PHP code that takes the content from a feed, I would like to separate that content into two Divs. For example: in the div1 will be the first five posts on a Carousel (jcarousel) and on div2 will be another five posts on a listing.

<?php

            // loalidade BR e idioma pt_BR
            setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese") or die('Locale not installed');
            date_default_timezone_set('America/Sao_Paulo');

            $rss = new DOMDocument();
            $rss->load('https://jovemnerd.com.br/nerdnews/feed/');
            $feed = array();
            foreach ($rss->getElementsByTagName('item') as $node) {
                $item = array ( 
                    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                    );
                array_push($feed, $item);
            }
            <div class='box'>
            $limit = 5;

            for($x=0;$x<$limit;$x++) {
                $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
                $link = $feed[$x]['link'];
                $description = $feed[$x]['desc'];
                // aqui foi alterado para a função com a formatação correta
                $date = strftime("%A, %d de %B de %Y", strtotime($feed[$x]['date']));                  
                echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
                echo '<small><em>'.$date.'</em></small></p>';
                echo '<p>'.$description.'</p>';
            }?>

This is the code that I take the contents of the RSS, but I have no idea how to separate it.

1 answer

2


       $rss = new DOMDocument();
       $rss->load('https://jovemnerd.com.br/nerdnews/feed/');
       $feed = array();
       foreach ($rss->getElementsByTagName('item') as $node) {
           $item = array ( 
               'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
               'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
               'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
               'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
               );
           array_push($feed, $item);
       }

       echo '<div class="div-1">';
           for($x=0; $x<5; $x++) {
               $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
               $link = $feed[$x]['link'];
               $description = $feed[$x]['desc'];
               $date = strftime("%A, %d de %B de %Y", strtotime($feed[$x]['date']));

               echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
               echo '<small><em>'.$date.'</em></small></p>';
               echo '<p>'.$description.'</p>';
           }
       echo '</div>';

       echo '<div class="div-2">';
           for($x=5; $x<10; $x++) {
               $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
               $link = $feed[$x]['link'];
               $description = $feed[$x]['desc'];
               $date = strftime("%A, %d de %B de %Y", strtotime($feed[$x]['date']));
               echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
               echo '<small><em>'.$date.'</em></small></p>';
               echo '<p>'.$description.'</p>';
           }
         echo'</div>'; ?>
  • Thank you very much, man. But it gave this error here Parse error: syntax error, Unexpected '<' in /home/pennydreadful/public_html/test/index.php on line 33

  • 1

    Exchange the <div class='div-1'> for echo "<div class='div-1'>", has php and html code snippets together, so the error. Another option is to close the php tag before the <div class='div-1'> and Bir again before the for

  • But then I wouldn’t have to change the structure of ""? I ask this because I don’t know PHP.

  • Fix the above code as stated by fernadoandrade, html was mixed with PHP code.

  • thank you @Edvaldofarias -now shows this error: Parse error: syntax error, Unexpected 'for' (T_FOR), expecting ',' or ';' in /home/pennydreadful/public_html/test/index.php on line 34

  • What happens is that it doesn’t show 5 results per div, it shows many, look http://www.pennydreadful.com.br/test/

  • 1

    @Felipestoker just limit the amount of the second div exchanging $x<count($feed); for $x<=5; for example.

  • The second DIV will also have the 5 item. I had understood that the rest of the feed was in the second div. Sorry for the bug.

  • Note 10, thank you very much.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.