簡體   English   中英

進度欄和導入數組mysql

[英]Progress bar and import array mysql

我有一個腳本,可從csv文件導入電話號碼。 驗證后,我使用以下腳本將所有結果放在php數組$data_rec[]上:

while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
{
$data_rec[]=''.$data[0].'';
}

之后,我想將所有結果插入到我的mysql dbb上,並插入以指示進度條完成的沖洗量。 我在互聯網上搜索了幾個小時,但找不到快速簡便的解決方案。

我要插入mysql dbb的腳本:

foreach($data_rec as $id => $number)
{
 $req=("INSERT INTO contact VALUES('".$idclient."','', '".$numero."','','','','','','','0')");  
$doins = mysql_query($req) or die(mysql_error()); 

}

我找到了jqueryprogressbar,但是我不明白如何使用腳本進行安裝。 你有想法嗎 ? 非常感謝你


謝謝您的幫助。 我閱讀了您的鏈接,發現(我認為)解決問題的方法。 但這仍然行不通。 我看到進度條,但是當所有結果都插入時(最后)。 有什么辦法可以幫助我嗎?

在我的頁面test.php上,我有一個ajax腳本。

<script type='text/javascript'>
$(document).ready(function() {
$("#import_2").click(function (event) {
$("#import_2").attr('class', 'vert');
$('div#display_import').empty();    // we empty the div to display the result (in case of old result)

        $.ajax({                                                            
            type: "POST",
            url: "testtest.php",
            data: "fonction=importok",
            success: function(msg){
            $('div#display_import').fadeIn();           // Fade in on the result div
            $("div#display_import").append(msg);        // display the result of the div
            }
            }); 
     });
 });

</script>

div上的一個按鈕開始導入:

<div style="margin:10px 10px 10px 10px;width: 150px; height: 20px;">
        <label id="import_2" style="width: 150px; height: 20px;padding-left: 10px; cursor: pointer;" class="">
          <span style="vertical-align: 0px; margin-right: 10px;"><img id="img_tous" src="images/icone-bouton-gris.png" width="10" height="10" style="margin-right: 5px;" /> <strong>IMPORT</strong></span>
          </label>
        </div>

<div id="display_import" class="texte_13">
</div>

在testtest.php上,我有要在mysql dbb上導入的php腳本。$ _SESSION ['listeok'] countain一個數組,包含所有結果(大約12000個“數字”)

if(isset($_POST['fonction']) and $_POST['fonction']=="importok")
{
// display progress bar
echo '<div id="progress" style="width:500px;border:1px solid #ccc;"></div>';
echo '<div id="information" style="width"></div>';
// Total processes
$data=$_SESSION['listeok'];
$total=10;
// Loop through process
for($i=1; $i<=$total; $i++)
{
    // Calculate the percentation
    $percent = intval($i/$total * 100)."%";

    // Javascript for updating the progress bar and information
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
    </script>';

    $req=("INSERT INTO contact VALUES('".$i."','', '".$data[$i]."','','','','','','','0')");    
    $doins = mysql_query($req) or die(mysql_error());

    // This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);


    // Send output to browser immediately
    flush();


    // Sleep one second so we can see the delay
    sleep(1);
}

// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"    </script>';


}

您好,這里是一般想法...

在循環中,您每隔100步將進度寫入一個臨時文件。

$i = 0;
foreach($data_rec as $id => $number)
{
   $req=("INSERT INTO contact 
      VALUES('".$idclient."','', '".$numero."','','','','','','','0')");  
      $doins = mysql_query($req) or die(mysql_error()); 
   if($i++ % 100 == 0){
     file_put_contents("anytempfile","$i of ".count($data_red));
   }

}

在html端,您只需每0.5秒通過ajax調用該文件的內容並顯示它。 現在,您無需太多開銷就可以觀看進度。

為什么要文件? 因為一個簡單的靜態文件將導致非常低的開銷(您無需初始化會話或任何與腳本相關的事情)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM