上下文
我有一个带复选框和php页面的表单来处理数据。我查看了我的数据:一切正常。
要管理复选框,我会做一个" FOREACH"其中有几个" IF"根据表单中选中的框,找到带有指令和sql查询的循环。
我尝试循环我的数组以进入循环" IF"适用于用户检查的每个方框。
问题:
当我运行脚本时,我检查3或5个框,只检查最后一个循环" IF"被考虑在内...... :(
我检查了表格HTML和PHP测试POST:一切正常。
所以我的问题是:
我在哪里陷入困境,以至于foreach只能通过" IF"中的数组的最后一个元素。环
以下代码:
if ( isset($_POST['ModalMultipleColorEdit']) && !empty($_POST['Planning_ID2']) && !empty($_POST['CaseColor2']) && !empty($_POST['CaseWeek2']) )
{
//------- Extract Needed Posted Data
$TargetID = $_POST['Planning_ID2'];
$TargetColor = $_POST['CaseColor2'];
$TargetMonth = $_POST['CaseMonth2'];
//----- GET OLD DATA
$REQ = $DB->query("SELECT * FROM `Planning` WHERE `ID_Planning` = ".$TargetID." ;")
or die(var_dump($REQ->errorInfo()));
$DATA = $REQ->fetch();
// Explore Targeted Weeks Array
foreach( $_POST['CaseWeek2'] as $CheckedWeek )
{
//----------- IF TARGET MONTH IS JANUARY
if ( $TargetMonth == "Janvier" && $CheckedWeek == "(S-1)" )
{
//------- Extract Color String Period
$GetColor = explode("." , $DATA['Planning_M1']);
//------- Change Period Color
$GetColor[0] = $TargetColor;
//------- Build New Color String
$NewCaseMonth = $GetColor[0].".".$GetColor[1].".".$GetColor[2].".".$GetColor[3].".".$GetColor[4];
// ------ UPDATE REQUEST
$REQ2 = $DB->prepare('UPDATE `Planning` SET Planning_M1 = ? WHERE `ID_Planning` = ?');
$REQ2->execute(array($NewCaseMonth, $TargetID)) or die(var_dump($REQ2->errorInfo()));
$REQ2 = NULL;
}
if ( $TargetMonth == "Janvier" && $CheckedWeek == "(S-2)" )
{
//------- Extract Color String Period
$GetColor = explode("." , $DATA['Planning_M1']);
//------- Change Period Color
$GetColor[1] = $TargetColor;
//------- Build New Color String
$NewCaseMonth = $GetColor[0].".".$GetColor[1].".".$GetColor[2].".".$GetColor[3].".".$GetColor[4];
// ------ UPDATE REQUEST
$REQ2 = $DB->prepare('UPDATE `Planning` SET Planning_M1 = ? WHERE `ID_Planning` = ?');
$REQ2->execute(array($NewCaseMonth, $TargetID)) or die(var_dump($REQ2->errorInfo()));
$REQ2 = NULL;
}
if ( $TargetMonth == "Janvier" && $CheckedWeek == "(S-3)" )
{
//------- Extract Color String Period
$GetColor = explode("." , $DATA['Planning_M1']);
//------- Change Period Color
$GetColor[2] = $TargetColor;
//------- Build New Color String
$NewCaseMonth = $GetColor[0].".".$GetColor[1].".".$GetColor[2].".".$GetColor[3].".".$GetColor[4];
// ------ UPDATE REQUEST
$REQ2 = $DB->prepare('UPDATE `Planning` SET Planning_M1 = ? WHERE `ID_Planning` = ?');
$REQ2->execute(array($NewCaseMonth, $TargetID)) or die(var_dump($REQ2->errorInfo()));
$REQ2 = NULL;
}
if ( $TargetMonth == "Janvier" && $CheckedWeek == "(S-4)" )
{
//------- Extract Color String Period
$GetColor = explode("." , $DATA['Planning_M1']);
//------- Change Period Color
$GetColor[3] = $TargetColor;
//------- Build New Color String
$NewCaseMonth = $GetColor[0].".".$GetColor[1].".".$GetColor[2].".".$GetColor[3].".".$GetColor[4];
// ------ UPDATE REQUEST
$REQ2 = $DB->prepare('UPDATE `Planning` SET Planning_M1 = ? WHERE `ID_Planning` = ?');
$REQ2->execute(array($NewCaseMonth, $TargetID)) or die(var_dump($REQ2->errorInfo()));
$REQ2 = NULL;
}
if ( $TargetMonth == "Janvier" && $CheckedWeek == "(S-5)" )
{
//------- Extract Color String Period
$GetColor = explode("." , $DATA['Planning_M1']);
//------- Change Period Color
$GetColor[4] = $TargetColor;
//------- Build New Color String
$NewCaseMonth = $GetColor[0].".".$GetColor[1].".".$GetColor[2].".".$GetColor[3].".".$GetColor[4];
// ------ UPDATE REQUEST
$REQ2 = $DB->prepare('UPDATE `Planning` SET Planning_M1 = ? WHERE `ID_Planning` = ?');
$REQ2->execute(array($NewCaseMonth, $TargetID)) or die(var_dump($REQ2->errorInfo()));
$REQ2 = NULL;
}
}
//------- REDIRECT WHEN SUCCESS AND NO MORE TARGETS
header('Location: PlanningNow.php?Process=Success');
exit();
}
else
{
header('Location: PlanningNow.php?Process=ErrorForm');
exit();
}
提前感谢您的帮助! :)
答案 0 :(得分:1)
我必须编制一些数据进行测试。考虑这种方法将查询调用减少到一个,并在很大程度上整合您的代码......
代码:(Demo)
$_POST['CaseWeek2']=['(S-2)','(S-3)','(S-5)']; // target checkboxes
$_POST['CaseColor2']='purple'; // target color
$_POST['CaseMonth2']='Janvier'; // target month
$checkedWeeks=['(S-1)','(S-2)','(S-3)','(S-4)','(S-5)']; // possible checkboxes
$DATA['Planning_M1']='red.orange.yellow.green.blue'; // old data from db
$oldcolors=explode('.',$DATA['Planning_M1']);
if($_POST['CaseMonth2']=='Janvier'){
// var_export(array_intersect($checkedWeeks,$_POST['CaseWeek2']));
$keys=array_keys(array_intersect($checkedWeeks,$_POST['CaseWeek2'])); // get appropriate keys
// var_export($keys);
$values=array_fill(0,sizeof($keys),$_POST['CaseColor2']); // generate equal number of values
// var_export($values);
$replacements=array_combine($keys,$values); // combine keys and values to make correct array
// var_export($replacements);
$newcolors=array_replace($oldcolors,$replacements); // replace old values with new values
var_export($newcolors);
//$REQ2=$DB->prepare('UPDATE `Planning` SET Planning_M1 = ? WHERE `ID_Planning` = ?');
//$REQ2->execute(array(implode('.',$newcolors), $TargetID));
}
输出:
array (
0 => 'red',
1 => 'purple',
2 => 'purple',
3 => 'green',
4 => 'purple',
)
哦,为了记录,你之前的迭代被“忽略”的原因是:
$GetColor = explode("." , $DATA['Planning_M1']);
每次调用UPDATE时,您都有效地提取旧的缓存$DATA['Planning_M1']
数据,交换单一颜色,并覆盖之前迭代的更新。
如果你想保持你的代码大致相同,你可以写:
$GetColor = explode("." , $DATA['Planning_M1']);
在foreach()
循环之前 ,然后有条件地修改$GetColor
,因为您正在使用$GetColor[n]
。然后在循环结束后,您可以使用完全修改的$GetColor
数据对数据库进行单次UPDATE调用。