PHP做進度條時,可以用 ob_flush(); flush();
兩個函數來刷新緩存,將緩存中的信息發送到瀏覽器。但要注意很多伺服器會累積滿4k左右的內容才會發送,所以可以透過填充空格( echo str_pad('', 4096);
)來強制刷新。
<code><span>public</span><span><span>function</span><span>insert</span><span>(<span>$data</span>,<span>$html</span>)</span> {</span><span>echo</span><span>$html</span>; <span>$i</span> = <span>0</span>; <span>$sum</span> = count(<span>$data</span>); <span>if</span>(<span>$sum</span> == <span>0</span>){ <span>echo</span><span>"数据出错"</span>; <span>return</span>; } <span>$noped</span> = <span>0</span>; <span>try</span> { <span>foreach</span>(<span>$data</span><span>as</span><span>$v</span>){ <span>$tdata</span>[] = <span>$v</span>; <span>$i</span>++; <span>if</span>( <span>$i</span>%<span>20</span>==<span>0</span> || <span>$i</span>==<span>$sum</span>-<span>1</span>){ <span>$ret</span> = <span>$this</span>->btrManger_m->inserts100(<span>$tdata</span>); <span>//一次插入100条</span><span>if</span>(<span>$ret</span>){ <span>$s</span> = <span>$i</span>/<span>$sum</span>*<span>100</span>; <span>echo</span> str_pad(<span>''</span>, <span>4096</span>); <span>//填充缓存</span><span>echo</span><span>"<script> setProgressBar("</span>.(<span>$s</span>).<span>");</script>"</span>; ob_flush(); flush(); } <span>else</span>{ <span>//出错 终止</span><span>break</span>; } <span>$tdata</span> = <span>array</span>(); } } } <span>catch</span> (<span>Exception</span><span>$e</span>) { <span>echo</span><span>$e</span>; <span>return</span>; } <span>$resultStr</span> = <span>"'共计"</span>.<span>$sum</span>.<span>"条记录,成功操作完成"</span>.<span>$i</span>.<span>"条!'"</span>; <span>echo</span><span>"<script> setText("</span>.<span>$resultStr</span>.<span>");</script>"</span>; }</code>
前端用bootstrap的進度條實現:
<code><span><span>function</span><span>setProgressBar</span><span>(width)</span>{</span> str=width+<span>"%"</span>; $(<span>'.progress-bar'</span>).attr(<span>'aria-valuenow'</span>,str).css(<span>'width'</span>,str) .html(str); <span>if</span>(width == <span>100</span>){ $(<span>'#idInsertResult'</span>).html(<span>"数据操作完成!"</span>); } }</code>
<code><<span>div</span><span>class</span>=<span>"progress"</span>> <<span>div</span><span>class</span>=<span>"progress-bar progress-bar-info progress-bar-striped active"</span> role=<span>"progressbar"</span> aria-valuenow=<span>"0"</span> aria-valuemin=<span>"1"</span> aria-valuemax=<span>"100"</span> style=<span>"width: 3%;"</span>> <span>0</span>% </<span>div</span>> </<span>div</span>></code>
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
以上就介紹了PHP 進度條,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。