0

I have a script to import phone number from csv file. After verification, I put all the result on php array $data_rec[] with this script :

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

I would like after, to putt all the result on my mysql dbb with an insert to and indicate the pourcentage done with progress bar. I searched from couple of hours on internet, but I can't find fast and easy solution.

My script to insert into mysql dbb :

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

}

I found jqueryprogressbar but I can't understand how to install with my script. Do you have an idea ? Thank you very much


Thank you for your help. I readed your links and find (i think) a way for my problem. But it still doesn't work. I see the progress bar but when all the result was insert (at the end). An idea to Help me ?

On my page test.php I have an ajax script.

<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>

And a button on a div to start the import :

<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>

On testtest.php I have my php script to import on mysql dbb The $_SESSION['listeok'] countain an array with all my result (around 12 000 "number")

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>';


}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Sylvain
  • 95
  • 4
  • 16
  • http://stackoverflow.com/q/18566362/1296333 or http://stackoverflow.com/q/18735848/1296333 – agassner Feb 10 '14 at 15:29
  • thank you. For stackoverflow.com/q/18566362/1296333 when the result is very large (10 000 resultat) the script very slow. And if you change the sleep time from 1sec to 0.1, the browser bug. For the other solution, I can't find more information about installation of jqueryprogressbar...do you have a link with more information ? I'm not a professionnel of jquery :) – Sylvain Feb 10 '14 at 15:46

1 Answers1

0

Hello here is the general idea ...

in your loop you write your progress to a temporary file every 100 steps.

$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));
   }

}

On the html side you simply call via ajax every 0,5 seconds the content of that file and displays it. now you can watch your progress without much overhead.

Why a file? because a simple static file will cause very low overhead (you dont need to initialize session oder any script related things)

Benjamin Eckstein
  • 884
  • 2
  • 9
  • 19