Jump to content

Adding a count and while loop using xml data


andrew_biggart

Recommended Posts

Hi I have currently written some code which uses of the Twitter API and extracts the information required to display my most current tweet on my website. This is all working perfectly but at the minute it is only displaying one tweet and I would like it to display as many as the variable $limit is set to.

 

I have tried numerous count with while loops but just cannot seem to get my head around the logic of it.

 

Here is the code im currently using which displays one tweet.

 

            <?php

					$username = "my_twitter_username";
					$limit = "2";
					$twitter_url = "http://twitter.com/statuses/user_timeline/$username.xml?count=$limit";

					$buffer = file_get_contents($twitter_url);
					$xml = new SimpleXMLElement($buffer);

					$status_item = $xml -> status;
					$status_id = $xml -> status -> id;
					$user_item = $xml -> status -> user;
					$user_id = $xml -> status -> user -> screen_name;
					$description =  $status_item -> text;
					$status_time =  $status_item -> created_at;
					$status_img = $user_item -> profile_image_url;

					$description = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $description);
				        $description = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $description);
				        $description = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $description);
				        $description = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a>", $description);

					echo "
					<div class='tweet-wrapper'>
						<div class='tweet-img'>
							<a href='http://www.twitter.com/la__academia' target='_BLANK'><img src='$status_img' alt='La Academia Twitter' style='width:30px height:30px;' /></a>
						</div><!-- tweet-img -->
						<div class='tweet-text'>
							<p class='tweet-p'>$description</p>
							<p class='tweet-time'>$status_time . <a href='http://twitter.com/?status=@$user_id%20&in_reply_to_status_id=$status_id&in_reply_to=$user_id' target='_BLANK' class='tweet-reply'>Reply</a></p>
						</div><!-- tweet-text -->
						<div class='cleaner'></div>
					</div><!-- tweet-wrapper -->						
					";

		?>

Thanks for any help.

Link to comment
Share on other sites

Ok I have got this but it is just displaying an infinite amount of the same tweet. Help Please?

 

            <?php

					$username = "la__academia";
					$limit = "2";
					$twitter_url = "http://twitter.com/statuses/user_timeline/$username.xml?count=$limit";

					$buffer = file_get_contents($twitter_url);
					$xml = new SimpleXMLElement($buffer);

					$status_item = $xml -> status;
					$statuses = $xml -> statuses;
					$count= count(array($statuses));
					$status_id = $xml -> status -> id;
					$user_item = $xml -> status -> user;
					$user_id = $xml -> status -> user -> screen_name;
					$description =  $status_item -> text;
					$status_time =  $status_item -> created_at;
					$status_img = $user_item -> profile_image_url;

					$description = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $description);
				        $description = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $description);
				        $description = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $description);
				        $description = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a>", $description);


					while ($count <= $limit) {
					echo "
					<div class='tweet-wrapper'>
						<div class='tweet-img'>
							<a href='http://www.twitter.com/la__academia' target='_BLANK'><img src='$status_img' alt='La Academia Twitter' style='width:30px height:30px;' /></a>
						</div><!-- tweet-img -->
						<div class='tweet-text'>
							<p class='tweet-p'>$description</p>
							<p class='tweet-time'>$status_time . <a href='http://twitter.com/?status=@$user_id%20&in_reply_to_status_id=$status_id&in_reply_to=$user_id' target='_BLANK' class='tweet-reply'>Reply</a></p>
						</div><!-- tweet-text -->
						<div class='cleaner'></div>
					</div><!-- tweet-wrapper -->						
					";
					}

		?>  

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.