Click to See Complete Forum and Search --> : Flush will not work
slice
Mar 16th, 2006, 09:39 AM
I want to display live in the browsers
for($i=0;$i<100000;$i++)
{
//here would be something like echo ($i)
}
but the problem is that flush() will not work because it flushes whatever is in memory.In this problem visitor will see value of $i in the box changing with high speed.
I think there will be some javascript usage in this problem.
Your help is highly appreciated
penagate
Mar 17th, 2006, 04:32 AM
Did you try output bffering?
ob_start();
for ($i = 0; $i < 100000; $i++) {
echo $i;
ob_flush();
}
ob_end_clean();
slice
Mar 20th, 2006, 05:43 AM
I have tried it.
But its not working as i need because it displays like
123456789101112.....
I want
1 and then it should change 2 and then it should change to 3 and so on...
I think there should be some javascript trick.
Ecniv
Mar 21st, 2006, 04:28 AM
Javascript would be something like:
for (i=1; 100000; ++) {
obj.innerHTML = i
}
Very rough as I haven't used javascript in ages.
obj is the object on the page - how you get to it and which bowser that works in - dunno ;)
penagate
Mar 21st, 2006, 08:37 AM
I can't really remember either but I think something like this would work
window.onload = init;
var obj;
function init()
{
obj = document.getElementById('counter');
for (var i = 1; i < 100000; i++) {
obj.text = i.toString();
}
}
<body>
<!-- ... -->
<p id="counter">0</p>
<!-- ... -->
</body>
john tindell
Mar 21st, 2006, 08:47 AM
I can't really remember either but I think something like this would work
window.onload = init;
var obj;
function init()
{
obj = document.getElementById('counter');
for (var i = 1; i < 100000; i++) {
obj.text = i.toString();
}
}
<body>
<!-- ... -->
<p id="counter">0</p>
<!-- ... -->
</body>
Instead of obj.text use either obj.innerHTML or obj.innerText depending on what you want to do with it.
slice
Mar 21st, 2006, 08:51 AM
:mrgreen:
You have totally skipped php part. :)
PHP page would calculate and variable should be shown on page "live".
:)
penagate
Mar 21st, 2006, 08:53 AM
I don't get it ... could you explain exactly what your desired effect is?
john tindell
Mar 21st, 2006, 09:17 AM
I think I know what you want. You would need to store the current data in a database or session, then have another page to query that informatio. so....
page.php
session_start();
for($i=0;$i<100000;$i++)
{
$_SESSION['value'] = $i;
}
query.php
session_start();
print $_SESSION['value'];
and your display page
<p id="counter">0</p>
<script language="javascript" type="text/javascript">
function createRequestObject()
{
var request_o; //declare the variable to hold the object.
var browser = navigator.appName; //find the browser name
if(browser == "Microsoft Internet Explorer"){
/* Create the object using MSIE's method */
request_o = new ActiveXObject("Microsoft.XMLHTTP");
}else{
/* Create the object using other browser's method */
request_o = new XMLHttpRequest();
}
return request_o; //return the object
}
function handlePHPValue()
{
try
{
if(http.readyState == 4)
{
var response = http.responseText;
document.getElementById('counter').setInnerText = response;
}
}
catch(Exception)
{
}
}
var http = createRequestObject();
function getPHPValue()
{
try
{
http.open('get', 'query.php');
/* Define a function to call once a response has been received. This will be our
handleProductCategories function that we define below. */
http.onreadystatechange = handlePHPValue;
/* Send the data. We use something other than null when we are sending using the POST
method. */
http.send(null);
timerIDThought = self.setTimeout("getPHPValue()", 100);
}
catch(Exception)
{
http = createRequestObject();
getPHPValue();
}
}
getPHPValue();
</script>
slice
Mar 21st, 2006, 01:19 PM
I am sure it would work...
thank you john. :)
Just one little confusion how should i synchronize all three files(page.php,query.php and index.php).
If visitor browse anysite.ext/calc/index.php
If i call function of page.php on the top then after its complete execution javascript will run which would check query.php .
So how can i synchronize the process..?
Please help in understanding this last problem.
Once again thank you john tindel and all other helpers :wave:
john tindell
Mar 21st, 2006, 04:28 PM
The method i have posted is a pain in the arse to get to work, just been trying. But storing the information in a database and retrieving it is better than sessions.
I managed to get the code working, I think, but it was unpredictable and doesnt seem worth the trouble to make a half decent script. With the thought that php scripts timeout after 30 seconds what infomration do you want displaying to the user?
Could this just not be fudged using javascript to give the false impression of realtime processing?
slice
Mar 22nd, 2006, 08:37 AM
I want user should be able to see live values which are being processed.
even if there is 5 seconds delay it would be acceptable.
for loop was just an example.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.