-4

I have something like this

<?php 
    for ($x=0; $x<10; $x++) {

        echo $x;
}
?>

Now i need to display that $x data one after another with one second delay; i believe i need javascript to do that.

I've found various of similar scripts, but none worked for me, can anyone post any example on how to do that?

Thanks in advance

skymario84
  • 123
  • 1
  • 12
  • 1
    I think there may be some confusion. PHP is ***server-side*** and JavaScript is ***client-side***. By the time the JavaScript begins to execute, the PHP has already completed it's execution. The only way to accomplish something like this is with AJAX. – War10ck Jul 07 '14 at 16:30
  • 1
    Please show us what you tried and what went wrong. – showdev Jul 07 '14 at 16:30
  • wow, you need to stop what you're doing right now and readup on backend vs front end – andrew Jul 07 '14 at 16:31
  • is it possible in any way to delay the output of for loop for one second and display it one after another? – skymario84 Jul 07 '14 at 16:32
  • @skymario84 No. Again, you're talking server-side vs client-side. You can delay the PHP execution by 1 second intervals. All that is going to do though is delay the output and probably show a blank white page until the script finishes execution. The user will still see the completed output from the script execution (i.e. All numbers, not one at a time). – War10ck Jul 07 '14 at 16:35
  • 2
    @War10ck well, technically its prob possible with a websocket or something, but @@skymario84 it would be better to send all the data to the browser in one hit, probably in a json encoded array, then loop through in javascript – andrew Jul 07 '14 at 16:39
  • @andrew thanks, i wanted to do something like that, but can't get it to work, can you post any examples on how to do that? – skymario84 Jul 07 '14 at 16:41
  • @andrew That's true. Very good point. – War10ck Jul 07 '14 at 16:41
  • Thanks everyone, this solved my problem: http://stackoverflow.com/questions/15036232/php-loop-how-to-print-each-result-and-delay-it-for-a-second-before-echoing-anot?rq=1[link] @Rich's answer solved it all... – skymario84 Jul 07 '14 at 17:06

1 Answers1

0

Yes, you'll need JavaScript for this. All PHP can do is emit the values to client-side code. Then it's up to the client-side code to display those values.

So, for example, your PHP code can populate a JavaScript array:

<script type="text/javaScript">
    var values = <?php echo json_encode($php_variable); ?>;
</script>

In this case $php_variable would be an array containing the values you want to display. Now your client-side code has an array called values which it can iterate. You can use setTimeout to schedule something to happen with a delay. Iterating over that array, it might look something like this:

var index = 0;
var displayValue = function () {
    if (index >= values.length) { return; }

    var value = values[index++];
    // display "value" somewhere on your page

    setTimeout(displayValue, 1000);
};
displayValue();

This should display each value in the array with 1-second increments. Note that I sort of glazed over the "display somewhere on your page" part, since that's up to you really. Where/how are you looking to display this? This works a little differently client-side than it does server-side, since the entire HTML document is already rendered. You need to identify where in that document you want to display the value and display it there, not just "echo" it.

David
  • 208,112
  • 36
  • 198
  • 279