ERROR with if statement

Hi,
I have problem with PHP code. The result shows exactly what I want but it enter to if statement regardless there is an error or not.

the output is attached

Can you help me to sole this problem, please?

[php]<?php
require_once(‘TwitterAPIExchange.php’);
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
‘oauth_access_token’ => “enterd code”,
‘oauth_access_token_secret’ => “enterd code”,
‘consumer_key’ => “enterd code”,
‘consumer_secret’ => “enterd code”
);
$url = “https://api.twitter.com/1.1/statuses/user_timeline.json”;
$requestMethod = “GET”;
if (isset($_GET[‘user’])) {$user = $_GET[‘user’];} else {$user = “iagdotme”;}
if (isset($_GET[‘count’])) {$count = $_GET[‘count’];} else {$count = 20;}
$getfield = “?screen_name=$user&count=$count”;
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string[“errors”][0][“message”] != “”)
{echo “

Sorry, there was a problem.

Twitter returned the following error message:

”.$string[errors][0][“message”]."

";exit();}
foreach($string as $items)
{
echo “Time and Date of Tweet: “.$items[‘created_at’].”
”;
echo “Tweet: “. $items[‘text’].”
”;
echo “Tweeted by: “. $items[‘user’][‘name’].”
”;
echo “Screen name: “. $items[‘user’][‘screen_name’].”
”;
echo “Followers: “. $items[‘user’][‘followers_count’].”
”;
echo “Friends: “. $items[‘user’][‘friends_count’].”
”;
echo “Listed: “. $items[‘user’][‘listed_count’].”

”;
}
?> [/php]


All your if statements have

[php];}[/php]

It should be

[php]};[/php]

Change these and it will probably work.

Uhm…no
[php]if (isset($_GET[‘user’])) {$user = $_GET[‘user’];} else {$user = “iagdotme”;}[/php] is correct the other way will give you an error. There’s an error somewhere else, turn on error reporting will help you out:

Put this at the top of the page or in a configuration file:
[php]/* Turn on error reporting */
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(-1);[/php]

A better way to write this would to be use a tenary statement:
[php]$user = (isset($_GET[‘user’])) ? $_GET[‘user’] : “iagdotme”;[/php]

If it’s a logic error it’s going to be a little tougher to debug.

Me bad you have errors turned on. :stuck_out_tongue:

Your problem likes somewhere in here:
[php]if($string[“errors”][0][“message”] != “”)
{echo “

Sorry, there was a problem.

Twitter returned the following error message:

”.$string[errors][0][“message”]."

";exit();}[/php]

I would format it this way for it would be easier to read:
[php]if($string[“errors”][0][“message”] != “”) {
echo “

Sorry, there was a problem.

Twitter returned the following error message:

”.$string[errors][0][“message”]."

";
exit();
}[/php]

Valkrider and Strider64 thank you for your response. I know the problem from the if statement that you mentioned. I have been working to solve it but I cannot.

Okay, sorry for jumping in, but, it is a simple error. Your line # 19 where the error is also includes line #20.
That is actually one clause…

So, look at your line 19-20:

if($string["errors"][0]["message"] != "") {echo "

Sorry, there was a problem.

Twitter returned the following error message:

".$string[errors][0]["message"]."

";exit();}
Now, the first $string[] uses an INDEX of "errors" and the second one uses an INDEX of errors ! ! ! So, add some quotes on the second one! HINCE, THE INDEX ERROR MESSAGE! [php] if($string["errors"][0]["message"] != "") {echo "

Sorry, there was a problem.

Twitter returned the following error message:

".$string["errors"][0]["message"]."

";exit();} [/php]

Thank you ErnieAlex

I solve the problem and it works perfectly

Thank you agine for you explanation

Glad I found it for you. Good luck. If you have any other issues create a new post. I will mark this solved…

Sponsor our Newsletter | Privacy Policy | Terms of Service