rajeshkr Posted March 22, 2014 Share Posted March 22, 2014 hi, I have a loop on my code, i know it will show message everytime that code will run but i want that the message only show on the final count. for ($i = 0; $i < $post_count; $i++) { $sql[] = "INSERT INTO pro_customer_album_price(customer_id, customer_name, customer_user_name, sl_no, album_name, paper, finish, price, vat, total_amt, date, time) VALUES ('$customer_id','$customer_name','$customer_user_name','".$sl_no[$i]."','".$album_name[$i]."','".$paper_used[$i]."','".$finishing[$i]."','".$price[$i]."','".$vat[$i]."','".$total_amt[$i]."','$date','$time')"; } foreach ($sql as $query) { $data = mysqli_query($con, $query); if(!$data) { echo " Error".mysqli_error($con); } else { echo "Inserted!"; /// this is the message coming everytime but i want this message only show when the loop complete or in last count. } } Thanks Link to comment https://forums.phpfreaks.com/topic/287170-how-to-show-message-only-once-in-a-loop/ Share on other sites More sharing options...
kicken Posted March 22, 2014 Share Posted March 22, 2014 Set a flag variable to indicate if there were any errors or not. Then after your loop check that variable and output the message. $hasErrors=false; foreach ($sql as $query){ $data = mysqli_query($con, $query); if (!$data){ $hasErrors=true; } } if (!$hasErrors){ echo 'Inserted!'; } Link to comment https://forums.phpfreaks.com/topic/287170-how-to-show-message-only-once-in-a-loop/#findComment-1473530 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.