通过PHP按钮将表导出为CSV
我真的是php新手,我所学的一切都来自我的学校教科书和在线研究。话虽如此,我正试图完成一项任务,而我被困在最后一部分。对于最后一部分,作业说创建一个PHP脚本,该脚本将把雇员表的内容转储到CSV文本文件中,该文本文件的每个记录都用逗号分隔。每个新记录应从新行开始 强>。我尝试了许多在线教程,但是没有一个教程讲授如何将这个事件置于一个按钮中。我包含了我的代码,因此您可以看到我的烂摊子。这还不错,但是我相信我可以用更少的代码来完成相同的任务。同样,我只是刚开始,这是我目前能做的最好的事情。任何人都可以就如何做到这一点提出任何建议。我在第140行有我的按钮。我还使用了db_connect()函数,因此不必多次编写它。在保存为csv之前,我可以使用peon读取数据库。
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>
因此,课程结束了,我想我会证明我如何解决我的问题。这对我来说最有意义,不需要很多代码。我相信可能有更短的方法来做到这一点,但是对于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();
}