2016-10-20 6 views
1

私は多次元配列と単一配列からなるフォームを持っています。多次元配列は、ユーザーがボタンをクリックしたときに動的に生成されるので、別のジョブや別の程度のようなフィールドを追加することができます。PHP多次元配列からCSV

多次元配列の場合は、データを収集していることを確認するためにテストしたかったのです。教育用インプットコレクションprint_r($_POST["educationHistory"]で以下を実行して、アレイをテストし、正しくロギングしていることを確認してください。それは次のように配列内容を出力します。

Array ([0] => WLU [1] => Math [2] => 2016) 

そして、後でログに記録される配列ごとに、数値が変化します。

多次元配列を.CSVに保存しようとすると、かなり異なってきます。

私は上記のprint_rレイアウトのスタイルをプッシュしようとしています(Array ([0] => WLU [1] => Math [2] => 2016))私はすべてのフォームフィールドを出力していますが、運がないです。

このようなことは可能ですか?

このような配列の値を印刷することができたのは、上記の例です。WLU Math 2016ですが、2または3のエントリがあると混乱することがあります。

アドバイスはありますか?ここで

は完全なコードです:

PHP

<?php 

//page one inputs 

$firstName = $_POST["firstName"]; 
$lastName = $_POST["lastName"]; 
$homeAddress = $_POST["homeAddress"]; 
$homeAddressTwo = $_POST["homeAddressTwo"]; 
$city = $_POST["city"]; 
$province = $_POST["province"]; 
$postalCode = $_POST["postalCode"]; 
$homePhone = $_POST["homePhone"]; 
$personalEmail = $_POST["personalEmail"]; 
$confirmEmail = $_POST["confirmEmail"]; 
$oectaNumber = $_POST["oectaNumber"]; 
$memberStatus = $_POST["memberStatus"]; 
$teacherTraining = $_POST["teacherTraining"]; 
$teachingYears = $_POST["teachingYears"]; 


$employmentHistory = $_POST["employmentHistory"]; 
$employmentHistoryValues = ""; 

foreach($employmentHistory as $value) 
{ 
    $employmentHistoryValues .= $value . " ||| "; 
} 

$nonSchoolEmployer = $_POST["nonSchoolEmployer"]; 
$nonSchoolEmployerValues = ""; 
foreach($nonSchoolEmployer as $employerValue) 
{ 
    $nonSchoolEmployerValues .= $employerValue . " ||| "; 
} 


$educationHistoryValues = ""; 
foreach(print_r($_POST["educationHistory"]) as $educationValue) 
{ 
    $educationHistoryValues .= $educationValue; 
} 



$csvdata = $firstName . ", " . $lastName . ", " . $homeAddress . ", " . $homeAddressTwo . ", " . $city . ", " . $province . ", " . $postalCode . ", " . $homePhone . ", " . $personalEmail . ", " . $confirmEmail . ", " . $oectaNumber . ", " . $memberStatus . ", " . $teacherTraining . ", " . $teachingYears . "," . $employmentHistoryValues . "," . $nonSchoolEmployerValues . "," . $educationHistoryValues; 

$fp = fopen("formdata.csv", "a"); 

if($fp) 
{ 
    fwrite($fp, $csvdata . "\n"); 
    fclose($fp); 
} 

?> 

HTML:

<div id="educationHistory" name="educationHistory[]"> 
    <input type="text" class="three-lines" name="educationHistory[]" id="educationInstitution_1" placeholder="Institution" onblur="this.placeholder='Institution'" onfocus="this.placeholder=''" onkeyup="checkPage2()" /> 

    <input type="text" class="three-lines" name="educationHistory[]" id="degreeFromInstitution_1" placeholder="Degree/Diploma" onblur="this.placeholder='Degree/Diploma'" onfocus="this.placeholder=''" onkeyup="checkPage2()" /> 

    <input type="date" class="three-lines" name="educationHistory[]" id="educationalDates_1" /> 
    <input type="button" value="+" onclick="addEducation()" /> 
</div><!--end educationHistory Div -->     
</div><!--end of education div--> 
+0

さてあなたは次に – RiggsFolly

+0

を処理しているどのようなデータたちを示すことから始めますが、出力は – RiggsFolly

+0

になりたいどのような形式私たちを見ますその後、あなたは何時のレートを求めるのですか? – RiggsFolly

答えて

0

この$ _POSTデータを仮定:ここ

$_POST['employmentHistory']=array('somehere','somethere'); 
$_POST['nonSchoolEmployer']=array('somebody','somebodyelse'); 
$_POST['educationHistory']=array('heapshere','heapsoverthere','andtheretoo'); 
$_POST['firstName']="John"; 
$_POST['lastName']="Smith"; 
$_POST['homeAddress']="1 Here St."; 
$_POST['homeAddressTwo']="Apt 2"; 
$_POST['city']="Townsville"; 
$_POST['province']="Quebec"; 
$_POST['postalCode']="H1C"; 
$_POST['homePhone']="418-555-0157"; 
$_POST['personalEmail']="418-555-0135"; 
$_POST['confirmEmail']="[email protected]"; 
$_POST['oectaNumber']="999999"; 
$_POST['memberStatus']="Active"; 
$_POST['teacherTraining']="some"; 
$_POST['teachingYears']="eleventeen"; 

は、あなたのコードは次のとおりです。(私は$ _Pがあなたに警告する義務がありますOSTデータは、第1サニタイズ/検証する必要があります)

$employmentHistoryValues=implode(' ||| ',$_POST["employmentHistory"]); 
// this avoids the trailing ' ||| ' that your method produced. 

$nonSchoolEmployerValues=implode(' ||| ',$_POST["nonSchoolEmployer"]); 
// this avoids the trailing ' ||| ' that your method produced. 

// Your foreach(print_r()) will throw a WARNING and should not be used. 
// Your method smashes all $_POST["educationHistory"] data together with no separator, so I will too. 
$educationHistoryValues=implode('',$_POST["educationHistory"]); 

// declare the keys of the $_POST values you want to extract 
$postkeys=array('firstName','lastName','homeAddress','homeAddressTwo','city','province','postalCode','homePhone','personalEmail','confirmEmail','oectaNumber','memberStatus','teacherTraining','teachingYears'); 

// join the single-value elements with the multi-value elements and glue them together as a comma-space-separated string. 
$csvdata=implode(', ',array_merge(array_intersect_key($_POST,array_flip($postkeys)),[$employmentHistoryValues,$nonSchoolEmployerValues,$educationHistoryValues])); 

echo "csvdata = $csvdata"; // remove after you verify result as correct 

if($fp=fopen("formdata.csv", "a")){ 
    fwrite($fp,$csvdata."\n"); 
    fclose($fp); 
} 

出力:

csvdata = John, Smith, 1 Here St., Apt 2, Townsville, Quebec, H1C, 418-555-0157, 418-555-0135, [email protected], 999999, Active, some, eleventeen, somehere ||| somethere, somebody ||| somebodyelse, heapshereheapsoverthereandtheretoo 

編集:私はちょうどあなたの質問を再読み込み、問題命名フォームフィールドに対応していませんでした...

educationHistoryデータを3つにバッチする場合は、次のようにフォームを設定できます。

<input type="text" class="three-lines" name="educationHistory[0][institution]" id="educationInstitution_1" placeholder="Institution" onblur="this.placeholder='Institution'" onfocus="this.placeholder=''" onkeyup="checkPage2()" /> 

<input type="text" class="three-lines" name="educationHistory[0][degree]" id="degreeFromInstitution_1" placeholder="Degree/Diploma" onblur="this.placeholder='Degree/Diploma'" onfocus="this.placeholder=''" onkeyup="checkPage2()" /> 

<input type="date" class="three-lines" name="educationHistory[0][date]" id="educationalDates_1" /> 
<input type="button" value="+" onclick="addEducation()" /> 

そして、addEducation()は、次のフィールドのバッチのインデックスをインクリメントします。

受信:

$_POST['educationHistory']=array(
    array('institute'=>'WLU','degree'=>'Math','date'=>'2016'), 
    array('institute'=>'UQ','degree'=>'English','date'=>'2012') 
); 

あなたの処理コードは、その後、次のようになります。

$educationHistoryValues=''; 
foreach($_POST['educationHistory'] as $a){ 
    $educationHistoryValues.=($educationHistoryValues!=''?', ':'').implode(' ',$a); 
} 

echo "educationHistoryValues = $educationHistoryValues"; 
// educationHistoryValues = WLU Math 2016, UQ English 2012