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
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!';
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.