how to use a loop or query in the existing code.