I am trying to display some info from a ajax call using firephp and firebug.I am able to display someinfo when the ajax call is done. Is there a way to display while it is executing? For example my code below id inside a foreach loop.
foreach ($this->PreQualcalls as $value) {
ob_start();
if(isset($array ['env:Envelope'] ['env:Body']['n1:preQualResponse1207'])){
$this->setKeyNamePreQualResponse("n1:preQualResponse1207");
$this->setKeyNameServiceProducts("n1:ServiceProduct1207");
$this->setKeyNameModemProducts("n1:ModemProduct1207");
$this->firephp->log("entered the 1207 call");
}else{
$this->setKeyNamePreQualResponse("n1:preQual1308Response");
$this->setKeyNameServiceProducts("n1:ServiceProduct1308");
$this->setKeyNameModemProducts("n1:ModemProduct1308");
$this->firephp->log("entered the 1308 call");
}
ob_flush();
$this->firephp->log($this->getKeyNamePreQualResponse(),"Response type");
}
i want to be able to display getKeyNamePreQualResponse() in the console as it is being going through the loop.
Is it possible?
thank you.
FirePHP is designed to collect log information while the PHP script executes and then sends it in special headers along with the page response. It will not show logging calls in Firebug in realtime.
To debug your code using logging, place more logging calls into the loop. e.g.
$this->firephp->log("value", $value);
You have a loop that assigns a new value to $value on each iteration but don't use it in any of the statements in the loop. That does not seem right.
If you need realtime debugging I suggest using xDebug or other realtime logging tool.
Related
I have a php script which is something like below.
script.php
<?php
foreach($available as $loop)
{
echo file_get_contents($loop);
}
?>
basically loop runs like 100 to 200.When I run domain.com/script.php on browser..output is displayed only after all the loop is completed.But I want the page to update the output in realtime.
Is it posible by setting some kinda of header in php script or via htacces ?
You can do this with some servers using the flush() function.
You may be better off using JavaScript and AJAX to fetch and display progress of your long-running task, though.
My function have for loop and once loop it's output percent of that loop. It's look like this:
function checkisset($v){
foreach ($variable as $key=>$value)
if($v==$value) echo $key/count($variable);
}
and in client i want to show process of loop.
Ex: count($varibale)=10; First time in loop it will be 10%; and when 100% will run success function.
How can i do it??
Tks everyone.
To update your webpage dynamically, you have to establish communication between the client and the server, and send updates.
One way to send updates is "Server sent events". Another is "forever frame", where the page opens javascript link to the server and the server sends javascript statements over time.
In either case, you loop with updates will be a separate resource from the main page that you intend to update.
I have a JavaScript functions which calls a PHP function through AJAX.
The PHP function has a set_time_limit(0) for its purposes.
Is there any way to stop that function when I want, for example with an HTML button event?
I want to explain better the situation:
I have a php file which uses a stream_copy_to_stream($src, $dest) php function to retrieve a stream in my local network. The function has to work until I want: I can stop it at the end of the stream or when I want. So I can use a button to start and a button to stop. The problem is the new instance created by the ajax call, in fact I can't work on it because it is not the function that is recording but it is another instance. I tried MireSVK's suggest but it doesn't worked!
Depending on the function. If it is a while loop checking for certain condition every time, then you could add a condition that is modifiable from outside the script (e.g. make it check for a file, and create / delete that file as required)
It looks like a bad idea, however. Why you want to do it?
var running = true;
function doSomething(){
//do something........
}
setInterval(function(){if(running){doSomething()}},2000); ///this runs do something every 2 seconds
on button click simply set running = false;
Your code looks like:
set_time_limit(0);
while(true==true){//infinite loop
doSomething(); //your code
}
Let's upgrade it
set_time_limit(0);
session_start();
$_SESSION['do_a_loop'] = true;
function should_i_stop_loop(){
#session_start();
if( $_SESSION['do_a_loop'] == false ) {
//let's stop a loop
exit();
}
session_write_close();
}
while(true==true){
doSomething();
should_i_stop_loop(); //your new function
}
Create new file stopit.php
session_start();
$_SESSION['do_a_loop'] = false;
All you have to do now is create a request on stopit.php file (with ajax or something)
Edit code according to your needs, this is point. One of many solutions.
Sorry for my English
Sadly this isn't possible (sort of).
Each time you make an AJAX call to a PHP script the script spawns a new instance of itself. Thus anything you send to it will be sent to a new operation, not the operation you had previously started.
There are a number of workarounds.
Use readystate 3 in AJAX to create a non closing connection to the PHP script, however that isn't supported cross browser and probably won't work in IE (not sure about IE 10).
Look into socket programming in PHP, which allows you to create a script with one instance that you can connect to multiple times.
Have PHP check a third party. I.E have one script running in a loop checking a file or a database, then connect to another script to modify that file or database. The original script can be remotely controlled by what you write to the file/database.
Try another programming language (this is a silly option, but I'm a fan of node). Node.js does this sort of thing very very easily.
I want to update a page when my database is modified. I want to use jquery for doing this. Question not clear? Then have a look at this, Suppose this is my page:
<?php
$query=mysql_query("select * from tbl1 where user='admin'");
if(mysql_num_rows?($query)!=0)
{
echo 'Table 1 has values';
} else {
echo 'Table1 is empty';
}
?>
This action should be performed whenever any new entry is added to the database. Now suppose I add an entry to the database manually then the page should automatically show the result as "Table1 has values". I know it can be used by using refresh page periodically but I don't want to use it. Instead I want to try something other, like ajax polling? Can someone give me a demo?
You can use long polling, but do a lot of research first. Your server may kill the request that appears to be open for a long amount of time.
In PHP, your code will look something like...
set_time_limit(0);
while (TRUE) {
// Query database here
if ($results) {
echo json_encode($results);
exit;
}
sleep(1);
}
You can use Ajax jQuery Framework with Ajax:
http://www.w3schools.com/Ajax/default.asp
http://api.jquery.com/jQuery.ajax/
It will call the server side script Asynchronously and update your page accordingly. You can use jQuery to specify the format of the update also.
You are looking for Ajax-Push/Comet solutions. These aren't trivial.
You also mentioned ajax pooling.
Well, on the server side you need to loop until you have a timeout (that you defined yourself or the server did, make sure you return the HTTP status code for Timeout Occured) or the request can be satisfied.
And on the client side whenever you complete the operation successfully just handle it and than make the same ajax call again, if you timed out just make the same ajax request again until it's satisfied.
I am using flash to call a PHP page that needs to do a bit of processing. Is it possible to let PHP continue processing but show a response anyway so flash doesn't stall waiting?
My answer from here:
You can send Connection:Close headers,
which finishes the page for your user,
but enables you to execute things
"after page loads".
There is a simple way to ignore user
abort (see php manual too):
ignore_user_abort(true);
Use output control aka output buffering to do this. http://www.php.net/manual/en/function.ob-flush.php
You could try using flush()
As an example, try these two different pieces of code:
// without flush()
foreach ( range(1, 5) as $num ) {
echo "Beep $num<br>";
sleep(1);
}
// with flush()
foreach ( range(1, 5) as $num ) {
echo "Beep $num<br>";
flush();
sleep(1);
}
You can close the connection within a registered function within register_shutdown_function if you do not need to wait for the processing to be over to output content (i.e., if you do not need to output anything related to the outcome of the processing you wish to do).
See : http://www.php.net/manual/en/features.connection-handling.php#93441
The reason to put it in a register_shutdown_function is that even if the client aborts the connection, the processing will keep on going to the very end.