简体   繁体   中英

PHP web page - Preview CSV upload before inserting into database

I have a current web page using file upload with HTMl and PHP to read the CSV and insert into the database. This all works fine but I'm trying to find a fairly quick and painless way of showing the data to the user before submitting and inserting to the database. Right now my submit button posts to the PHP and immediately does a VAR dump and inserts to the DB. I'm hoping there's a fairly streamlined way to show it on the page as soon as the file is added to the upload box.

if(isset($_POST['submit']))
{
ini_set('auto_detect_line_endings', true);

$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");

while(!feof($handle)){
$filesop = print_r(fgetcsv($handle, 0, ","));
}


$coldata = array();

$coldata[ "orderNumber" ] = $filesop[0];
$coldata[ "place" ] = $filesop[1];
$coldata[ "workOrderNum" ] = $filesop[2];
$coldata["lowSideMIUNum"] = $filesop[3];
//rest of array elements and insert statement

Output it as an html table

$maxPreviewRows = PHP_INT_MAX; // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;

echo '<table>';

if ($hasHeaderRow) {
    $headerRow = fgetcsv($handle);
    echo '<thead><tr>';
    foreach($headerRow as $value) {
        echo "<th>$value</th>";
    }
    echo '</tr></thead>';
}

echo '<tbody>';

$rowCount = 0;
while ($row = fgetcsv($handle)) {
    echo '<tr>';
    foreach($row as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';

    if (++$rowCount > $maxPreviewRows) {
        break;
    }
}
echo '</tbody></table>';

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