duaner5714 2017-04-06 15:49
浏览 56
已采纳

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
  • 写回答

1条回答 默认 最新

  • dongzhidian3538 2017-04-06 16:05
    关注

    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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器