I'm working on a twitter widget right now that declares a tweetCount at the beginning of the code. It uses both PHP and JavaScript. I am trying to clean up code that I have working by hardcoding it simply using copying and pasting, now obviously this is a terrible way to go about it especially when I want to modify the tweet count later. I know in PHP you can create variable that is incrementing in value based on the iteration you are in the loop, i.e. tweet1, tweet2, tweet3, etc. I'm wondering how I would go about doing this same effect in JavaScript or jQuery. Sample code of the JavaScript that I have currently is below. The full code can be viewed HERE if you want to look at the full script. How do I go about auto incrementing the code in either jQuery or JavaScript? I looked at this stackoverflow post, but didn't fully understand how to integrate it to work in my code. The live example can be found HERE. Any help would be much appreciated.
EDIT:
Updated the code to reflect the nature of the way that PHP is pulling from the JSON feed from Twitter's API
EDIT2:
Switching to pure JavaScript to make it much cleaner to work with in addition to cleaning up the code and making the calls all client side. I will update the code to reflect it. Thanks for the advice and help!
EDIT3:[FINAL RESULT]
So I promised I would post the final code. Here's the result, thanks to all your input for the help. I also got a little help from my friend. HERE is the link. Any questions? Feel free to contact me and I'll be glad to answer them. I ended up taking roughly 40 lines of code off just from switching to pure JavaScript so that you guys can see how much that it helped! Furthermore, the Twitter API doesn't allow for client side calls for security reasons I guess, so I had to make the call in PHP unfortunately. It would have been nice to do it all in JavaScript, but I guess we'll have to live with a little PHP in the script. The code is way cleaner though in pure JavaScript!
Code that made the 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>";
?>
Initial code:
<?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
} ?>