如何在网页上显示CSV信息?

时间:2015-01-30 05:55:53

标签: php csv

我这里有这个代码,允许上传并覆盖数据库。 但我不知道如何让它在我的网页上显示CSV详细信息。 有人可以帮忙吗?

 <?php
 session_start();
if (isset($_POST["Upload"])) {
include "dbFunctions.php";
mysql_select_db($DB) or die(mysql_error());
$filename = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
    $file = fopen($filename, "r");

    $count = 0;
    do {
        $excelData = fgetcsv($file, 10000, ",");

        if ($excelData === FALSE && $count == 0) {
            break;
        } elseif ($excelData !== FALSE) {
            if (count($excelData) < 8) {
                break;
            }
            $count++;
            $sql = "INSERT into profile (id, password, name, dob, contact, email, gender, module, school) values ('" . $excelData[0] . "',SHA1('" . $excelData[1] . "'),'" . $excelData[2] . "','" . $excelData[3] . "','" . $excelData[4] . "','" . $excelData[5] . "','" . $excelData[6] . "','" . $excelData[7] . "','" . $excelData[8] . "') ON DUPLICATE KEY UPDATE password=SHA1('" . $excelData[1] . "'), name='$excelData[2]', dob='$excelData[3]', contact='$excelData[4]', email='$excelData[5]', gender='$excelData[6]', module='$excelData[7]', school='$excelData[8]'";


            mysql_query($sql);
        }
    } while ($excelData !== FALSE);
    if ($count > 0) {
        $msg = 'CSV File has been successfully imported!';
    } else {
        $msg = 'Invalid File. Please check that the file is uploaded in .CSV format!';
    }
//        echo $msg;
}
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="stylesheet/style.css" rel="stylesheet"/>
</head>
<body>

    <h2>Load Details</h2>
    <br>
    <?php
    echo $msg
    ?>
    <br>

    <table align='center' border='3' cellpadding='5' cellspacing='0'>
        <tr>
            <th>ID </th>
            <th>Password</th>
            .....

        </tr>
    </table>

    <?php
    echo "<tr>";
    echo "<td>" "</td>";
    echo "</tr>";
    ?>

</body>

把什么放在桌子里?

2 个答案:

答案 0 :(得分:1)

尝试这种编码..

<?php
 echo "<html><body><table style='border: 1px solid black;'>\n\n";
 $f = fopen("so-csv.csv", "r");
 while (($line = fgetcsv($f)) !== false) {
    echo "<tr>";
    foreach ($line as $cell) {
            echo "<td style='border: 1px solid black;'>" . htmlspecialchars($cell) . "</td>";
    }
    echo "</tr>\n";
  }
  fclose($f);
    echo "\n</table></body></html>";

答案 1 :(得分:0)

找到以下代码,通过成功解析显示已处理CSV文件的表结构,

<?php
session_start();
if (isset($_POST["Upload"])) {
    include "dbFunctions.php";
    mysql_select_db($DB) or die(mysql_error());
    $filename = $_FILES["file"]["tmp_name"];
    if ($_FILES["file"]["size"] > 0) {
        $file = fopen($filename, "r");

        $count = 0;
        do {
            $excelData = fgetcsv($file, 10000, ",");

            if ($excelData === FALSE && $count == 0) {
                break;
            } elseif ($excelData !== FALSE) {
                if (count($excelData) < 8) {
                    break;
                }
                $count++;
                $sql = "INSERT into profile (id, password, name, dob, contact, email, gender, module, school) values ('" . $excelData[0] . "',SHA1('" . $excelData[1] . "'),'" . $excelData[2] . "','" . $excelData[3] . "','" . $excelData[4] . "','" . $excelData[5] . "','" . $excelData[6] . "','" . $excelData[7] . "','" . $excelData[8] . "') ON DUPLICATE KEY UPDATE password=SHA1('" . $excelData[1] . "'), name='$excelData[2]', dob='$excelData[3]', contact='$excelData[4]', email='$excelData[5]', gender='$excelData[6]', module='$excelData[7]', school='$excelData[8]'";


                mysql_query($sql);
            }
        } while ($excelData !== FALSE);
        if ($count > 0) {
            $msg = 'CSV File has been successfully imported!';
        } else {
            $msg = 'Invalid File. Please check that the file is uploaded in .CSV format!';
        }
        //        echo $msg;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="stylesheet/style.css" rel="stylesheet"/>
</head>
<body>

    <h2>Load Details</h2>
    <br>
    <?php
    echo $msg
    ?>
    <br>

    <?php 

    if(strpos($msg,'success') !== false){
?>
      <table align='center' border='3' cellpadding='5' cellspacing='0'>
        <tr>
            <th>ID </th>
            <th>Password</th>
            .....

        </tr>
<?php 

    $dataList = fopen($filename, "r");

    $loopCount = 0;
        while($dataRow = fgetcsv($dataList, 10000, ",")){


                if (count($dataRow) < 8) {
                    continue;
                }
                $loopCount++;
                ?>
            <tr>
                <td><?php echo $dataRow[0]; ?> </td>
                <td><?php echo $dataRow[1]; ?> </td>
                .....

            </tr>
                <?php 
            }
        }
    ?>

    </table>



</body>
</html>