0

I compare a string from a feed with another variable and echo a corresponding string.

$xml = @simplexml_load_file($feed);

foreach ($xml->entry as $entry) {      
    $caller = $entry->caller[0];
    $message = $entry->message[0];
} 


if (($caller == $id) {
  echo '$message';
}

I want to echo no more than 5 messages, regardless of the number of ($caller == $id) matches.

 $x=1; 

 while (($caller == $id) && ($x<=5)) {
         echo '$x $message';
         $x++;
 }

That general approach has failed.

I thought maybe I could put the condition in a function and call it a certain number of times but no luck.

function myFunction(){
    echo '$message';
}

$x=1; 

while($x<=5) {
    echo '$x';
    myFunction();
    $x++;   
} 
4
  • how has that general approach failed? Commented Nov 23, 2013 at 2:59
  • You want to display 5 times the same message ? By message I mean an XMl entry. Or you want to only display 5 first XML entries ? Commented Nov 23, 2013 at 3:00
  • Lets see your complete original code including the loop. Your approach should work, other than the facts that variable scoping and string interpolation are wrong Commented Nov 23, 2013 at 3:00
  • While $caller == $id will always return true.. mot understanding what you arr tryin to achieve here. Commented Nov 23, 2013 at 3:01

2 Answers 2

2

For one, your while loop would actually only output 4 results because you are saying while x is LESS than 5, not <= 5. You can leave it < 5, but change x to equal 0 instead of 1;

The second problem is that as soon as $caller does not == $id, your while loop will stop. You should only need to use a foreach loop for this, not both a foreach to extract the data and a while to loop over it again.

The third problem with your code is that you are writing your caller and message values to the same variable over and over in your foreach. Then, in your while loop, your $caller and $message variables will always be equal to the last items in the $xml->entry array.

$xml = @simplexml_load_file($feed);
$number_of_results_to_show = 5;
$x = 0; // counter

foreach ($xml->entry as $entry) {      
    $caller = $entry->caller[0];
    $message = $entry->message[0];

    if ($caller == $id && $x < $number_of_results_to_show) {
        $x++;
        echo $message;
    }

    // also, you can use a break to prevent your loop from continuing
    // even though you've already output 5 results
    if ($x == $number_of_results_to_show) {
        break;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Which loop are you referring to?
$x will be always 0 because it is assigned inside foreach loop. Probably should move $x = 0; outside foreach.
@NikolaiSamteladze Oops! Thanks for catching that, editing. It's getting late, lol.
2

I assume that you have an array $xml->entry and you want to print message[0] of up to 5 array elements. Message is printed if $caller matches $id.

$xml = @simplexml_load_file($feed); 

// Iterate through $xml->entry until the end or until 5 printed messages 
for($i = 0, $j = 0; ($i < count($xml->entry)) && ($j < 5); ++$i) {      
    $caller = $xml->entry[$i]->caller[0];
    $message = $xml->entry[$i]->message[0];

    if ($caller == $id) {
        echo "$message";
        ++$j;
    }
} 

If you want to store results from $xml->entry then:

$xml = @simplexml_load_file($feed); 
$storedResults = new array();
foreach($xml->entry as $entry) {      
    $caller = entry->caller[0];
    $message = entry->message[0];

    // Store data in array. $storedResults will be an array of arrays
    array_push(storedResults, array( 'caller' => $caller, 'message' => $message ));   
} 

// Print up to 5 messages from the stored results
$i = 0, $j = 0;
while (($i < count($storedResults)) && ($j < 5)) {
    if ($storedResults[$i]['caller'] == $id) {
        echo $storedResults[$i]['message'];
        ++$j;
    }
    ++$i;
}

11 Comments

But I can't limit the xml, I need all the results of the feed, and then only echo the ones that match $id (a url param), and then only five of those.
I have not seen that you store results from $xml->entry. How do you want to store them in an array?
@user1877124 Updated my answer. Now covers the case when you store all entries from $xml->entry and then output up to 5 of them.
Your $storedResults variable should really be called $formattedResults, as that's all you're doing here. The only different between $xml->entry and $storedResults is that you've formatted your array slightly differently. @user1877124 You are NOT limiting the xml, it's ALL still in $xml->entry, looping and reading from it do not modify it.
@AlexKinnee Yeah, $storedResults just represents some kind of storage. In this particular case it is not very useful as the same data can be obtained from $xml->entry.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.