简体   繁体   中英

Read txt file and insert values into database using PHP and Ajax

I have a text file that contains values with line breaks, like this

Lawrence
Barse
Sifron
etc
etc

I want to read this file and insert each value into a Database Table. I want the Front End Page to display information on each value that's inserted, like

Value Lawrence inserted successfully

So, I've written my PHP code like this

$count = 0;
$fileName = "names.txt";
    $fhandler = fopen($fileName, 'r');
    $theData = fread($fhandler, filesize($fileName));
    fclose($fhandler);

    $myArray = explode("\n", $theData);

So the variable $myArray contains all the values from the text file,

next I've written a Javascript function to send value from $myArray[0], to my data insertion php script page, which is something like this

<script >
    var cnt;
    function init()
    {
        var str = "<?php echo $myArray[$count] ?>";
        alert("Str is "+str);
    }
    function writeData(data)
        {
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("my_content").innerHTML += xmlhttp.responseText;
                    repeat();
                }
              }
            $data = ""+data+"";
            xmlhttp.open("GET","returnVals.php?b="+$data,true);
            xmlhttp.send();
        }
    function repeat()
    {
        <?php echo $count++; ?>
        cnt = "<?php echo $count ?>";
        var val = "<?php echo $myArray[$count] ?>";
        alert("Count is "+cnt+" and Val is "+val);
        writeData(val);
    }
</script>

My returnVals.php is the script that handle all the data insertion and echo's and output, that I print in the response handler.

Also, I know that I'm echoing php values inside javascript, which is not the standard way of programming, but I just don't see any other alternative. My Javascript repeat() function re calls the writeData function based on the $count value, but I'm not able to increment this value.

Is there a better way to achieve this...

thanks in advance!!!

I've run across a few times. I've developed a non-AJAX pattern for long running import processes which exceed PHP timeouts before they complete. One day I might upgrade it to AJAX but this seems pretty straightforward to me. Hopefully it is of some help to you.

PATTERN

  • jump to the new starting point
  • output fixed number of new records
  • write out continue link
  • auto reload (optional) to the continue link

CODE

<?php
// variables
$num_rows = 25;  // number of rows to handle per page view

// get the starting index
$start_at  = (isset($_REQUEST['start'])) ? $_REQUEST['start'] : 0;
$new_start = $start_at + $num_rows;

// skip ahead to $start_at
$in_file   = fopen(...);
for ($i = 0; $i < $start_at; $i++) {
    // skip over this many rows
    fgets($in_file);
}

// process num_rows
for ($i = 0; $i < $num_rows; $i++) {
    $line = fgets($in_file);

    // process the line

    $morework = !!$line;  // fgets returns FALSE when done
}

// write out continue link with auto reload trigger
if ($morework) {
    echo "<a id='clickme' href='/my/page?start={$new_start}'>Continue from {$new_start}</a";
    echo "<script>";
        echo "window.location = document.getElementById('clickme').href";
    echo "</script>";
} else {
    echo "DONE";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM