Results 1 to 12 of 12

Thread: Flush will not work

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Flush will not work

    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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Flush will not work

    Did you try output bffering?
    PHP Code:
    ob_start();

    for (
    $i 0$i 100000$i++) {
      echo 
    $i;
      
    ob_flush();
    }

    ob_end_clean(); 

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: Flush will not work

    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.

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Flush will not work

    Javascript would be something like:
    Code:
    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

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Flush will not work

    I can't really remember either but I think something like this would work

    Code:
    window.onload = init;
    var obj;
    
    function init()
    {
      obj = document.getElementById('counter');
      for (var i = 1; i < 100000; i++) {
        obj.text = i.toString();
      }
    }
    HTML Code:
    <body>
      <!-- ... -->
      <p id="counter">0</p>
      <!-- ... -->
    </body>

  6. #6
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Flush will not work

    Quote Originally Posted by penagate
    I can't really remember either but I think something like this would work

    Code:
    window.onload = init;
    var obj;
    
    function init()
    {
      obj = document.getElementById('counter');
      for (var i = 1; i < 100000; i++) {
        obj.text = i.toString();
      }
    }
    HTML Code:
    <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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: Flush will not work

    :mrgreen:

    You have totally skipped php part.

    PHP page would calculate and variable should be shown on page "live".


  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Flush will not work

    I don't get it ... could you explain exactly what your desired effect is?

  9. #9
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Flush will not work

    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
    PHP Code:
    session_start();
    for(
    $i=0;$i<100000;$i++)
    {
        
    $_SESSION['value'] = $i;

    query.php
    PHP Code:
    session_start();
    print 
    $_SESSION['value']; 
    and your display page
    Code:
    <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>

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: Flush will not work

    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
    Last edited by slice; Mar 21st, 2006 at 02:23 PM.

  11. #11
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Flush will not work

    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?

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: Flush will not work

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width