0

I am trying to add a Twitter count number and a Feedburner count number but when I combine the two with basic addition, it fails! If I get the valkues and print them seperatley it works fine, just will not let me add the two...

$twitCnt = twitter_subscribers(TWITTER_USERNAME); $feedCnt = feed_subscribers(FEEDBURNER_USERNAME); $totalCnt = $twitCnt + $feedCnt; echo $totalCnt; 

Assume that $twitCnt is = 2000 and $feedCnt is = 1000

Now when I try to add the 2 instead of getting 3000 I will get the $feedCnt value + 1 = 1001 instead of 3000

I am currently stumped, If I print the $twitCnt and the $feedCnt they show the correct amounts, however when I add the 2 in my code, the $twitCnt is shown as = 1 instead of it's actual value.

Any ideas what would cause this?


Update after running var_dump($twitCnt, $feedCnt)

string(5) "3,000" object(SimpleXMLElement)#238 (1) { [0]=> string(5) "1,000" } 

Also the Functions for getting the stats...

function twitter_subscribers($username = 'yourname'){ $count = get_transient('twitter_count'); if ($count != false){ return $count; }else{ $count = 0; $url = 'http://api.twitter.com/1/users/show.xml?screen_name='. $username; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); $xml = new SimpleXMLElement($data); $count = $xml->followers_count; $count = (float) $count; $count = number_format($count); set_transient('twitter_count', $count, 21600); // 6 hour cache return $count; } } function feed_subscribers($username = 'yourname') { $feed_url = 'http://feeds.feedburner.com/' . $username; $count = get_transient('rss_count'); if ($count != false) return $count; $count = 0; $data = wp_remote_get('http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $feed_url . ''); if (is_wp_error($data)) { return 'error'; } else { $body = wp_remote_retrieve_body($data); echo $body; $xml = new SimpleXMLElement($body); $status = $xml->attributes(); if ($status == 'ok') { $count = $xml->feed->entry->attributes()->circulation; } else { $count = 300; // fallback number } } set_transient('rss_count', $count, 21600); // 6 hour cache//60*60*24 return $count; } 
5
  • 1
    Show us the result of var_dump($twitCnt); var_dump($feedCnt);. Commented Feb 8, 2012 at 2:25
  • 1
    Please var_dump($twitCnt, $feedCnt) to check what they are first. Commented Feb 8, 2012 at 2:27
  • I have added the result of the var_dump above Commented Feb 8, 2012 at 2:33
  • var_dump() might just be PHP's greatest debug tool ;) Commented Feb 8, 2012 at 2:35
  • Don't use number_format() before you do any manipulation of numbers. Only use it when displaying them. Not sure where the comma is coming from in your feedburner circulation attribute. This number is not meant to be formatted according to the docs. Commented Feb 8, 2012 at 2:38

3 Answers 3

4

Well, first $twitCnt is a formatted string (I don't know why you format the value directly into the variable, since you need to compute that value prior to display it, you should only format it when displaying it). And $feedCnt is an object of type SimpleXMLElement.

To fix this, do not format $twitCnt but only when you display the value (ie. echo it) to the user. And fix this line $count = $xml->feed->entry->attributes()->circulation; so it returns a numeric value instead of an object.

** Update **

I took a small piece of code found here and giving it to you as a solution to convert your formatted numbers to actual numeric values.

For instance, it will convert 1,200.123 into 1200.123

function formattedStringToNumeric($str) { $a = array_pad(explode(".",str_replace(',','',$str)), 2, 0); // Split the string, using the decimal point as separator $int = $a[0]; // The section before the decimal point $dec = $a[1]; // The section after the decimal point $lengthofnum=strlen($dec); // Get the num of characters after the decimal point $divider="1"; // This sets the divider at 1 $i=0; while($i < ($lengthofnum)) { $divider.="0"; // Adds a zero to the divider for every char after the decimal point $i++; } $divider=(int)$divider; // Converts the divider (currently a string) to an integer return $int+($dec/$divider); // compiles the total back as a numeric } 

But why go complicated when you can simply do :

$value = (float) str_replace(',','',$string); 
Sign up to request clarification or add additional context in comments.

2 Comments

FYI, you need cast SimpleXMLElement to the data type required, eg return (int) $count. Not sure where the formatting on the feedburner count is coming from though
Great thanks everyone for the help, now I see the problem started once I got over the 1,000 mark because my code was adding the , before processing the numbers, I wouldn't of gotten this without the help here thanks
2

The issue is here:

$count = $xml->feed->entry->attributes()->circulation; 

SimpleXMLElement::attributes returns a SimpleXMLElement object, and so does fetching one of the properties, but you can cast to the expected data type just like you did here in the twitter function:

$count = $xml->followers_count; $count = (float) $count; 

So, cast to integer, float, or even string:

$count = (int) $xml->feed->entry->attributes()->circulation; 

Comments

1

ensure, that the variables are numbers.

$totalCnt = (int)$twitCnt + (int)$feedCnt; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.