I'd like to help but am extremely confused by your code and what you are trying to do. I would reiterate trying to start simply, get it to work and then add more complexity.
And try to make your code look like code found in the book. It makes it a lot easier for us to understand. http://web2py.com/book/default/chapter/10

