I am currently working on twitter widgets that declare tweetCount at the beginning of the code. It uses both PHP and JavaScript. I'm trying to clear code that works for me with hardcoding, just using copy and paste, now obviously this is a terrible way to do this, especially when I want to change the tweet counter later. I know that in PHP you can create a variable that increments the value based on the iteration you are in the loop, i.e. Tweet1, tweet2, tweet3 etc. I am wondering how I will do the same effect in JavaScript or jQuery. The sample JavaScript code I currently have is given below. The full code can be viewed HERE if you want to see the full script. How do I make code auto-increment in jQuery or JavaScript? I looked at this column column , but could not fully understand how to integrate it to work in my code. A live example can be found HERE . Any help would be greatly appreciated.
EDIT:
Updated code to reflect the nature of how PHP pulls JSON from the Twitter API from a feed.
EDIT2:
Switching to pure JavaScript to make it cleaner to work with it, in addition to clearing the code and making calls on all client computers. I will update the code to reflect it. Thanks for the advice and help!
EDIT3: [FINAL RESULT]
Therefore, I promised that I would send the final code. Here is the result, thanks to all your help data. I also helped my friend a bit. HERE is a link. Any questions? Feel free to contact me and I will be happy to answer them. I ended up taking about 40 lines of code, just switching to pure JavaScript so you guys can see how much it helped! Also, the Twitter API does not allow client-side calls for security reasons, I think that's why I had to make a call in PHP, unfortunately. It would be nice to do all this in JavaScript, but I think we will have to live with a little PHP in a script. The code is cleaner, although in pure JavaScript!
Code that made JavaScript possible:
<?php $tweetData = file_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$tweetCount"); echo "<script> var tweets = " . $tweetData . ";</script>"; ?>
Source:
<?php for($i = 0; $i < $tweetCount; $i++) { ?> ${"tweet{$i}"} = $decode[$i][text]; ${"tweetTime{$i}"} = $decode[$i][created_at]; <script type="text/javascript"> var i = "<?php echo $i ?>"; var tweetTime1 = "<?php echo ${"tweetTime{$i}"} ?>"; var datebefore1 = new Date(tweetTime1.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/, "$1 $2 $4 $3 UTC")); var tempdate1 = datebefore1.toString(); var date = "GMT-0800 (Pacific Standard Time)"; var date1 = tempdate1.split(date)[0]; </script> <?php } ?>
source share