简体   繁体   English

PHP处理大表格

[英]PHP processing large form

I am having difficulty in saving all post data to csv file and emailing the values to an address. 我在将所有发布数据保存到csv文件以及将值通过电子邮件发送到地址时遇到困难。 I have over 125 post variables and im quite stuck. 我有125个以上的发布变量,即时通讯卡住了。 Here is a sample of my code thus far: 到目前为止,这是我的代码示例:

<?php
$x23 = "date";
$x24 = "fclose";
$x25 = "fopen";
$x26 = "fwrite";
$x27 = "mail";
$x28 = "time";
extract($_POST);
$a1 = $_POST['fname'];
$a2 = $_POST['lname'];
$a3 = $_POST['email'];
$a4 = $_POST['landline'];
$a5 = $_POST['mobile'];
$a6 = $_POST['addr1'];
$a7 = $_POST['addr2'];
$a8 = $_POST['towncity'];
$a9 = $_POST['postcode'];
$x1d = $a1 . "," . $a2 . "," . $a3 . "," . $a4 . "," . $a4 . "," . $a5 . "," . $a6 . "," . $a7 . "," . $a8 . "," . $a9 . "
";
$quotation = $x25("file5.csv", "a");
$x26($x1d, $quotation);
$x24($x1d);
header('Location: mydomain.co');
$x20 = 'mail@mydomain.com';   
$x22 = 'Quotation - My Company';
$x27($x20, $x21, $x23, $x1f);
?>

I have not put all variables in this question (not sure how to do this, but I think fputcsv() would do the job, to save all the 125 variables being written, but Iv'e never much used PHP and not sure on arrays and fputcsv()). 我没有将所有变量都放在这个问题中(不确定如何做到这一点,但是我认为fputcsv()可以完成工作,以保存所有写入的125个变量,但是我从来没有使用过太多的PHP,也不确定在数组上使用和fputcsv())。

How would I loop through all my post variables and append them to csv as one row? 我将如何遍历我所有的post变量并将它们作为一行附加到csv?

Any help would be appreciated. 任何帮助,将不胜感激。

The function you're looking for is implode . 您要寻找的功能是implode It implodes an array into a single string with a specific "glue", in this case ','. 它使用特定的“胶水”将数组内嵌到单个字符串中,在本例中为“,”。

So, the simplest way is to use implode(',', $_POST) , but in case not all of the values are supposed to be used, there's a nice way to handle it by changing the names of the form elements. 因此,最简单的方法是使用implode(',', $_POST) ,但是如果不是所有值都应该使用,则有一种很好的方法可以通过更改表单元素的名称来进行处理。

In the form, if you change the name from "fname" into "data[fname]" and do the same for the rest of the elements, then $_POST['data'] will be an array that holds all of the values. 在表单中,如果将名称从"fname"更改为"data[fname]" ,并对其余元素执行相同操作,则$_POST['data']将是一个包含所有值的数组。 Then using implode(',', $_POST['data']) will make sure only desired values are used. 然后使用implode(',', $_POST['data'])将确保仅使用所需的值。

You first of all need to be disciplined about solving one problem at a time. 首先,您必须受训于一次解决一个问题。

(1) Create the CSV data in a variable
(2) Write the data to a file
(3) Send an email
(4) Include an attachment in the email

Or even break it down into more steps. 甚至将其分解为更多步骤。 Trying to solve a bunch of problems all at once is only going to get yourself more confused, especially if you are just getting started on programming. 一次解决所有问题只会使自己更加困惑,尤其是在您刚开始编程时。

So let's solve the "create the CSV" file. 因此,让我们解决“创建CSV”文件。

You can easily loop through your $_POST and create a CSV file like this: 您可以轻松地遍历$_POST并创建一个CSV文件,如下所示:

$csv_line = '';
$sep = ''; // don't put a comma before the 1st element
foreach ($_POST as $k -> $v) {
    $csv_line .= $sep . "\"$v\"";
    $sep = ','; // put a comma before the 2nd, 3rd, etc. elements
}

However, there are at least 2 problems with this. 但是,这至少有两个问题。 You may not want EVERY element in your $_POST to be put into the CSV, and there is no guarantee of the order those elements within $_POST. 您可能不希望$ _POST中的每个元素都放入CSV中,并且不能保证$ _POST中的这些元素的顺序。

So we'll create a separate array with just the fields we are interested in and making sure they are in the order we want to keep it: 因此,我们将创建一个仅包含我们感兴趣的字段的单独数组,并确保它们按照我们想要保留的顺序排列:

$myFields = array('email', 
                  'fname', 
                  'lname', 
                  'junk', 
                  ...);
$csv_line = '';
$sep = ''; // don't put a comma before the 1st element
foreach ($myFields as $k) {
    $csv_line .= $sep . "\"$_POST[$k]\"";
    $sep = ','; // put a comma before the 2nd, 3rd, etc. elements
}

Now all you have to do is write out that line into a file, figure out how to send an email, and figure out how to attach the file to the email. 现在,您要做的就是将该行写到文件中,弄清楚如何发送电子邮件,并弄清楚如何将文件附加到电子邮件中。 But at least you are one step closer... 但是至少你离我们更近了一步...

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

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