PHP 进度条

WBOY
WBOYoriginal
2016-07-30 13:29:441302parcourir

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("</script></span>.(<span>$s</span>).<span>");"</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("</script></span>.<span>$resultStr</span>.<span>");"</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>div<span>class</span>=<span>"progress"</span>>
    div<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教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:微信自动回复代码Article suivant:pap Day1