How do I limit the for loop count?

Joined
Mar 25, 2021
Messages
28
Reaction score
0
Hi, the following works fine but I’d like to limit the max number of items pulled at a given time? Wondering how to do this?

PHP:
function rssNews() {
    $items = xmlFeed('research');
    $items = orderByDate($items);
    $output = '';
    $output = '<ul id="rssnews">';

    if (count($items) >= 2) {
        for ($i = 0; $i < count($items); $i++) {
            if ($items[$i][description] != '') {
                $cleanDate = convertPubDate($items[$i][pubDate], 'F dS, Y');
                $output .= '<li>';
                if ($items[$i][thumb] != '') {
                    $output .= '<img src="'.$items[$i][thumb].'" width="85px" height="85px" alt="'.$items[$i][title].'">';
                } else {
                    $output .= '<img src="http://mydomain.com/images/placeholder.png" width="85px" height="85px" alt="'.$items[$i][title].'">';
                }
                $output .= '<h3><a href="'.$items[$i][link].'" target="_blank">'.$items[$i][title].'</a></h3>';
                $output .= '<p>'.htmlspecialchars_decode($items[$i][description]).' <em class="date">'.$cleanDate.'</em></p>';
                $output .= '</li>';
            }
        }
    } else {
        $output .= '<li><article><p>Please check back soon</a>.</p></article></li>';
    }
    $output .= '</ul>';
    return $output;
}
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
The number of items your pulling is determined by this loop
for ($i = 0; $i < count($items); $i++) {...}
Don't use "count($items)". When you call the function pass it the number of item you want.
function rssNews($I) {
$numberItems -=$I;

and the loop is
for ($i = 0; $i < $numberItems -; $i++) {...}
 
Joined
Mar 25, 2021
Messages
28
Reaction score
0
Like this?

PHP:
function rssNews() {
    $items = xmlFeed('research');
    $items = orderByDate($items);
  
    $maxPosts = 10;
  
    $output = '';
    $output = '<ul id="rssnews">';

    if (count($items) >= 2) {
        for ($i = 0; $i < $maxPosts; $i++) {
            if ($items[$i][description] != '') {
                $cleanDate = convertPubDate($items[$i][pubDate], 'F dS, Y');
                $output .= '<li>';
                if ($items[$i][thumb] != '') {
                    $output .= '<img src="'.$items[$i][thumb].'" width="85px" height="85px" alt="'.$items[$i][title].'">';
                } else {
                    $output .= '<img src="http://mydomain.com/images/placeholder.png" width="85px" height="85px" alt="'.$items[$i][title].'">';
                }
                $output .= '<h3><a href="'.$items[$i][link].'" target="_blank">'.$items[$i][title].'</a></h3>';
                $output .= '<p>'.htmlspecialchars_decode($items[$i][description]).' <em class="date">'.$cleanDate.'</em></p>';
                $output .= '</li>';
            }
        }
    } else {
        $output .= '<li><article><p>Please check back soon</a>.</p></article></li>';
    }
    $output .= '</ul>';
    return $output;
}

This seems to work. Do i need to add something under $maxPosts like

PHP:
$counter = 1;

and then after the closing </li> output something like

PHP:
if (($counter % 2) == 0) { $output .= '</ul><ul class="rssnews">'; }
                $counter++;

or does the else take care of whatever for that?
 
Last edited:
Joined
Nov 13, 2020
Messages
302
Reaction score
38
No need to do any of that. You will always get 10 posts this way. The way I gave would let you change the amount every time. You could set the incoming perameter of function rssNews() to 10. That would let you change it when you wanted. Go look it up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top