Reputation: 21
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.
Upvotes: 2
Views: 102
Reputation: 13534
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;
}
Upvotes: 3
Reputation: 1819
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.
Upvotes: 0