简体   繁体   English

使用PHP将表格数据保存为CSV格式

[英]Save the table data in CSV format using PHP

I retrieve the data from CSV file and store it in table using PHP. 我从CSV文件中检索数据,并使用PHP将其存储在表中。 In this table I am using textboxes within td where I can edit the table data. 在此表中,我使用td中的文本框编辑文本数据。 Now I want to store this edited data in the CSV file, but I am not able to store this edited data from this table to CSV file. 现在,我想将此编辑后的数据存储在CSV文件中,但是我无法将此编辑后的数据从该表存储到CSV文件中。

Here is my code: 这是我的代码:

<html>
    <head>
    </head>
    <body>
    <div>
        <br>
        <h1 style="text-align: center">* * * PROJECTS * * *</h1>
        <br>
            <input type = "hidden" id = "hide" name = "hide" value = "">
            <input type = "button" name = "submit" onclick = "document.write('<?php echoAlert() ?>');" Value = "SAVE">
        <br>
    </div>

    <?php
        function echoAlert()
        {

// What do I have to write here to store the data in CSV file? //要在CSV文件中存储数据,我必须在这里写些什么?

    }
    ?>

    <?php
        $handle = fopen("data.csv", "w"); // this is my .csv file.
        $hide = $_REQUEST['hide'];    // here I will retrieve the data from data.csv file
        fwrite($handle,$hide);    // Here I will write the data from file to table

        $file = file('data.csv');
        $lines = count($file);

This is my table, here I write the file. 这是我的表,在这里我写文件。

        echo'<table id = "projest" border = "1" cellpadding="2" cellspacing="0"
        style = "width: 60%; margin-left: auto; margin-right: auto; border-color: brown; background-color:gray;">';

        for ($i=1; $i<$lines; $i++) 
        {
            $part = explode(',', $file[$i]);
            echo'<tr>
                <td align= "center" width="5%">'.$part[0].'</td>
                <td align= "center" width="25%"><input type="text" value='.$part[1].'></td>
                <td align= "center" width="25%"><input type="text" value='.$part[2].'></td>
                <td align= "center" width="25%"><input type="text" value='.$part[3].'></td> 
            </tr>';
        }
        echo'</table>'; 
    ?>

Here I can edit the data.... The problem is I want to store this edited data in this same csv file. 在这里我可以编辑数据。...问题是我想将此编辑后的数据存储在同一csv文件中。 But not able to store it. 但是无法存储。

Please give me some code to store this table data in a CSV file. 请给我一些代码,以将此表数据存储在CSV文件中。

    </body>
</html>

Why dont you use fputcsv() 你为什么不使用fputcsv()

Click Here . 点击这里 For more information about this 有关此的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM