PHP网页 - 在插入数据库之前预览CSV上传

PHP网页 - 在插入数据库之前预览CSV上传

问题描述:

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

我有一个当前的网页,使用HTMl和PHP上传文件来读取CSV并插入数据库。 这一切都运行正常,但我正在尝试找到一种相当快速,无痛的方式,在提交和插入数据库之前向用户显示数据。 现在我的提交按钮发布到PHP并立即执行VAR转储并插入到数据库中。 我希望一旦将文件添加到上传框中,就会有一种相当简化的方式在页面上显示它。 p>

  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]; 
 //其余数组元素和插入 声明
  code>  pre> 
  div>

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