des3rtfoxie Posted December 21, 2009 Share Posted December 21, 2009 Hi all, I've just registered on PHPFreaks because I've got a question that I simply can't work out by myself. Im developing a web based reporting application, based upon PHP/MySQL. At some point the user can chose some parameters from a form like dates etc and hit a button, then the form data is submitted to execute_report.php, which contains some code as follos: <html> Some html stuff here, like text output for the user, that the query xyz has been started <?php Here goes the first query ?> Then html code again to insert a line that the query has finished Another line of text that the next query has started <?php And the next MySQL query ?> Text again that the query has finished ... and so on </html> Basically everything works like a charm. The queries do what I want and deliver the output and results just fine. What bugs me is, that all results and the html text output are only displayed after the last query has finished. During the whole executing time, the browser just says "Loading" until the queries have executed, then all results are displayed the way I intended them to. I already tried experimenting with putting the queries into functions and also with sleep commands, but to no avail. I would love to give the user a feedback before a query starts and after it has finished, sort of in "Real time". Any tips would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Catfish Posted December 21, 2009 Share Posted December 21, 2009 No idea how to do this one. I am actually interested to know how you would do this too! Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 21, 2009 Share Posted December 21, 2009 If you want real-time reporting of each query you would need to use some pretty complicated AJAX. And call a script with each query one after the other, and get ajax to update the page each time. AJAX is actually pretty simple once u know how to use it. -CB- Quote Link to comment Share on other sites More sharing options...
des3rtfoxie Posted December 21, 2009 Author Share Posted December 21, 2009 The term "Real time" might be misleading. All I would like my php code to do, is do one thing after another. 1) Execute a query 2) Give feedback to user that it the query has finished ( I don't want to display results here ! ) 3) Go on with the next query 4) Provide feedback 5) And so on What I don't grasp is, that - even when I put the user feedback as "echo blah blah" into the php code and not in the html code - that php always seems to execute all MySQL queries first before it echoes any output. I would expect the server to parse my code line by line if you know what I mean : ) Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 21, 2009 Share Posted December 21, 2009 PHP is a pre-processor. That means output doesnt get forwarded to apache until the process has finished (script closure). Unfortunately you cannot change this without elaborate tcp calls etc. (that i know of). To do what your asking, you will need to setup an ajax script, so that when the user clicks submit, the first query gets sent to the special script by javascript (without loading another page), then it would wait for a response (that you can display to the user), and then it can do it again, and again, until the last query. This is relatively simple if you have any experience with javascript. Just check out some ajax tutorials - you'll get it . -CB- Quote Link to comment Share on other sites More sharing options...
optikalefx Posted December 21, 2009 Share Posted December 21, 2009 Better yet!!! Check out mySQL triggers. they are perfect for this. I made a video on how to use them BEFORE a query, but they work the same for AFTER one too. Quote Link to comment Share on other sites More sharing options...
des3rtfoxie Posted December 21, 2009 Author Share Posted December 21, 2009 Tanks for the replies guys. Using AJAX would indeed solve what I was looking for .. BUT .. I started building the application in Java and completely revamped it recently to run on PHP / MySQL only, due to performance issues, so I'll have to see .. I think I'll look more into the Ajax thing none the less when I'm done with the rest of my app. Thanks again ! Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 21, 2009 Share Posted December 21, 2009 All you need is an if clause in ur script to tell wether its ajax calling it, and you can handle the queries easy . Would take a little time if it's your first go but really is more simple than it sounds . And since u obviously know javascript (having porting your code from it), you should take it on easy . Just follow a quick tutorial and you'l get the hang of it in like 1 day (thats how long it took me - without knowing java). -CB- Good Luck Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 24, 2009 Share Posted December 24, 2009 This is one very tempremental solution: (Since a number of things can prevent it's intended function); Upon browsing the php manual i came across a function called "flush()". This nifty little function will output to the browser immediately (though there are specific buffering restrictions on specific versions of specific browsers). There are a few things that will stop it from working properly, i suggest you take a long look at http://www.php.net/flush Good Luck Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.