开发者

modify loop to display message 1 time for all instances

开发者 https://www.devze.com 2023-03-10 12:48 出处:网络
My code check the 1st 3 messages of the twitter wall and looks for a string called \"code\". if its there it will echo \"code available\" and if its not it will echo \"no code\". Right now it echos 3

My code check the 1st 3 messages of the twitter wall and looks for a string called "code". if its there it will echo "code available" and if its not it will echo "no code". Right now it echos 3 times for each message.

how would i modify this code to check all 3 messages still, but only echo the "no code" or "code available" message one time for all wall posts?

function echo_messages($url,$max = 1)
{
     $data = json_decod开发者_JAVA百科e(file_get_contents($url));
     $counter = 0;
     foreach($data->data as $post) 
     {
        preg_match("/code/", $post->message, $code);        
        echo '<div id="facebook">';
        if (strlen($code[0]) == 0){
            echo 'Facebook: No Code';
        }else{
            echo 'Facebook: Code Available';
        }
        echo '</div>';
       $counter++;
       if($counter >= $max)
       {
           return true;
       }
    } 
}
echo_messages('https://graph.facebook.com/234265435775/posts',3);


How about this:

function echo_messages($url,$max = 1) {
     $data = json_decode(file_get_contents($url));
     $counter = 0;
     foreach($data->data as $post) 
     {
        preg_match("/code/", $post->message, $code);        
        if (strlen($code[0])!= 0){
            echo '<div id="facebook">Facebook: Code Available</div>';
            return;
        }
        $counter++;
        if($counter >= $max) break;
    } 
    echo '<div id="facebook">Facebook: No Code</div>'; 
}

echo_messages('https://graph.facebook.com/234265435775/posts',3);

It'll run up to $max times. Once it finds code it'll stop processing and report that. If it doesn't find a code by $max times, or if there aren't enough records - it'll report no code is found. The only thing it doesn't do that you asked is run 3 times regardless of if it finds a code in the first message (but I can't imagine why you'd want it to do that unnecessary processing!).

By the way, it would be much better to return a true/false indicating whether a code was found, and then construct your message outside the function.

function hasCode($url,$max=1) {/*...*/}
echo "<div id=\"facebook\">Facebook: ".
    (hasCode('https://graph.facebook.com/234265435775/posts',3)?
      "Code Available":"No Code").
     "</div>";


function echo_messages($url,$max = 1)
{
    $data = json_decode (file_get_contents ($url));
    $result = 'No Code';
    foreach (new LimitIterator (new ArrayIterator ($data->data), 0, $max) as $post)
    {
        if (preg_match ('/code/i', $post->message))
        {
                $result = 'Code available';
                break;
        }
    }

    printf ('<div id="facebook">Facebook: %s</div>', $result);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号