0

みなさん、良い一日を、

正直、行き詰まっています…ご協力をお願いします。私が直面している課題は次のようになります。1。CSVファイルをサーバーにアップロードします。2.CSVファイルを開きます。3.解析し、データを表示して、csvファイルの各レコードのSQLクエリを作成します。

ここで、私の問題はステップ3です。データを解析してテーブルに表示するところまで到達しましたが、クエリの作成に失敗しました。ここでの助けは大歓迎です!

これは、csvファイルを解析してデータを表示するために使用しているコードです。ファイルの各レコードに対してSQLクエリを作成して実行するために、コードをどのように変更する必要があるかについて、ご協力をお願いしたいと思います。

if ( $file = fopen( "upload/" . $storagename , r ) ) {

            echo "File opened.<br />";

            $firstline = fgets ($file, 4096 );
            //Gets the number of fields, in CSV-files the names of the fields are mostly given in the first line
            $num = strlen($firstline) - strlen(str_replace(";", "", $firstline));

            //save the different fields of the firstline in an array called fields
            $fields = array();
            $fields = explode( ";", $firstline, ($num+1) );

            $line = array();
            $i = 0;

            //CSV: one line is one record and the cells/fields are seperated by ";"
            //so $dsatz is an two dimensional array saving the records like this: $dsatz[number of record][number of cell]
            while ( $line[$i] = fgets ($file, 4096) ) {

                $dsatz[$i] = array();
                $dsatz[$i] = explode( ";", $line[$i], ($num+1) );

                $i++;
            }

            echo "<table>";
            echo "<tr>";
            for ( $k = 0; $k != ($num+1); $k++ ) {
                echo "<td>" . $fields[$k] . "</td>";
            }
            echo "</tr>";

            foreach ($dsatz as $key => $number) {
                //new table row for every record
                echo "Record <tr>";
                foreach ($number as $k => $content) {
                    //new table cell for every field of the record
                    echo "<td>" . $content . " </td>";
                }
            }

            echo "</table>";
        }

お時間をいただきありがとうございます。

4

1 に答える 1

0

これを試して:

        foreach ($dsatz as $key => $number) {

    // New code here -lighthart
            echo "insert into table ("
                 .implode(",", $fields)
                 .") values ("
                 .implode(",",$number).");"


            //new table row for every record
            foreach ($number as $k => $content) {
                //new table cell for every field of the record
                echo "<td>" . $content . " </td>";
            }
        }
于 2013-02-21T22:31:55.323 に答える