gpt4 book ai didi

php - 在 foreach () 循环中打破 if

转载 作者:行者123 更新时间:2023-11-29 02:51:22 25 4
gpt4 key购买 nike

我正在完成我的网站,但我遇到了中断问题foreach 语句中的 if 语句, 我想当一个学生试图选择他/她已经选择的工作时,if() 失败并从整个 foreach() 循环中中断,这是我的代码,if() 中的中断只中断 if (), 不是整个foreach 请帮助我。谢谢

foreach($_POST['JobId'] AS $i){
$sqlCheckingBeofrAdding ="SELECT * FROM JobsLists WHERE JobId = '".$i."' AND SSU = '".$SSU."' ";
$rs = mysqli_query($dbCIE ,$sqlCheckingBeofrAdding);

$row1 = mysqli_fetch_assoc($rs);

if($row1 > 0){
echo "You Already Have this Job In your List";
break;
}

//insert into junction table..
$sqlUpdate = "UPDATE Jobs SET NoStudent=NoStudent-1 WHERE JobId = '" . $i . "'";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));

$sqlInsert ="INSERT INTO JobsLists(`JobID` , `SSU`) VALUES(".$i.",'".$SSU."' )";
$MyQuery= mysqli_query($dbCIE, $sqlInsert) or die(mysqli_error($dbCIE));

}

最佳答案

如果您的查询只返回单个学生记录,那么您可以使用 return 并且这不会执行循环内的进一步代码。

如果您的查询返回多个学生记录,您可以使用continue

对于第 1 点:

if($row1 > 0){ 
echo "You Already Have this Job In your List";
return;
}

对于第 2 点:

if(condition){ 
echo "You Already Have this Job In your List";
continue;
}

关于php - 在 foreach () 循环中打破 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com