当循环遍历下面的foreach
循环时,每次执行循环时都会得到echo
输出。如何将这些消息减少到只有一条最终消息?
即。如果仅"success messages"
,则回显"Success"
,如果发生错误("fail" or "nothing saved" message)
,则回显"Fail"
。一旦发生错误,循环不应停止,但应完全继续。最后,我只想知道是否发生了错误或结果是否未保存。
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
} else {
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result="
//SQL query
";
$query=mysqli_query($conn,$result);
if($query) {
echo "Success.<br>";
} else {
echo "Fail.<br>";
}
} else {
echo "Nothing saved.";
}
}
}
答案 0 :(得分:5)
每个人都建议你一个答案。我的建议如下: -
<?php
$failed_query = array(); // create an empty array to get which query fails
$failed_count = 0; // count to come to know how many query failed
$success_count = 0; // count to come to know how many query succeed
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
}else {
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result="//SQL query";
$query=mysqli_query($conn,$result);
if($query) { // if query runs
$success_count++; // increase success counter
} else {
$failed_count++; // increase failed counter
$failed_query[] = $result; // insert failed query to the array
}
}
}
}
if($success_count > $failed_count){
echo "final output is success";
}else{
echo "final output is failed";
}
echo "query executed successfully(Number)".$success_count;
echo "query execution failed(Number)".$failed_count;
echo "Failed queries are <br/>";
echo "<pre/>";print_r($failed_query);
?>
注意: -
1.你想要的最终状态,你将获得成功或失败。
2.它会让您清楚地了解已成功执行的查询次数以及失败次数。
3.此外,您还可以检查哪些查询失败以及原因(通过检查失败的数组)
答案 1 :(得分:2)
在循环之前设置变量,并在失败时更改它。然后在之后回复它。
$msg = '';
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
} else {
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result = "//SQL query";
$query = mysqli_query($conn, $result);
if(!$query) {
$msg = 'Fail';
}
} else {
$msg = 'Nothing saved.';
}
}
}
if(!empty($msg)) {
echo $msg;
}
答案 2 :(得分:2)
您可以使用错误计数器:
<?php
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
} else {
$errors = 0; //<========== ERROR COUNTER.
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result="
//SQL query
";
$query=mysqli_query($conn,$result);
if($query) {
//echo "Success.<br>";
} else {
//echo "Fail.<br>";
$errors++; //<===========
}
} else {
//echo "Nothing saved.";
$errors++; //<============
}
}
if ( $errors == 0 )
echo "Success";
else echo "Fail";
}
?>
答案 3 :(得分:0)
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
} else {
$output = "";
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result="
//SQL query
";
$query=mysqli_query($conn,$result);
if($query) {
$output .= "Success.<br>";
} else {
$output .= "Fail.<br>";
}
} else {
$output .= "Nothing saved.";
}
}
echo $output;
}
答案 4 :(得分:0)
您可以通过添加标记来实现此目的。
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
} else {
$flag_success = True;
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result="
//SQL query
";
$query=mysqli_query($conn,$result);
if($query) {
} else {
$flag_success = False;
}
} else {
echo "Nothing saved.";
}
}
if($flag_success)
echo "success";
else
echo "error";
}
基本思想是我们假设所有交易在启动时都是成功的。现在如果有错误,我们将布尔标志设置为false。然后最后检查foreach循环外的状态标志并相应地打印输出
答案 5 :(得分:0)
如果你想继续遍历数组(即在遇到错误时不是break
),一般来说,你可以这样做:
$failureHappened = false;
foreach($arr as $elem) {
// do whatever with $elem to check "success" or "failure"
if (conditional_check($elem) == true) {
// success
} else {
// failure
$failureHappened = true;
}
}
if ($failureHappened) {
// error handling
} else {
// success case, if you need to do anything here
}
如果循环结束时$failureHappened
仍为false
,则表示没有错误。如果是true
,则发生一个或多个错误/失败。如果您不特别关心导致错误的原因,只是发生了这样的错误,这很有用。
答案 6 :(得分:0)
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
} else {
$failsCount = 0;
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result="
//SQL query
";
$query=mysqli_query($conn,$result);
if(!$query) {
$failsCount++;
}
} else {
$failsCount++;
}
}
echo ($failsCount == 0) ? "Success.<br>" : "Fail.<br>";
}
答案 7 :(得分:0)
如果我理解正确,如果所有查询都运行正常,您希望打印“成功”,如果查询失败,则打印“失败”。
为此,请使用临时变量来跟踪某个查询是否失败并使用该变量进行打印。更新的代码:
if(!isset($_POST["submitbutton"])) {
echo "Click this button to save your input.";
} else {
$temp = 0
foreach($_POST['tipp_id'] as $key => $tipp_id) {
if($tipp_id > 0) {
$result="
//SQL query
";
$query=mysqli_query($conn,$result);
if($query) {
$temp = 1 // set the temporary variable accordingly
//echo "Success.<br>";
} else {
$temp = 2
//echo "Fail.<br>";
}
} else {
echo "Nothing saved.";
}
}
if($temp == 1) {
echo "Success.<br>";
} elseif($temp == 2) {
echo "Fail.<br>";
}
}