簡體   English   中英

使用PHP將表格數據保存為CSV格式

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

我從CSV文件中檢索數據,並使用PHP將其存儲在表中。 在此表中,我使用td中的文本框編輯文本數據。 現在,我想將此編輯后的數據存儲在CSV文件中,但是我無法將此編輯后的數據從該表存儲到CSV文件中。

這是我的代碼:

<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()
        {

//要在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);

這是我的表,在這里我寫文件。

        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>'; 
    ?>

在這里我可以編輯數據。...問題是我想將此編輯后的數據存儲在同一csv文件中。 但是無法存儲。

請給我一些代碼,以將此表數據存儲在CSV文件中。

    </body>
</html>

你為什么不使用fputcsv()

點擊這里 有關此的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM