[FIXED] Echo from a script

 
Post new topic   Reply to topic    PHP User Group Malta Forum Index // PHP-Javascript Projects
View previous topic :: View next topic  
Author Message
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 56
Location: Iklin

PostPosted: Sun Dec 16, 2007 9:27 pm    Post subject: [FIXED] Echo from a script Reply with quote

 hi guys,

well basically I am writing a simple routing script. It reads data from an XML and from a PostgreSQL.
 
Some of the data is being randomized, and every now and then some data is echoed in the background.
 
Is there a way to have the script, using XMLHTTPResponse (AJAX), to make this :
 
 

     
  •  Run the script in the background
     
  •  Whenever the script echo's soemthing it echoes to the main <div> I want it to
     
  •  The new echo should replace the prior echo
     
  •  The echo should be real time / live 
     
  •  The echo can not be saved in a Javascript function and parsed to the user one by one
     


The script which echo's data is already done via PHP and can be accessed here : http://yancho.no-ip.org/~yancho/xml_parser.php

As you can see it echo's randomly (basically before reading each XML edge, it waits 10 seconds, but then, since sometimes it compares and fails, and has to compare again, it waits another 2 seconds.

That means that the looping cannot be done in javascript, and has to be done on the PHP script.

On my php script, at the moment I am using this :

php:

ob_start 
();
for (
$i=1$i $k $i++) {
         echo 
"<b>================================++++++++++++++++++++++++++++++=========================================</b>";
         echo 
"<b><color=red>                       WORKING ON A NEW EDGE!!!!!                   </color></b>";
         echo 
"<b>================================++++++++++++++++++++++++++++++=========================================</b>";

         echo 
"<br><b><i>WORKING ON EDGE :  ".$xml->edge[$i]->id." WITH THE NAME ".$xml->edge[$i]->sname." </i></b></br></br>";
       
$no_of_linestrings get_how_many_linestrings_in_multi ($xml,$i);

  do {
       
//=  echo "<br>Randomizings one of the lines in the multiline <br>";
        // randomizing one of the lines
        
$randomline randomline ($xml$i ,$no_of_linestrings) ;

        
//=echo "<br>Checking how many points there are on this linestring <Br>";
        // Checking how many points there are on this linestring
        
$how_many_points_linestring howmanypoints ($randomline) ;


        
//=echo "<br>Now we got the line .. time to get a poin on the line <Br>";
        // randomizing a point on one of the lines
        
$rgeom randomgeom($xml,$i$how_many_points_linestring,  $randomline );
        
//=echo "<br>rgeom is : $rgeom <br>";
         //$rgeom now holds a random point on a random line string in the multistring we are dealing with




          //=echo "<br>Now getting end pt <Br>";
         // now it is time to get the actual ending point of the multi line string

         
$end_pt =  get_end_point($xml$i);

         
//$end_pt has the ending point of the  multilinestring


         //= echo "<br>Now comparing both <Br>";
         // now we need to compare distance
         
$dist =  compare_random_with_end ($rgeom$end_pt);

         
//$dist holds a t = true .. or an f = false
        //= echo "<br><b><i>Booelan Dist is : $dist </i></b><br>";


         
sleep(2); //sleeping 2seconds before next randomizing


  
}
  while ( 
$dist >= 30 );

        
// we are near 100meters of the ending point .. time to display the new node
          
echo date("H:i:s");
          echo 
node_details ($imake_time($i$xml), $xml );  
          
// ^-- THIS IS WHERE THE NODE IS GOING TO BE ECHOE'D
     
ob_flush();
     
flush() ;

     
sleep (10);

}



Any suggestions on which 

  • AJAX methods
  • Any PHP alterations

I need to read into will extremely help me please.

Thanks alot!!


_________________


Last edited by yancho on Wed Dec 19, 2007 12:42 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 56
Location: Iklin

PostPosted: Wed Dec 19, 2007 3:21 am    Post subject: Reply with quote
Well,

basically this is how I fixed it :

3 Scripts


  1. XML Parser (xmlparser.php) - gets the start/end point and parse the xml

  2. xml_parser using f_open() call another script (insertscript.php)and pass via GET the amount of seconds to sleep before saving into a table. This is the code of the query in xml_parser.php :

    Code:

    <?php

      function doquery ($sleep, $id, $tid, $contents) {

          $ip = $_SERVER['REMOTE_ADDR'];
          $contentz = 'sleep='.urlencode($sleep) . '&id=' .urlencode($id). '&tid=' .urlencode($tid). '&ip=' .urlencode($ip). '&contents=' .urlencode($contents);
          $page = 'http://localhost/~yancho/insertscript.php?'.$contentz;
          $fp = fopen ($page , "r");
          fgets($fp,1024);
          fclose($fp);

      }
    ?>

    In the insertscript.php an important function I used is : ignore_user_abort() so basically disallows the user to stop it

  3. In the mean time, an XMR AJAX script, using YahooUI starts every 5seconds asking for an update and if it finds, publish to <div> else, wait 5 seconds and start again. This is the AJAX request I am using :
    Code:
     var o = YAHOO.util.Connect.asyncRequest('GET', "next_instruction.php", callback);


  4. The PHP script next_instruction.php basically gets the last row in the table, outputs it, and delete the row (keeping the table always empty)

  5. The request wait for the prior one to finish, before starting a new one by doing this :
    Code:
    window.setTimeout(function(){ start() }, 5000);
    Calling the fucntion start() which wraps the asyncRequest forces the script to get the data first before opens another request



Hope this info helps someone who ends up with the same scenario like me Smile Hope also you don't waste a week playing with this **** Laughing

Links to scripts


Please remember this is NOT a tutorial, nor was it intended to be - Am just releasing these scripts for public domain so maybe someone else can find it useful.

Thanks to : killfrog, Maldman, hex4, Bizon Smile
_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Post new topic   Reply to topic    PHP User Group Malta Forum Index // PHP-Javascript Projects All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

alexisRed v1.2 // Theme Created By: Andrew Charron and Web Hosting Bluebook // Icons in Part By: Travis Carden
Boards optimised using the phpBB-SEO mod found at phpbb-seo.com
Boards hosted courtesy of solutions-lab.net
Link Backs : PHPClasses.org - MT Page :: PHPUsergroups.org - MT Page



Powered by phpBB © 2001, 2002 phpBB Group