2014-08-27 79 views
0

我試圖設計一個用戶界面,該界面允許用戶同時提出多個請求(用於搜索結果中顯示的所有項目)。我已將它設置爲單個請求將完美工作的位置,但它只會提取組請求中的最後一個項目。有沒有辦法循環,拿起所有的變量,並每次寫出一個新行?我確定使用相同的'name ='「'值是錯誤的,但我不夠明智,不知道什麼是正確的。將變量集PHP循環到CSV

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 = '[email protected]'; 
    $toname = 'John Doe'; 
    $from = "[email protected]"; 
    $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"; 
} 
?> 

回答

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

+0

它在PHP的分隔符行上出錯。我是否需要將'$ del =「,」;'更改爲其他內容? – 2014-08-27 18:46:26

+0

您還需要更改後端。所有$ _POST變量現在都是您必須同時循環的數組。例如,計算count($ _ POST ['id'])的總行數,然後執行'for($ i = 0; $ i Wige 2014-08-27 19:14:47

+0

正如Walter所說,「Donny,你你的元素已經不存在了。「我要去學習一些PHP課程,然後回到你的答案並再試一次。我已經做了一段時間的網頁設計,但是這個更復雜的編程是新的領域。 – 2014-08-28 13:08:09

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; 
}