By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP How do I limit the for loop count?


Active Coder
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?

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;
I think this?

// read feed from xml
function xmlFeed($section) {
    $rss = new rss_php;
    $feedURL = site_url().'/'.$section.'.xml';
    return $rss->getItems();

Can I just add something to the first snippet like for ($i = 0; $i < count($items) < 12; $i++) { or something of that nature? I am unclear how to limit this.

New Threads

Latest posts

Buy us a coffee!

Top Bottom