{"id":511,"date":"2012-03-21T10:05:06","date_gmt":"2012-03-21T08:05:06","guid":{"rendered":"https:\/\/polyetilen.lt\/?p=511"},"modified":"2023-05-02T09:30:13","modified_gmt":"2023-05-02T06:30:13","slug":"display-data-from-long-running-php-script-with-jquery","status":"publish","type":"post","link":"https:\/\/polyetilen.lt\/en\/display-data-from-long-running-php-script-with-jquery","title":{"rendered":"Display data from long running PHP script with jQuery"},"content":{"rendered":"<p>When a PHP script is executed for a long time and it is not clear what it is happening, such as a long loop, a prolonged processing, rainy weather, or other magical things. That is, the desire to see what is happening with the data and at what stage of implementation is a script program. One of the options to use function flush() which clears PHP output buffer and all its contents are sent to the user's browser.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?\r\nheader('Content-Type: text\/plain; charset=utf-8');\r\n\/\/unlimited script run time\r\nset_time_limit(0);\r\n\/\/loop\r\nfor($i=0;$i&lt;1000;$i++){\r\n\t\/\/data output\r\n\techo &quot;\\nloop: $i progress: &quot;.round($i\/10).&quot;%&quot;;\r\n\t\/\/clear and send PHP output buffer\r\n\tflush();\r\n\t\/\/pause for clarity\r\n\tusleep(100000);\r\n}\r\n<\/pre>\n<p>Another option is to use a script to write data to a separate file and load this file from time to time in the browser. You will need to write data file from the script:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?\r\nset_time_limit(0);\r\nfor($i=0;$i&lt;1000;$i++){\r\n\t\/\/writing data to the file log.txt, you need to set file write permissions\r\n\tfile_put_contents('log.txt', &quot;loop: $i&lt;br&gt; progress: &quot;.round($i\/10).&quot;%&quot;);\r\n\t\/\/pause for clarity\r\n\tusleep(100000);\r\n}\r\n<\/pre>\n<p>Further data file will load jQuery load function:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE HTML&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;UTF-8&quot; \/&gt;\r\n&lt;title&gt;Progress&lt;\/title&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script language=&quot;javascript&quot; type=&quot;text\/javascript&quot;&gt;\r\n$(document).ready(function() {\r\nvar reload = setInterval(\r\n\tfunction(){\r\n\t\t\/\/load data from file and display in element id=&quot;progress&quot;\r\n\t\t$('#progress').load('log.txt');\r\n\t}, 1000); \/\/every second\r\n});\t\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\t&lt;div id=&quot;progress&quot;&gt;&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When a PHP script is executed for a long time and it is not clear what it is happening, such as a long loop, a prolonged processing, rainy weather, or other magical things. That is, &hellip; <a href=\"https:\/\/polyetilen.lt\/en\/display-data-from-long-running-php-script-with-jquery\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"http:\/\/polyetilen.lt\/?p=269","footnotes":""},"categories":[8],"tags":[101,103,102,53,34],"class_list":["post-511","post","type-post","status-publish","format-standard","hentry","category-programavimas","tag-ajax","tag-duomenys","tag-flush","tag-jquery","tag-php","en-US"],"_links":{"self":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/comments?post=511"}],"version-history":[{"count":1,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/511\/revisions"}],"predecessor-version":[{"id":512,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/511\/revisions\/512"}],"wp:attachment":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/media?parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/categories?post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/tags?post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}