<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Joey Rivera &#187; feedback</title>
	<atom:link href="http://www.joeyrivera.com/tag/feedback/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joeyrivera.com</link>
	<description>Blogging about PHP, Actionscript, MySQL, and other interests.</description>
	<lastBuildDate>Fri, 02 Dec 2011 03:55:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>ob_start(), ob_flush(), flush(), set_time_limit() &#8211; Give user feedback during execution</title>
		<link>http://www.joeyrivera.com/2008/ob_start-ob_flush-flush-set_time_limit-give-user-feedback-during-execution/</link>
		<comments>http://www.joeyrivera.com/2008/ob_start-ob_flush-flush-set_time_limit-give-user-feedback-during-execution/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 19:58:46 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[feedback]]></category>
		<category><![CDATA[flush()]]></category>
		<category><![CDATA[ob_flush()]]></category>
		<category><![CDATA[ob_start()]]></category>
		<category><![CDATA[set_time_limit()]]></category>
		<category><![CDATA[sleep()]]></category>
		<category><![CDATA[usleep()]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=238</guid>
		<description><![CDATA[So every once in a while I want/need to write some code that may take some time to execute. The problem is because it takes so long to execute, I need to make sure to give the user some feedback during this process so they know the page is working. If I hit submit in [...]]]></description>
			<content:encoded><![CDATA[<p>So every once in a while I want/need to write some code that may take some time to execute. The problem is because it takes so long to execute, I need to make sure to give the user some feedback during this process so they know the page is working. If I hit submit in a page and nothing changes in my screen after 20-30 seconds I assume something is wrong. For example, I wrote a php page that would query multiple users in one db, then for each user load their xml data from another db and parse out the information to return a report for all users in the date range. Depending on the date range, this could take a few minutes to complete. This is where the php output buffer comes into play.<span id="more-238"></span></p>
<p>The idea is you start the output buffer with the php <a title="ob_start() php function page" href="http://us3.php.net/ob_start" target="_blank">ob_start()</a> function,  print/echo stuff to the browser (but it won&#8217;t be displayed since it&#8217;ll be kept in the buffer), finally flush the buffer so it send it&#8217;s contents to the browser. You repeat this each time you want something displayed in the browser. Here&#8217;s an example, it&#8217;s very similar to the examples you can find the the php.net manual covering output buffers.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
 <br />
<span class="co1">// start output buffer</span><br />
<span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/ob_get_level"><span class="kw3">ob_get_level</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> == <span class="nu0">0</span><span class="br0">&#41;</span> <a href="http://www.php.net/ob_start"><span class="kw3">ob_start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span>=<span class="nu0">0</span>;<span class="re0">$i</span>&lt;<span class="nu0">70</span>;<span class="re0">$i</span>++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
    <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;printing&#8230;&lt;br /&gt;&#8217;</span>;<br />
    <a href="http://www.php.net/print"><span class="kw3">print</span></a> <a href="http://www.php.net/str_pad"><span class="kw3">str_pad</span></a><span class="br0">&#40;</span><span class="st0">&#8221;</span>,<span class="nu0">4096</span><span class="br0">&#41;</span>.<span class="st0">&quot;<span class="es0">\n</span>&quot;</span>;<br />
    <br />
    <a href="http://www.php.net/ob_flush"><span class="kw3">ob_flush</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
    <a href="http://www.php.net/flush"><span class="kw3">flush</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
    <a href="http://www.php.net/usleep"><span class="kw3">usleep</span></a><span class="br0">&#40;</span><span class="nu0">30000</span><span class="br0">&#41;</span>;</p>
<p>    <span class="co1">// depending on what you are doing, you may or may not need this</span><br />
    <a href="http://www.php.net/set_time_limit"><span class="kw3">set_time_limit</span></a><span class="br0">&#40;</span><span class="nu0">30</span><span class="br0">&#41;</span>; </p>
<p><span class="br0">&#125;</span><br />
 <br />
<span class="kw2">?&gt;</span></div>
<p>First you start the buffer, then loop 70 times and echo a str. I had to add the <a title="str_pad() manual on php.net" href="http://us3.php.net/str_pad" target="_blank">str_pad()</a> part to make this work on Google Chrome and I won&#8217;t try to act like I can explain why&#8230; I probably read it somewhere :p. Then we do <a title="ob_flush() at php.net" href="http://us2.php.net/manual/en/function.ob-flush.php" target="_blank">ob_flush()</a> and <a title="flush() at php.net" href="http://us2.php.net/manual/en/function.flush.php" target="_blank">flush()</a>, both are needed for this to work. For this example a <a title="usleep() php.net manual" href="http://us2.php.net/usleep">usleep()</a> call was added to make each page take a little bit longer to execute so we can see this example work. Finally, not needed for this example, but was needed for my report since it took so long to execute &#8211; <a title="php.net set_time_limit()" href="http://us2.php.net/set_time_limit" target="_blank">set_time_limit()</a>. I believe the php&#8217;s default time limit for executing a page is 30 seconds, anything over that and you will get a php error saying your script is taking too long and has been stopped. To solve this, I reset my timer to 30 seconds on every loop since each call shouldn&#8217;t take more than 30 seconds. You can set the time limit to never expire but that could be bad and I wouldn&#8217;t recommended.</p>
<p>That&#8217;s all the code you need to make this example work. Depending on what you are working on: start the buffer, then just print/echo whatever needed and flush to display on the screen then continue to the next loop/section. If you have any questions, comments, better ways of doing this, or improvements please feel free to comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2008/ob_start-ob_flush-flush-set_time_limit-give-user-feedback-during-execution/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

