Page 1 of 1

twitter code and $_GET function

Posted: Mon Sep 26, 2011 2:01 am
by ahhreeyell
I'm trying to write an site that uses Twitter data based on a search using the search_twitter() function. However, there is a problem. It works fine whenever I enter one word, but any time I enter a search term that's more than one word, I receive the following error message: Invalid argument supplied for foreach() in /Users/ahhreeyell/Sites/templateSearch.php on line 26

Does anybody know how to fix this?

Here's my code for the page with the form:

Code: Select all

<?php

//require the appropriate functions files
require_once('twitter_functions.php');


?>

<!DOCTYPE html>
<title>ask anything</title>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>

<br><br><br><br><br><br><br>

<div id="search">
<br><br>
<form action="script.php" method="get">
<input type="text" name="question" id="searchbox" />
<input type="submit" name="submit" id="submit" value="Ask" />
<br />
</form>
<br><br>

</div>

<body>
</body>
</html>
Here's my code for the PHP page where the user gets directed once s/he posts a question:

Code: Select all

<?php

//require the appropriate functions files
require_once('twitter_functions.php');

$query = $_GET["question"];

//a twitter search url
$url = "http://search.twitter.com/search.json?geocode=40.751744,-73.985882,10mi&q=$query&rpp=10";

//get some data!
$tweets = search_twitter($url);

?>
<!DOCTYPE html>
<html>
	<head>
		<title><?php echo $_GET["question"]; ?></title>
		<link rel="stylesheet" type="text/css" media="screen" href="styles.css" />
	</head>
	<body>
	
		<div class="tweets">
			<?php			
				//loop through each tweet
				foreach($tweets as $tweet) {		
					//within the loop, each $tweet contains a lot more information than is used in this example.
					//to see all information contained, try print_r($tweet) to see all of the pieces available.
				
					//create a unique counter using ternary syntax
					//more info can be found at http://php.net/manual/en/language.operators.comparison.php
					//under the "Ternary Operator" example
					/* Basically the equivalent of saying:
						if(isset($counter)) {
							$counter = $counter + 1;
						} else {
							$counter = 1;
						}
					*/
					$counter = isset($counter) ? $counter + 1 : 1;
					
					//get the contents of the tweet
					$text = $tweet->text;
					
					//get the user who tweeted the tweet
					$from_user = $tweet->from_user;

					//create the html structure
					//ends up looking like:
					/*
					
						<div class="tweet" id="post-1">
							<h1>This is the contents of my tweet</h1>
							<h3>This is the author information</h3>
						</div>
						<div class="tweet" id="post-2">
							<h2>This is the contents of my tweet</h2>
							<h3>This is the author information</h3>
						</div>
					
					*/
					
					//echo the opening tweet div, with dynamic id
					echo '<div class="tweet" id="post-' . $counter . '">';
					
					//determine if this is the first tweet or not, choose a tag accordingly
					if($counter == 1) {
						$tweetOpenTag = "<h1>";
						$tweetCloseTag = "</h1>";
					} else {
						$tweetOpenTag = "<h2>";
						$tweetCloseTag = "</h2>";

					}
					
					//choose the opening and closing tag for the author information
					$authorOpenTag = "<h3>";
					$authorCloseTag = "</h3>";
					
					//output the tweet
					echo $tweetOpenTag . $text . $tweetCloseTag;
					echo $authorOpenTag . $from_user . $authorCloseTag;
					
					//close the tweet div
					echo "</div>\r\n";
				

				} //close the for loop
			
			?>
		</div>
	
	</body>
</html>

Re: twitter code and $_GET function

Posted: Mon Sep 26, 2011 6:22 am
by Celauran
What is the content of $tweets when you're getting this error?