将变量集PHP循环到CSV

时间:2014-08-27 18:24:57

标签: php html

我试图设计一个用户界面,允许用户同时发出多个请求(针对搜索结果中显示的所有项目)。我已将其设置为单个请求完美运行的位置,但它只会获取组请求中的最后一项。有没有办法循环并获取所有变量并每次都写出一个新行?我确信使用相同的' name =""'价值是错误的,但我没有足够的智慧知道什么是正确的。

HTML:

  <form name="single" action="get.php" method="post">
    <table>
      <tbody>
        <tr >
          <td colspan="2"><p>Convert All Items in Cart <br>
              <span style="font-weight:bold;font-size:10px;color:red">CAUTION: Only use the "Convert All Items" field if all titlegroups are going to be converted from and to the exact same asset types<span> </p></td>
        </tr>
        <tr>
          <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><br />
            <p>Additional Requirements:</p>
            <br/>
            <textarea name="textarea1" rows="6" cols="20" style="width:230px"></textarea>
            <hr/></td>
        </tr>
        <tr>
          <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><p>Rush Date: </p>
            <br/>
            <input name="date1" type="text" class="datepicker" placeholder="Rush request dates are not guaranteed." style="height:2em;width:230px" />
            <hr/></td>
        </tr>
        <tr>
          <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><p>Select Source Type</p>
            <br/>
            <select name="source1" style="width:230px;resize:both" multiple required>
              <option disabled selected hidden>Select Source Type</option>
              <optgroup label="Select All that Apply">
              <option>Lets just say I'm saving a lot of space</option>
              </optgroup>
            </select>
            <hr/></td>
        </tr>
        <tr>
          <td class="titleGalleryViewLabel" style="font-weight:bold" colspan="2"><p>Request Type</p>
            <br/>
            <select name="type1" style="width:230px;resize:both" multiple required>
              <option disabled selected hidden>Select Request Type</option>
              <optgroup label="Select All that Apply">
              <<option>Lets just say I'm saving a lot of space</option>
              </optgroup>
            </select>
            <hr/></td>
        </tr>
        <tr>
          <td class="titleGalleryViewLabel">&nbsp;</td>
        </tr>
        <tr>
          <td><input type="submit" name="submit" value="Submit" class="csbutt"></input></td>
          <td><input class="csbutt" type="reset" name="reset" value="Reset Form"></input></td>
        </tr>
      </tbody>
    </table>
<input name="cwi" type="hidden" value=" "/>
<input name="publisher" type="hidden" value="Pub 1"/>
<input name="title" type="hidden" value="Title 1"/>
<input name="parent" type="hidden" value="ISBN 1"/>
<input name="author" type="hidden" value="Author 1"/>
<input name="bill" type="hidden" value=" "/>

<input name="cwi" type="hidden" value=" "/>
<input name="publisher" type="hidden" value="Pub 2"/>
<input name="title" type="hidden" value="Title 2"/>
<input name="parent" type="hidden" value="ISBN 2"/>
<input name="author" type="hidden" value="Author 2"/>
<input name="bill" type="hidden" value=" "/>
  </form>

PHP:

<?php
require 'class.phpmailer.php';


date_default_timezone_set('America/Kentucky/Louisville');
$date = time();
$day= getdate();

if(isset($_POST['source1'])){
    $org = 'request.csv';
    $new = $date.'_requested.csv';
    if (!copy($org, $new)) {
        echo "Failed to copy";
    }
    $del = ",";
    $data[1] = $_POST['cwi'];
    $data[2] = $_POST['date1'];
    $data[3] = $_POST['date1'];
    $data[4] = $_POST['parent'];
    $data[5] = $_POST['parent'];
    $data[6] = $_POST['publisher'];
    $data[7] = $_POST['title'];
    $data[8] = $_POST['author'];
    $data[9] = $_POST['bill'];
    $data[10] = $_POST['source1'];
    $data[11] = $_POST['type1'];
    $data[12] = $_POST['textarea1'];
    $file = fopen($date.'_requested.csv', "a");
    $data = "\r\n".implode($del, $data);
    fwrite($file, $data);
    fclose($file);
    $pub = $_POST['publisher'];

    $to = 'foo@bar.com';
    $toname = 'John Doe';
    $from = "foo2@bar2.com";
    $fromname = 'Jane Doe';
    $subject = "Foo bar foo bar";
    $message = "<html>
        <head>
        <title>Request</title>
        </head>
        <body>
        <table>
            <tr>
                <td>Body</td>
            </tr>
        </table>
        </body>
        </html>";

//PHP Mailer
    $mail = new PHPMailer();
    $mail->IsSendmail();
    $mail->FromName = $fromname;
    $mail->From = $from;
    $mail->AddAddress($to, $toname);
    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->AddAttachment($date.'_requested.csv');
    $mail->isHTML(true);
    $result = $mail->send();
}
else{
    echo "Something went wrong";
}
?>

2 个答案:

答案 0 :(得分:0)

通过将[]附加到变量名称,您可以让表单提交一个变量数组供您的PHP解析。所以这个:

<textarea name="textarea1" rows="6" cols="20" style="width:230px"></textarea>

变为:

<textarea name="textarea1[]" rows="6" cols="20" style="width:230px"></textarea>

来源:http://php.net/manual/en/faq.html.php

答案 1 :(得分:0)

你可以做点什么 这样:

<input name="post_data[]" type="text">
<input name="post_data[]" type="number">
<input name="post_data[]" type="email">

服务器端:

$array_of_posted_data_from_form=$_POST["post_data"];
scv='';
foreach($array_of_posted_data_from_form as $value_from_data_from_form_array){
   $scv .="\r\n".$value_from_data_from_form_array;
}