PHP Loop while updating variable

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
LVL 1
chugarahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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.

0
Scott MadeiraCommented:
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

0
chugarahAuthor 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 :)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

chugarahAuthor 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.
0
Chris StanyonWebDevCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chugarahAuthor Commented:
Wow :)) you did it ChrisStanyon.
It works like a charm, I can now extended further, thanks alot,:)
Thank you wery much
0
chugarahAuthor Commented:
Greate :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.