PHP Loop while updating variable

chugarah
chugarah used Ask the Experts™
on
Hello Experts out there. I have small and simple problem
Trying to calculate and show each update before going to next numer of the loop

$i=1;
while($i < 10){

	sleep(1);
  $i++;
    echo($i);
}

Open in new window

.

Output

2345678910

I want to see if its possible to show something like this:
2
sleep
replace 2 with new (3)
sleep
replace 3 with new (4)
and so on.

The sleep timer is set to 1second. Trying to replace old value in the varaible instead of printing it as you see in the first output.
Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Am I right in thinking that you want to display only 1 number at a time - like a clock ticking?

show number 2, wait a second, show number 3, wait a second etc.

If so, you won't be able to do this with PHP (at least not in the same page). You'll have to do it with Javascript. PHP can't update the browser display dynamically. The whole of your loop will happen on the server side, and only display a page once completed - taking 10 seconds.

If that's not what you're after, can you give us more info.

Something like this?
$i=1;
while($i < 10){

	echo $i.'<br>sleep<br>';
	sleep(1);
	
  $i++;
  
    echo 'Replace '.$i-1.' with new ('.$i.')<br>';
};

Open in new window

Author

Commented:
Hi ChrisStanyon: tand smadeira:, thx for the incredible fast answer hhehe, like buger king :) but better..nvm

Yes I have allready an ajax client side: Code below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
	#wrap{
		font-family:"Lucida Grande", Arial;
		width:800px;
		margin:100px auto;
	}
	a:link,a:visited{
		color:#202020;
		text-decoration:none;
	}
	a:hover{
		background-color:#FFF2D5;
	}
	h2,h3{margin:2px 0;}
	#slider{
		height:300px;
		margin:50px 0;
	}
	#current_value{
		position:absolute;
		margin:0 0 0 35px;
		background-color:#FFF2D5;
		padding:4px;
	}
	#info{
		margin:10px 0 0 0;
		display:none;
		background-color:#FFF9EB;
		padding:8px;
	}

</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="jquery-1.5.1.min.js"></script>
<script language="javascript">

$(document).ready(function(){   
//ajaxTime.php is called every second to get time from server   
var refreshId = setInterval(function()
{     
$('#timeval').load('value.php?randval='+ Math.random());   
}, 30);   
//stop the clock when this button is clicked   
$("#stop").click(function(){     
clearInterval(refreshId);   
});

	$("form#submit").submit(function() {
	// we want to store the values from the form input box, then send via ajax below
	var fname = $('#fname').attr('value');
		
		$.ajax({
			type: "POST",
			url: "post.php",
			data: "fname="+ fname,
			success: function(){
				$('form#submsit').hide();
				//$('form#submit :input').val("");
				$('div.success').fadeIn();
			}
		});
	return false;
	});

});

</script>



</head>
<body>

<div align="center" id="timeval">--:--:--<br />

Here is the ajax update div
</div>

  
  
  
<br />
<br />



<br />
<br />

</body>
</html>

Open in new window



IIgnore the last POST.php thingy, its another thing for later. The inportant thing that make this possible is

//ajaxTime.php is called every second to get time from server   
var refreshId = setInterval(function()
{     
$('#timeval').load('value.php?randval='+ Math.random());   
}, 30);   
//stop the clock when this button is clicked   
$("#stop").click(function(){     
clearInterval(refreshId);   
});

Open in new window


ChrisStanyon yes, exactly, planing to do an math caculation. Example an value that increse
with 15% every step in the loop. I am not sure if this is the best way to do.

smadeira, I will try your code :)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
smadeira:
trying your code recently, I didnt get to work tried to change it wery litle
 echo 'Replace '.($i-1).' with new '.$i.'<br>';

Open in new window


Output


1
sleep
Replace 1 with new 2
2
sleep
Replace 2 with new 3
3
sleep
Replace 3 with new 4
4
sleep
Replace 4 with new 5
5
sleep
Replace 5 with new 6
6
sleep
Replace 6 with new 7
7
sleep
Replace 7 with new 8
8
sleep
Replace 8 with new 9
9
sleep
Replace 9 with new 10

Still the same problem as in the begining, trying to remove all the other values and show
only the updated one..for example

1
sleep
Replace 1 with new 2
----Clear Scren, remove text---
2
sleep
Replace 2 with new 3
----Clear Scren, remove text---
And so on. Sorry for the bad info.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
How about something like this.

This code assumes a DIV in your page with an ID of counter.
<div id="counter"></div>

and calls value.php file every second, passing in the current value of the counter. It adds 1 to the counter value and replaces the old value.

Clicking on a button with ID of stop will stop the counter.


//within your HTML page.
<script language="javascript">
$(document).ready(function(){   
	var refreshId = setInterval(function() { $('#counter').load('value.php?currVal='+ $('#counter').text()); }, 1000);
	$("#stop").click(function(){ clearInterval(refreshId); });
});
</script>

<div id="counter"></div>
<button id="stop">Stop</button>


//value.php
<?php
if (!isset($_GET['currVal'])) { $_GET['currVal'] = 1; }
echo $_GET['currVal'] + 1;
?>

Open in new window

Author

Commented:
Wow :)) you did it ChrisStanyon.
It works like a charm, I can now extended further, thanks alot,:)
Thank you wery much

Author

Commented:
Greate :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial