0

I got a script that:

  • reads urls from a txt file
  • does some calculations
  • inserts results into a table

I want to replace txt file with php array. Heres my current code:

<script type="text/javascript">

   $.get("imones.txt", function (data) {
        var array = data.split(/\r\n|\r|\n/);
        var beforeLoad = (new Date()).getTime(); 
        var loadTimes = []; 
            var beforeTimes = [];                           
        $('#frame_id').on('load', function () {                                             
            beforeTimes.push(beforeLoad); /
            loadTimes.push((new Date()).getTime());               
            $('#frame_id').attr('src', array.shift());  
                try {
                $.each(loadTimes, function (index, value) {                 
                    var result = (value - beforeTimes[index]) / 1000; 
                        if (result < 0) { 
                            result = result * (-1);
                        }
                    $("#loadingtime" + [index]).html(result);                       
                    beforeLoad = value;             
                });
                } catch(ex) {}
        }).attr('src', array.shift()); 

</script>

It reads from imones.txt, then inserts each url into a frame, does some calculations, and then inserts results into #loadingtime div. I want to replace imones.txt with a php array. Also i would like the output to be stored in another php array instead of storing it in a div. Can someone help me with this?

2
  • by replacing imones.txt by a php array you mean replacing "imones.txt" with "imones.php" which contains a PHP array which value you want to retrieve? Commented Mar 18, 2013 at 12:50
  • No its just a $array = array(); in the same file, it stores the same urls as imones.txt Commented Mar 18, 2013 at 12:51

2 Answers 2

2

Try something like this:

<?php
    $str = implode(',',$yourPhpArr);
?>

<script type="text/javascript">

    var urls = "<?=$str?>";

    var array = urls.split(/,/);
    var beforeLoad = (new Date()).getTime(); 
    var loadTimes = []; 
    var beforeTimes = [];

    $('#frame_id').on('load', function () {                                             
       beforeTimes.push(beforeLoad); /
            loadTimes.push((new Date()).getTime());               
            $('#frame_id').attr('src', array.shift());  
                try {
                $.each(loadTimes, function (index, value) {                 
                    var result = (value - beforeTimes[index]) / 1000; 
                        if (result < 0) { 
                            result = result * (-1);
                        }
                    $("#loadingtime" + [index]).html(result);                       
                    beforeLoad = value;             
                });
                } catch(ex) {}
        }).attr('src', array.shift()); 

</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Depending on your configuration, <?=$str?> may not work. It that's the case use <?php echo $str ?> instead
You are right, but it really annoying when you insert lot of vars to php :) I read somewhere what in next versions <?= will work like <?php echo for any configuration.
0

Let a php file echo your array:

echo Array(1,2,3,4,5);

your html/javascript:

   $.get("yourphp.php", function (data) {
        var array = data.split(/\r\n|\r|\n/);
        var beforeLoad = (new Date()).getTime(); 
        var loadTimes = []; 
            var beforeTimes = [];                           
        $('#frame_id').on('load', function () {                                             
            beforeTimes.push(beforeLoad); /
            loadTimes.push((new Date()).getTime());               
            $('#frame_id').attr('src', array.shift());  
                try {
                $.each(loadTimes, function (index, value) {                 
                    var result = (value - beforeTimes[index]) / 1000; 
                        if (result < 0) { 
                            result = result * (-1);
                        }
                    $("#loadingtime" + [index]).html(result);                       
                    beforeLoad = value;             
                });
                } catch(ex) {}
        }).attr('src', array.shift()); 

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.