简体   繁体   English

通过PHP从动态表数据导出为CSV

[英]export to CSV from dynamic table data via PHP

Let's say I have a dynamic table in php page.... is there a way I can export what I have from the dynamic table to a CSV file (and text file [if possible]) via PHP... 假设我在php页面中有一个动态表...。有没有一种方法可以通过PHP将动态表中的内容导出到CSV文件(和文本文件,如果可能)。

Here I am getting data from table and show it in alert box. 在这里,我从表中获取数据并将其显示在警报框中。 But I want to save it in a file. 但我想将其保存在文件中。 Using php. 使用PHP。 Here is my code: 这是我的代码:

<html>
    <head>
    <script>
        function GetCellValues()
        {
            var str = '';
            var rows = document.getElementsByTagName('tr');
            var table=document.getElementById("project");
            for (var i=0;i<table.rows[0].cells.length;i++)
            {
                if (i > 2 )
                {
                    str = str + table.rows[0].cells[3].innerHTML.replace(", ");
                }
                else
                {
                    str = str + (table.rows[0].cells[i].innerHTML) + ', ' ;
                }
            }
        for (var c = 1 ; c < rows.length ; c++)
        {
            str += '\n' + "0" + c + ', ';
            var row = rows[c];
            var inputs = row.getElementsByTagName('input');                
            for (var k = 0 ; k < inputs.length ; k++)
            if (k > 1)
            {
                str += inputs[k].value.replace(", ");
            }
            else 
            {
                str += inputs[k].value + ', ';
            }
        }   
        document.getElementById('hiden').value = str;
        alert(document.getElementById('hiden').value);
    }
    </script>       
    </head>
    <body>
    <form>
        <br>
        <h1><u>PROJECT</u> :</h1>
        <br>
            <input type = "hidden" id = "hiden" name = "hiden" value = "">
            <input type = "button" name = "submit" onclick = "GetCellValues()" Value = "SAVE">          
        <br>
    </form>
    <?php
        $handle = fopen("data.csv", "w");
        $hide = $_REQUEST['hide'];
        fwrite($handle,$hide);
        $file = file('data.csv');
        $lines = count($file);

        echo'<table id = "project" border = "1" cellpadding="2" cellspacing="0"
        style = "width: 60%; margin-left: auto; margin-right: auto; border-color: brown; background-color:silver;">';
        echo'<tr cellpadding="100">
            <th width="15%">Sl.No.</th>
            <th width="15%">Project Name</th>
            <th width="15%">ChangeDate</th>
            <th width="15%">Changed By</th>
        </tr>';
        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>'; 
    ?>
    </body>
</html>

you could use the built in PHP5 function fputcsv 您可以使用内置的PHP5函数fputcsv

here is a sample code 这是示例代码

$result = mysqli_query($con, 'SELECT * FROM table_name');
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

$fp = fopen('file.csv', 'w');

foreach ($row as $val) {
    fputcsv($fp, $val);
}

fclose($fp);

It will return a comma separated string for each row written to file.csv: 对于写入file.csv的每一行,它将返回一个逗号分隔的字符串:

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

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