Jump to content

How to show message only once in a loop?


rajeshkr

Recommended Posts

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

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!';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.