0

I'm echoing a variable that adds my links (outputting a number) and totaling them. Since it's PHP it loads first and makes my site wait until finished loading. Can this be written in jQuery? I'm very new to JavaScript.

<?php  

 $a = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.batterytheme&output=number');
 $a = str_replace(",", "", $a);
 $b = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.connectiontheme&output=number');
 $b = str_replace(",", "", $b);
 $c = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.icontheme&output=number');
 $c = str_replace(",", "", $c);
 $d = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.percenttheme&output=number');
 $d = str_replace(",", "", $d);
 $e = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.statusnotifiertheme&output=number');
 $e = str_replace(",", "", $e);
 $f = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.cnote&output=number');
 $f = str_replace(",", "", $f);
 $g = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.iaccescnotekb&output=number');
 $g = str_replace(",", "", $g);
 $h = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.cnotelite&output=number');
 $h = str_replace(",", "", $h);
 $i = (301); //From c-note and Multi Lock Screen Theme on Rock Your Phone
    $j = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.multibrowsericon&output=number');
 $j = str_replace(",", "", $j);
 $k = file_get_contents('http://modmyi.com/cstats/index.php?package=com.modmyi.changeappstoreiconwithinstallous&output=number');
 $k = str_replace(",", "", $j);

 $z = $a+$b+$c+$d+$e+$f+$g+$h+$i+$j+$k;
 $z = number_format($z);
 echo $z;

?>
Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
cnotethegr8
  • 7,342
  • 8
  • 68
  • 104
  • can you expand on what this does a little more I am not sure whether I understand – Simon H Dec 17 '10 at 05:21
  • @cnotethegr8: what ttp://modmyi.com/cstats/index.php?package=com.modmyi.cnotelite&output=number is returning html javascript? – RageZ Dec 17 '10 at 05:21
  • Yes, you can accomplish the same thing with AJAX. However, I'm not super familiar with the jQuery ajax tools, so I'll leave the specifics to someone else. – Sam Dufel Dec 17 '10 at 05:24
  • 1
    What exactly are you trying to accomplish? Does this code actually work? If you're looking for a speed improvement, I doubt you'll see much appreciable improvement by switching up your code. It's likely you're waiting on the other script to deliver the results, not on your script. – Surreal Dreams Dec 17 '10 at 05:27
  • I think you'd be better off just to ask how to do whatever it is you're doing with jQuery and forget trying to convert the PHP business. – Brad Mace Dec 17 '10 at 05:29
  • @Jacob Relkin: Feel free to clean up my code. I just started teaching myself PHP about two months ago. Your comments aren't necessary. This code has a url that outputs a Javascript code in HTML (a live download count.) I wish to total these numbers together and be able to echo the final number. If anyone has a better solution please tell me! – cnotethegr8 Dec 17 '10 at 05:39

3 Answers3

0

you can use $.ajax. for example.

var a;
var b;
var c;
$.get('http://modmyi.com/cstats/index.php?package=com.modmyi.batterytheme&output=number', function(data) {
  a = data;
});

$.get('http://modmyi.com/cstats/index.php?package=com.modmyi.connectiontheme&output=number', function(data) {
  b = data;
});

 ... etc 

The only tricky thing would be to verify all the ajax call finished. The ajax call are asynchronous so you have no real way to make sure all the data you need are there. One easy solution would make one more ajax call in the success function callback of the preceding ajax call ie

$.get('url1', function(data){
    $.get('url2', function(data){
    });
});
RageZ
  • 26,800
  • 12
  • 67
  • 76
  • 1
    IMO, run the ajax requests sequentially - send the second one when the first one comes back, and so on. That way you don't get any synchronization issues adding up the results. – Sam Dufel Dec 17 '10 at 05:31
  • Thanks RageZ. So i can continue through all the links like this, but how do i output it? My knowledge in JavaScript is very limited. I've only used jQuery to add actions and classes... – cnotethegr8 Dec 17 '10 at 05:33
  • It's not the greatest but here is a solution that I came up with to have an event fire after a specified number of requests are complete: http://stackoverflow.com/questions/4368946/javascript-callback-for-multiple-ajax-calls/4369592#4369592 – subhaze Dec 17 '10 at 06:06
0

You have to remember firstly that JavaScript is not an alternative to PHP. PHP is a server-side language, JavaScript is purely client-side. One cannot replace the other.

You can convert that sample into usable JS. But, it will differ from a server-side implementation in that its behavior will be completely dependent upon the user agent and its capability to execute JavaScript.

Make a new PHP script that returns the adverts in XML format, and you can use the XMLHttpRequest object to obtain them through Javascript. I can write a sample of that in a sec if you like.

Neo
  • 15,491
  • 59
  • 215
  • 405
0

$.ajax({ url: 'http://modmyi.com/cstats/index.php?package=com.modmyi.batterytheme&output=number', success: function(data) { $('.result').html(data);} });

Better to store the result in an array or just add it another variable here itself by replacing

$('.result').html(data);

Joe
  • 610
  • 7
  • 21
  • Add the other urls sequentially to this and you will get your stuff done. To output the data have something like $('#result').html(data); where the div/span with id="result" is where you want to show the stuff and data is the total sum. – Joe Dec 17 '10 at 06:46