• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

通过PHP按钮表导出为CSV

用户头像
it1352
帮助1

问题说明

I am really new to php and everything I have learned is from my school textbook and online research. With that said I am trying to complete an assignment and I am stuck on the last part. for the final part the assignment says to "Create a PHP script that will dump the contents of the employee table into CSV text file that has comma separated values for each record. Each new record should begin on a new line". I have tried many online tutorials but none of them teach how to put this event in a button. I am including my code so you can see the mess I have. It's not to bad but I am sure I can do the same task with much less code. Again I am just starting out and this is the best I can do for now. Could anyone give any suggestions on how this could be done. I have my button on line 140. I also used a db_connect() function so i don't have to write it many times. I can peon use this to read the database before I save as a csv.

任何建议都将不胜感激。请注意,需要遵循大量代码。

Any suggestions would be greatly appreciated. Be warned It's a lot of code to follow.

    <h2>Employee Search</h2>
    <form action="Draft.php" name="dbToCSV" method="GET">
        <input type="submit" name="dbToCSV" value="Export Database to CSV" <?php if     (isset($_GET['dbToCSV']))
                    {
                        //Do this code
                    }
                    else
                    {
                        echo "Error!";
                    } ?>><br />

    </form>
    <form action="Draft.php" method="GET">
        By name: <input type="text" name="searchName">
        <input type="submit" value="Search Name"><br />
    </form>
    <form action="Draft.php" method="GET">
        By language: <input type="text" name="searchLang">
        <input type="submit" value="Search Language"><br />
    </form>

正确答案

#1

因此,课程结束了,我想我会证明我如何解决我的问题。这对我来说最有意义,不需要很多代码。我相信可能有更短的方法来做到这一点,但是对于php新手来说,我在代码方面做得很好。

So the class is over and I figure I would show how I resolved my issue. this made the most sense to me and it didn't take many lines of code. I am sure there are possibly even shorter ways to do this but for being very new to php i thing I did an ok job with the code.

我基本上做了一个if该语句检查数据库中是否有任何记录,然后接受每个记录字段,并在添加逗号的同时将其写入cdv文件。我做了两次此代码。最上面的代码写数据库字段名,后半部分写这些字段中的值。谢谢大家的建议。我很高兴能够及时解决问题并提交作业。

I basically made an if statement that checks if there are any records in the database then it takes each record field and writes it to the cdv file while adding a comma. I did this code twice. The top code writes the database field names and the second half writes the values in those fields. Thanks for the advice everyone. I am glad I was able to figure it out in time to submit my assignment.

if (isset($_GET['dbToCSV']))
{
    // database connection
    db_connect();

    $query = mysql_query("SELECT * FROM employee_data") or die(mysql_error());
    $number_rows = mysql_num_rows($query);

    if ($number_rows >= 1)
    {
        $filename = "exported_db_" . date("m-d-Y_hia") . ".csv"; // filenme with date appended
        $fp = fopen($filename, "w"); // open file

        $row = mysql_fetch_assoc($query);

        $seperator = "";
        $comma = "";

        foreach ($row as $name => $value)
        {
            $seperator .= $comma . $name; // write first value without a comma
            $comma = ","; // add comma in front of each following value
        }
        $seperator .= "\n";

        echo "Database has been exported to $filename";

        fputs($fp, $seperator);

        mysql_data_seek($query, 0); // use previous query leaving out first row

        while($row = mysql_fetch_assoc($query))
        {
            $seperator = "";
            $comma = "";

            foreach ($row as $name => $value)
            {
                $seperator .= $comma . $value; // write first value without a comma
                $comma = ","; // add comma in front of each following value 
            }

            $seperator .= "\n";

            fputs($fp, $seperator);
        } 

        fclose($fp);

    }
    else
        echo "There are no records in the database to export.";

    mysql_close();   
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfcfge
系列文章
更多 icon
同类精品
更多 icon
继续加载