Any chance the wonderful people here on daniweb could help me again with a loop? My apologies, loops are not my thing :| Basically what I'm trying to do is add a condition where on the last tweet (that is where the loop = $nooftweets), that tweet gets a different <span> class. Here's the function --

function get_tweets($twitter_id,
                    $nooftweets=3,
                    $dateFormat="m/d/y",
                    $includeReplies=false,
                    $dateTimeZone="America/New_York",
                    $beforeTweetsHtml="<ul>",
                    $tweetStartHtml="<li><p>",
                    $tweetMiddleHtml="&nbsp;&nbsp;<small>",
                    $tweetEndHtml="</small></p></li>",
                    $afterTweetsHtml="</ul>") {
 
    date_default_timezone_set($dateTimeZone);
    if ( $twitter_xml = twitter_status($twitter_id) ) {
        $result = $beforeTweetsHtml;
        foreach ($twitter_xml->status as $key => $status) {
            if ($includeReplies == true |
                    substr_count($status->text,"@") == 0 |
                    strpos($status->text,"@") != 0) {
                $message = processLinks($status->text);
                $result.=$tweetStartHtml.$message.$tweetMiddleHtml.
                            date($dateFormat,strtotime($status->created_at)).$tweetEndHtml;
                ++$i;
                if ($i == $nooftweets) break;
                }
            }
            $result.=$afterTweetsHtml;
    }
    else {
        $result.= $beforeTweetsHtml.
                    "<li id='tweet'>Twitter seems to be unavailable at the moment</li>".
                    $afterTweetsHtml;
    }
    echo $result;
}

Dani AI

Generated

Two simple, durable ways to give the final tweet a different span class (beyond the quick counter trick) are: 1) let CSS do it with the :last-child selector, or 2) mark the last item on the server when you build the list. already got it working; below are two clean approaches that are safe to use years later and that avoid fragile index math inside a long loop.

Use CSS if your markup is stable. It is the easiest and keeps PHP simple:

ul.tweets li:last-child span {
  color: #c00;
  font-weight: bold;
}

This requires no extra PHP work. It works in modern browsers; if the site must support very old browsers (legacy IE), prefer the server-side option.

Server-side: collect the tweets you will display into a small array, slice to the requested length, then render while comparing the loop index to the last index. This keeps filtering and pagination logic separate from output and makes it easy to add a special class for the final item:

$tweets = array_values(iterator_to_array($xml->status));
$visible = [];
foreach ($tweets as $t) {
    if ($allowReplies || strpos((string)$t->text, '@') !== 0) {
        $visible[] = $t;
    }
}
$visible = array_slice($visible, 0, $maxTweets);
$lastIndex = count($visible) - 1;

foreach ($visible as $i => $t) {
    $cls = ($i === $lastIndex) ? 'tweet-last' : 'tweet';
    $text = htmlspecialchars((string)$t->text, ENT_QUOTES, 'UTF-8');
    echo '<li><span class="'.$cls.'">'.$text.'</span></li>';
}

Notes and cautions: always escape or sanitize tweet text (avoid injecting raw HTML). Modern Twitter endpoints require authenticated, JSON-based requests — the old anonymous XML feeds no longer work; see the Twitter developer docs (https://developer.twitter.com/en/docs). Cache API responses to respect rate limits and add a graceful empty-feed message when no tweets are returned.

Actually got it figured out, maybe I'm not as bad as loops as I thought... :scared:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.