phpe.net上有一个进度条的类的

解决方案 »

  1.   

    楼主,你那个我从csv文件中取出来插入到mysql里去的程序能不能发给我一份。
      

  2.   

    to jxflll
    很简单的,给你个函数说明你就知道了
    fgetcsv
    (PHP 3>= 3.0.8, PHP 4 , PHP 5)fgetcsv -- 从文件指针中读入一行并解析 CSV 字段
    说明
    array fgetcsv ( int handle, int length [, string delimiter [, string enclosure]])
    和 fgets() 类似,只除了 fgetcsv() 解析读入的行并找出 CSV 格式的字段然后返回一个包含这些字段的数组。可选的第三个参数 delimiter 的默认值是逗号。可选参数 enclosure 的默认值是双引号。delimiter 和 enclosure 都被限制为一个字符。如果多于一个字符,则只使用第一个字符。 注: enclosure 参数是 PHP 4.3.0 新加的。 handle 必须是通过 fopen(),popen() 或者 fsockopen() 成功打开的有效文件指针。 length 必须大于 CVS 文件中长度最大的行(以便于处理行结束字符)。 fgetcsv() 出错时返回 FALSE,包括碰到文件结束时。 注: CSV 文件中的空行将被返回为一个包含有单个 null 字段的数组,不会被当成错误。 例子 1. 读取并显示 CSV 文件的整个内容<?php
    $row = 1;
    $handle = fopen ("test.csv","r");
    while ($data = fgetcsv ($handle, 1000, ",")) {
        $num = count ($data);
        print "<p> $num fields in line $row: <br>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            print $data[$c] . "<br>\n";
        }
    }
    fclose ($handle);
    ?>  
     
      

  3.   

    echo "<body >";
    echo "<br>";
    echo "<div id='pdiv' align='center' style='display:block'>";
    echo "<center>";
    echo "<table height='20'  border='0' cellpadding='0' cellspacing='0' width='260'>";
    echo "<tr>";
    echo "<td align='center' height='20' style='font-size:12px' COLSPAN='6'>正在处理信息,请等待...</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td align='left' valign='middle' width='200' height='20' style='background-repeat: repeat-x' background=''> <img  name='testimg' border='0' src='top1.gif' width='2' height='11'> </td>";
    echo "</tr>";
    echo "</table>";
    echo "</center>";
    echo "</div>";
    echo "</body>";
    echo "<script> pdiv.style.display='none' </script>";
    flush();
    set_time_limit(0);
    $max=2000;
    $width = 200;
    for($i=0;$i<$max;$i++)
    {
    $percent = ($i / $max) * 100;
    echo('<script>document.images.testimg.width = ' . ($percent / 100) * $width . '</script>');
    flush();
    }
    echo "<script> pdiv.style.display='none' </script>";
    flush();
      

  4.   

    magicsnake(北极狐) 的代码可行,但第一个echo "<script> pdiv.style.display='none' </script>";应去掉。
      

  5.   

    其实这个问题还有其他的解决方法。
    我们在处理这种问题的时候是采取的页面刷新的方法。
    一次插入数据可以少点,100条左右,然后用get参数控制
    比方 
    <meta http-equiv='refresh' content='0;url=insert.php?i=$i'>
    就不会超时,你也可以通过读$i来看进度。
      

  6.   

    $row = 1;
    $handle = fopen ("test.csv","r");require "connect.php";
    echo "<body >";
    echo "<br>";
    echo "<div id='pdiv' align='center' style='display:block'>";
    echo "<center>";
    echo "<table height='20'  border='0' cellpadding='0' cellspacing='0' width='260'>";
    echo "<tr>";
    echo "<td align='center' height='20' style='font-size:12px' COLSPAN='6'>正在处理信息,请等待...</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td align='left' valign='middle' width='200' height='20' style='background-repeat: repeat-x' background=''> <img  name='testimg' border='0' src='top1.gif' width='2' height='11'> </td>";
    echo "</tr>";
    echo "</table>";
    echo "</center>";
    echo "</div>";
    echo "</body>";
    flush();
    set_time_limit(0);
    $max=9000;
    $width = 200;
    while ($data = fgetcsv ($handle, 1000, ",")) {
        $num = count ($data);
        //print "<p> $num fields in line $row: <br>\n";
        
    $sql_insert="INSERT INTO `tbl_enterprise` (`fld_entername`, `fld_entercode`, `fld_enteraddress`, `fld_enterarea`, `fld_enterpostcode`, `fld_entertel`, `fld_entertype`, `fld_enterrelation`, `fld_enterindustry`, `fld_enterleader`, `fld_enterstreet`, `fld_enterbackup`) VALUES ('".$data[1]."', '".$data[0]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."', '".$data[7]."', '".$data[8]."', '".$data[9]."', '".$data[10]."', '".$data[11]."')";
    $res_insert=mysql_query($sql_insert) or die("Invalid query: " . mysql_error());;
    $percent = ($row / $max) * 100;
    echo('<script>document.images.testimg.width = ' . ($percent / 100) * $width . '</script>');
    flush();

    $row++;
    }
    echo "<script> pdiv.style.display='none' </script>";
    flush();
    fclose ($handle);
    echo "done";
    ?> 
    </tbody>
    </table>
    这个是我的代码,遇到奇怪的问题了,看数据库里9000条记录老早就插好了,可是进度条却慢慢的滚,怎么不是同步的吗?另外弱弱的问一下,flush()函数在这里起到什么作用啊?还有,我在试"北极狐"的代码的时候进度条也很慢,2000个循环不是应该一下子就好的吗?
    不明白了,请唠叨大哥指教!
      

  7.   

    这个是我的代码,遇到奇怪的问题了,看数据库里9000条记录老早就插好了,可是进度条却慢慢的滚,怎么不是同步的吗?
    *你在每插入一条记录时就控制进度条动一下,那么9000条记录就有9000句“<script>document.images.testimg.width =  ”
    应该要执行一段时间的。注意这并不是同步进行的!*另外弱弱的问一下,flush()函数在这里起到什么作用啊?
    将输出缓冲区的内容输出,而不等待程序结束*还有,我在试"北极狐"的代码的时候进度条也很慢,2000个循环不是应该一下子就好的吗?
    不明白了,请唠叨大哥指教!
    大约在2秒左右,如果远大于这个数就应该是你的浏览器问题了注意在本地测试和远程服务器上运行的效果是不一样的
      

  8.   

    <?php
    set_time_limit(0);
    $width = 500;   //显示的进度条长度,单位 px
    $total = 1000; //显示的步骤数,可以用数据库中实际取得的数组数代替
    $pix = $width / $total; //每条记录的操作所占的进度条单位长度
    $progress = 0;  //当前进度条长度
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
    <html>
    <head>
       <title>动态显示服务器运行程序的进度条</title>
       <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
       <style>
       body, div input { font-family: Tahoma; font-size: 9pt }
       </style>
       <script language="JavaScript">
       <!--
       function updateProgress(sMsg, iWidth)
       { 
           document.getElementById("status").innerHTML = sMsg;
           document.getElementById("progress").style.width = iWidth + "px";
           document.getElementById("percent").innerHTML = parseInt(iWidth / <?php echo $width; ?> * 100) + "%";
        }
       //-->
       </script>    
    </head><body>
    <div style="margin: 4px; padding: 8px; border: 1px solid gray; background: #EAEAEA; width: <?php echo $width+8; ?>px">
       <div><font color="gray">如下进度条的动态效果由服务器端 PHP 程序结合客户端 JavaScript 程序生成。</font></div>
       <div style="padding: 0; background-color: white; border: 1px solid navy; width: <?php echo $width; ?>px">
           <div id="progress" style="padding: 0; background-color: #FFCC66; border: 0; width: 0px; text-align: center;  height: 16px"></div>            
       </div>
       <div id="status">&nbsp;</div>
       <div id="percent" style="position: relative; top: -30px; text-align: center; font-weight: bold; font-size: 8pt">0%</div>
    </div>
    <?php
    flush();    //将输出发送给客户端浏览器
    for ($j=0;$j<$total;$j++){
       //  在此处使用空循环模拟较为耗时的操作,实际应用中需将其替换;
       for ($i = 0; $i < 100000; $i++) { 
           ;;
        }
    ?>
    <script language="JavaScript">
       updateProgress("正在操作第<?php echo $j; ?>条数据,请稍候....", <?php echo min($width, intval($progress)); ?>);
    </script>
    <?php
       flush();    //将输出发送给客户端浏览器,使其可以立即执行服务器端输出的 JavaScript 程序。
       $progress += $pix;    
    }   //end foreach
    //  最后将进度条设置成最大值 $width,同时显示操作完成
    ?>
    <script language="JavaScript">
       updateProgress("操作完成!", <?php echo $width; ?>);
    </script>
    <?php
    flush();
    ?>
    </body>
    </html>