我正在測試textarea
框及其對應的foreach
代碼。成功工作。基本上,代碼將每個值放入textarea框中,並將每個值插入到數據庫中。對於不同變量的多個foreach?在數據庫中複製的值
但我想複製這個,因爲我有多個textarea框。
任何人都可以驗證以下代碼嗎?
$text = trim($_POST['BachelorsDegrees']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
$textCert = trim($_POST['Certifications']);
$textArCert = explode("\n", $textCert);
$textArCert = array_filter($textArCert, 'trim'); // remove any extra \r characters left behind
$textDip = trim($_POST['Diplomas']);
$textArDip = explode("\n", $textDip);
$textArDip = array_filter($textArDip, 'trim'); // remove any extra \r characters left behind
$textAD = trim($_POST['AssociateDegrees']);
$textArAD = explode("\n", $textAD);
$textArAD = array_filter($textArAD, 'trim'); // remove any extra \r characters left behind
$textMD = trim($_POST['MastersDegrees']);
$textArMD = explode("\n", $textMD);
$textArMD = array_filter($textArMD, 'trim'); // remove any extra \r characters left behind
$textDD = trim($_POST['DoctorateDegrees']);
$textArDD = explode("\n", $textDD);
$textArDD = array_filter($textArDD, 'trim'); // remove any extra \r characters left behind
$textSD = trim($_POST['SpecialDegreePrograms']);
$textArSD = explode("\n", $textSD);
$textArSD = array_filter($textArSD, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $BachelorsDegrees)
{
foreach ($textArCert as $Certifications)
{
foreach ($textArDip as $Diplomas)
{
foreach ($textArAD as $AssociateDegrees)
{
foreach ($textArMD as $MastersDegrees)
{
foreach ($textArDD as $DoctorateDegrees)
{
foreach ($textArSD as $SpecialDegreePrograms)
{
mysql_query("INSERT INTO College_Courses (Name, BachelorsDegrees, Certifications,Diplomas,AssociateDegrees,MastersDegrees,DoctorateDegrees,SpecialDegreePrograms) VALUES ('$Name','$BachelorsDegrees', '$Certifications', '$Diplomas', '$AssociateDegrees', '$MastersDegrees', '$DoctorateDegrees','$SpecialDegreePrograms')") or die(mysql_error()) ;
}
}
}
}
}
}
}
}
else
{
echo mysql_error();
}
這是他們看起來像在數據庫:
DoctorateDegrees似乎工作正常,但其自身複製。
如果我把
<p>Onion1<br>
Onion2<br>
Onion3</p>
到textarea的箱子,他們將每個人都有自己的行,但它會不斷重複這樣的:
<p>Onion1<br>
Onion2<br>
Onion3</p>
<p>Onion1<br>
Onion2<br>
Onion3</p>
<p>Onion1<br>
Onion2<br>
Onion3</p>
等欄目,例如,BachelorsDegrees(這在我插入額外的foreach代碼之前一直工作正常),只會重複第一個值:
<p>Onion1<br>
<p>Onion1<br>
<p>Onion1<br>
MastersDegrees將重複相同的值一定的時間:
<p>Onion1<br>
<p>Onion1<br>
<p>Onion1<br>
Onion2<br>
Onion2<br>
Onion2<br>
Onion3<br>
Onion3<br>
Onion3<br>
任何想法?
注意:BachelorsDegrees
是之前工作之前,我在測試它之前添加在其他列。
很難回答這個問題。你能提供測試輸入數據嗎?解釋你想要什麼與你在數據庫中得到什麼?內循環將爲外循環的每次迭代重複其完整範圍的值。即使每個循環數組中只有3個值和7個循環級別,您也會得到每個$ textAr值3 * 3 * 3 * 3 * 3 * 3次。那麼你肯定** BachelorsDegrees「只是重複第一個值」? –
是的。它只適用於BachelorsDegrees自身,這意味着其他foreach循環和數組不在代碼中。當我把代碼插入時,第一個值會被多次插入到數據庫中。我在多行中看到相同的值。 – ThomasTheNoobCoder
的確,我的觀點也一樣。假設你有兩個級別的foreach()。然後,外層將取第一個值,然後**重用**內層foreach中的每個**值。當內部foreach已經遍歷所有的值時,它回到外部和第二個值。然後內部的foreach會再次重複所有的值,現在使用來自外部循環的第二個值。所以我的直覺是你可能想要重新設計你的代碼,這可能是你想要的。 –