2

Pretty newbie question :/

I have an API that returns values, I just want to get the number of elements in XML but its limited to 30 per query.

function SOMEfunction($number){
   $curl = curl_init("*URL*?format=xml&page=" . $number);
   $result = curl_exec($curl);
   $xml = simplexml_load_string($result);
   $ttn = $xml->count();
   echo "$ttn<br>";
}

so, Since I just want to get the number of elements in XML, i run a short while loop, which i want to sum somehow.

$sum=0;
$num=1;
while ($num < 7)
{
   $sum += SOMEfunction($num);
   $num++;
}
echo $sum;

the current out put is:

30
30
30
30
2
0
0

How can i sum them up?

Thanks.

4
  • Your function does not return value. echo "$ttn<br>"; should be return $ttn; Try making that change and combine it with the answer I posted and see if that works. Commented May 6, 2015 at 21:42
  • You are not making use of the argument that you are passing to your function. Even the syntax for passing the value seems to be incorrect. VALUE will be possibly treated as a constant by PHP. Commented May 6, 2015 at 21:43
  • Where is the value of $curl coming from? What does it look like. Maybe you should pass $curl as an argument to your function in place of VALUE. I am not sure if that will work even. Commented May 6, 2015 at 21:45
  • Your current code for the function will just echo the value but never assign them to $sum variable. Ideally you should not even be getting the output that you are currently getting. It should just be a single value. Commented May 6, 2015 at 21:59

2 Answers 2

3

SOMEfunction should return a value, not print it, as follows:

function SOMEfunction($number){
   $curl = curl_init("*URL*?format=xml&page=" . $number);
   $result = curl_exec($curl);
   $xml = simplexml_load_string($result);
   $ttn = $xml->count();
   return $ttn;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$sum=0;
$num = 1; 
do { 
 $sum += SOMEfunction($num);
 $num++;
} while ( $num > 0 && $num < 7 ); 

echo $sum;

This assumes that the rest of your code including SOMEfunction is working fine. You can also possibly re-factor the while loop but I am not entirely sure about that.

Another way to do this:

$sum=0;
$num=1;
while ($num < 7)
{
   $sum += SOMEfunction($num);
   $num++;
}
echo $sum;

Try this and see if it works. If not then I am not sure if your SOMEfunction is working correctly.

12 Comments

Yes, this do while loop looks like a for($num=1;$num<6;$num++). But I think it's a while loop, and the test should be wether SOMEfunction returned 0 or not.
But how will the sum be obtained in that case. You wanna add another answer with your approach ?
Hi, thanks for the reply. SOMEfunction returns each count of XML elements...not 0 always. when i added the code maximus suggested and removed my echo, i now get the value 0. Is it possible my initial values were not numbers?
Try turning your do/while loop into a for loop as suggested by @Niols and see what you get ?
Take a look at the updated answer and see if that works.
|

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.