0

I have a function that checks emails from an imap server using php imap functions.

That functions checks all emails in a folder starting from 1 to x=total messages.

and inside that function there is a script that checks if the emails is an spam email, and if it is it delete it from the server.

When the spam mail is deleted the total messages on the folder is reduced by 1. And the for loop skips the next email.

example, inbox haves 10 emails, and the for loop starts in 1 to 10. If email 5 is found to be spam it gets deleted, but when that happens the email that was 6 becomes 5, and the for loop skips it because it tries to read email 6.

How can I go back by 1 when that happens?

for(var $i = 1; $i<=$totalMessages; $i++){
  if(isSpam()==true){
    //delete from imap server
    $i = $i-1 //does not work, $i = $i -2 does not work eighter
  }
}
Alvaro Hernandorena
  • 610
  • 1
  • 5
  • 18

2 Answers2

2

This kind of problem is often solved by iterating backwards:

for(var $i = $totalMessages; $i>= 1; $i--){
    // ...etc
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • @AlvaroHernandorena, Did it work out for you? Could you let me know? – trincot May 02 '16 at 07:01
  • I am a bit surprised that my answer was "unaccepted" after so many days. Was there an issue? – trincot May 07 '16 at 20:48
  • Yout anwser was ok, but for my app the best anwser was the other one.I realized that going backwards on the for loop was not the best practice. – Alvaro Hernandorena May 09 '16 at 13:13
  • 1
    I am interested to know about "the best practice". Do you have a reference for it? (see for instance [this](http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it)) – trincot May 09 '16 at 15:02
  • "The best practice" was not what I meant, the best solution for my case was what I have should written. That function checks for new mails from an IMAP folder, starting from the last email already saved last time. For example starting 500 to 520. Going backwards made this: it started from 520 until 500, but if that loop is ended before it is completed, the emails indexed are for example 520,519,518,break (not indexing the emails between 517 and 500). – Alvaro Hernandorena May 11 '16 at 02:36
  • Then the function is called again and the last mail indexed is 520, so it goes from 520 to 520 and it ends, leaving the mails between 518 and 500 behind. Of course there should be a work around for that, but in my code changing the "for" for a "while" made the trick without problems. – Alvaro Hernandorena May 11 '16 at 02:36
2

in this case you are better of with a while loop

$index = 1;
while( $index <= $totalMessages) {
  if(isSpam()==true){
    //delete from imap server
  } else {
    $index++;
  }
}
RST
  • 3,899
  • 2
  • 20
  • 33
  • First I used the solution provided on the first anwser, but going backwards in the for loop was bringing me a lot of disadventages. So I ended using the while that you sugested. tnx RST – Alvaro Hernandorena May 07 '16 at 18:44