立即将csv文件中的数据显示为可手动保存

Display immediately data from csv file to handsontable

本文关键字:显示 保存 数据 csv 文件      更新时间:2023-09-26

我想在网页上的数组中显示*.csv文件中的数据。因此,当用户单击按钮时,我希望我的数据立即出现在我的数组中。

这就是我从*.csv:获取数据的方式

<?php 
        $file=file('Classeur1.csv');

        foreach ($file as $ligne)
        {
            list($index,$id_essai,$nom_local,$nombre_traitement,$nombre_blocs,$type_rotations,$essai_ou_suivi,$thematique,$annee_debut,$annee_fin)=explode(";",$ligne );
        }
    ?>

现在,当我点击按钮时,我调用一个javascript函数,如下所示:

<script>
$(document).ready(function(){
    $('#insert_data_csv').click(function(){
        $.post("import_data_csv.php",{array_data:list_data , array_handsontable:data_essai});
    });
});

在我的post方法中,我发送数组:一个来自csv,一个来自handontable。

这就是我如何申报我的列表_数据:

var index_essai = "<?php echo $index ;?>";
    id_essai = "<?php echo $id_essai;?>";
    nom_local_essai = "<?php echo $nom_local;?>";
    nombre_traitements = "<?php echo $nombre_traitement;?>";
    nombre_blocs = "<?php echo $nombre_blocs;?>";
    type_rotation =" <?php echo $type_rotations;?>";
    essais_ou_suivis =" <?php echo $essai_ou_suivi;?>";
    thematique = "<?php echo $thematique;?>";
    annee_debut = "<?php  echo $annee_debut;?>";
    annee_fin = <?php echo $annee_fin;?>;
list_data = [index_essai,
            id_essai,
            nom_local_essai,
            nombre_traitements,
            nombre_blocs,
            type_rotation,
            essais_ou_suivis,
            thematique,
            annee_debut,
            annee_fin
            ];

最终,我的ajax代码:

<?php
$array_data = $_POST['array_data'];
$array_handsontable = $_POST['array_handsontable'];
$i = 0;
foreach ($array_data as $ligne)
{
    $array_handsontable[$i][1] = $ligne;
    $i = $i+1;
    echo $ligne;
}
?>

我知道我的数据发送得很好,因为我可以从ajax文件在控制台中显示它们。但它们不会立即显示在我的手上。请帮帮我。

Insteed of Using explore使用下面的代码来分解记录并放入数组。

<?php
$row = 1;
if (($handle = fopen("Classeur1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>'n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />'n";
        }
    }
    fclose($handle);
}
?>