How to get confirmations for incoming transactions?
Calling bitcoin-cli listunspent 0 9999999 "[\"someLocalAddress\",\"someOtherLocalAddress\"]" will provide you with the exact number of confirmations for every transaction to these addresses so you will not need to perform all these iterations. 

Replacing 9999999 with what you consider in your application to be a confirmed transaction will only return the unconfirmed transactions, which is more effective if you already keep track of the confirmed transactions in your database.