PHP progress bar

WBOY
Release: 2016-07-30 13:29:44
Original
1280 people have browsed it

When making a progress bar in PHP, you can use the ob_flush(); flush(); two functions to refresh the cache and send the information in the cache to the browser. But be aware that many servers will accumulate about 4k of content before sending it, so you can force a refresh by filling in spaces (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>
Copy after login

The front-end is implemented using bootstrap’s progress bar:

<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>
Copy after login
<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>
Copy after login

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces the PHP progress bar, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template