4

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
} ?>
Community
  • 1
  • 1
Elias Ranz
  • 401
  • 1
  • 6
  • 21
  • 1
    Never ever ever write JavaScript and PHP in the same file! Use a ` – Aram Kocharyan Dec 31 '12 at 05:46
  • Why don't you replace '1' with '$i'? – bozdoz Dec 31 '12 at 05:46
  • @bozdoz I tried that and it didn't seem to like that at all – Elias Ranz Dec 31 '12 at 05:49
  • At the moment it seems you're data is available server side with PHP. How do you get access to your data? Why can't you use AJAX in JavaScript to achieve the same? – Aram Kocharyan Dec 31 '12 at 05:49
  • @AramKocharyan I'm using JSON to do the pull from twitter and then parsing it using php. Also the reason I'm using javascript is because I found a post that talked about displaying the date properly as well as editing the tweets to have linked hashtags, mentions, and of course links. – Elias Ranz Dec 31 '12 at 05:54
  • I would use only JavaScript for this. If it's JSON (JavaScript Object Notation) I'm assuming you can use `$.get()` and do your work from there. – Aram Kocharyan Dec 31 '12 at 05:58
  • @bozdoz just tried it again and added the line `console.log(tweetTime0);` and it says `Uncaught ReferenceError: tweetTime0 is not defined` theoretically that should be the first iteration of the loop. – Elias Ranz Dec 31 '12 at 06:00
  • @AramKocharyan I took a look at the $.get(), not entirely sure how I would parse the data that I need. I am currently doing it with `${"tweet{$i}"} = $decode[$i][text];` decode being generated from json_decode. I found the idea and then modified it to my need from [THIS](http://tareq.wedevs.com/2009/05/playing-with-twitter-json-using-php/) link. – Elias Ranz Dec 31 '12 at 06:10
  • There is no need to decode JSON in JavaScript since it's a subset of a JavaScript object. – Aram Kocharyan Dec 31 '12 at 07:32
  • @Elias I feel like you're not doing it right then. I output php like this, and get a value: http://jsfiddle.net/bozdoz/ZNJvt/. And check out the PHP: http://ideone.com/UrDTDb – bozdoz Dec 31 '12 at 08:30
  • I got it, posting the final code here in a second. Thanks though. I converted it all into js and a little php :) – Elias Ranz Dec 31 '12 at 08:32

2 Answers2

3

You may get all your php data into the js variable,

then using pure js after that, do not mix them again.

//['item1','item2','item3'],js array formation
var jsData = <?php echo $decode ?>;
var jsCount = <?php echo $tweetCount ?>;
for(var i=0;i<jsCount;i++){
    alert(jsData[i]);
}

//or

<script type="text/javascript">
<?php 
for ($i = 0; $i < $tweetCount; $i++) {
?>
    //here you can get your tweet1,tweet2,tweet3...
    var tweet<?php echo $i ?> = "<?php echo $i ?>"+'any data you want to append.';  
<?php
}
?>
</script>
meadlai
  • 895
  • 1
  • 9
  • 22
  • you want to create many JS-variable depends on PHP? so PHP will execute in the server at first, then it generate HTML & JS in the html file. so you can using Array also. – meadlai Dec 31 '12 at 06:11
  • how would I go about getting the exact value that I'm looking for? So for instance with the php version it's like `$decode[$i][text];` where i is the iteration through the loop, ie `$decode[0][text];` being the first tweet and `$decode[1][text];` being the second, etc. – Elias Ranz Dec 31 '12 at 06:15
  • I have updated my code, you can have a try. remember that: php will execute before JavaScript. – meadlai Dec 31 '12 at 06:21
  • I guess what I was trying to get at was how do you go about getting the text that I am looking for so in this case it's the tweet text? See [THIS](http://tareq.wedevs.com/2009/05/playing-with-twitter-json-using-php/) link to see what the JSON is producing in terms of the layout. I understand how it works in PHP, but not how to interact with that in JS, that's why I went with the PHP route. I'm open to switch to JS purely especially if it makes it a lot easier for me to code. – Elias Ranz Dec 31 '12 at 06:28
  • Selecting this as an answer because this was ultimately what I ended up doing. I switched pulling the data in using JSON then storing it in a variable in PHP, which really was a JavaScript variable. – Elias Ranz Dec 31 '12 at 08:42
2

To make it simple, you can create JavaScript array from your $tweetCount array as below:

$temp = "var temp = new Array();";
for($i = 0; $i < $tweetCount; $i++) {
   $temp.="temp.push(new Array(".$tweetCount[$i].")";
} 

Echo this $temp variable in your JavaScript script tag. After that, you can you use your temp array in your JavaScript script as below:

for(var i=0;i<temp.length;i++){
    console.log(temp[i]);
}
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
THE ONLY ONE
  • 2,110
  • 1
  • 12
  • 6
  • I get the following error on the console `Uncaught ReferenceError: temp is not defined` even though shouldn't it be generated from the `$temp.=...` line? – Elias Ranz Dec 31 '12 at 06:37