I have a question about PHP foreach loops. I’m using it in Codeigniter.
Basically I have a list of items in a db and I’m using a foreach loop to run a script on each item in the db until the script has been run on every item.
My question is: If I had, for example, 45 items in the database and then initiated the loop, what would happen if additional items were added to the database list while the loop was still running, thus making a bigger list of items to be looped over?
Would it either:
Run all the items that were in the loop when it was initiated (in this case, 45).
or
Run all the items in the loop and when it gets down to the end (at number 45) continue to run the script on the additional ones I added after the loop was initiated.
I can’t seem to find any answers for this, any info is most appreciated
When you fetch all items from the db with CodeIgniter, it puts the results in an result object or array, depending if you use
$this->db->result();or$this->db->result_array();So when you loop over this result, any new records won’t show up in this object or array. Only when you fire a new query, any new rows will be added to the result.