So you want to keep track of the number of a times a user (in a user's session) clicked on the images 1 to 3. Using a session to capture this count, you won't be able to track the total number of clicks across all users (just saying)

My understanding is:
* the url comes in as 'show/some_number'
* you use the the 'some_number' to query the database for a matching 'image'.

The changes you would need to make are:
if image.id == 1:
            session.one_counter = (session.one_counter or 0) + 1
if image.id == 2:
...

________________________________________
Kiran Subbaraman
http://subbaraman.wordpress.com/about/

On Sat, 27-02-2016 10:37 AM, Joe wrote:
Hi Kiran,

Thanks very much for your reply.

I tried to initialize session.one_counter/two_counter/three_counter but it seems my understanding of the session variables in this case is not sufficient to get this right.
Basically, I am stuck with this one.

My aim is to count the clicks to image.id *1*, image.id *2* and image.id *3* separately, so I can compare them.

If you could guide me in the right direction so I can have a better understanding and solve this problem or if you could correct my code, I would really appreciate it.

Thanks again.

Cheers,

Joe

On Saturday, February 27, 2016 at 12:31:11 PM UTC+8, Kiran Subbaraman wrote:

    You seem to be incrementing 'counter' in the session (the counter
    is user session specific then).
    Also, you are using session.one_counter/two_counter/three_counter,
    without having initialized it. Is that a typo?

    ________________________________________
    Kiran Subbaraman
    http://subbaraman.wordpress.com/about/
    <http://subbaraman.wordpress.com/about/>

    On Sat, 27-02-2016 6:35 AM, Joe wrote:
    I am trying to use session.counter to count clicks on images on a
    page.
    My problem is that my session.counter is not counting...
    I am doing something wrong, not sure exactly what.
    I am pretty sure the issue is the/if image.id <http://image.id> == 1/
    I'd appreciate some help with this.
    *Controller:*
    def index():
        images = db().select(db.image.ALL, orderby=db.image.id
    <http://db.image.id>)
        return dict(images=images)
    def show():
        image = db.image(request.args(0,cast=int)) or
    redirect(URL('index'))
        one_counter = []
        two_counter = []
        three_counter = []
        if image.id <http://image.id> == 1:
            session.counter = (session.counter or 0)+1
            one_counter=session.one_counter
        elif image.id <http://image.id> == 2:
            session.counter = (session.counter or 0)+1
            two_counter=session.two_counter
        elif image.id <http://image.id> == 3:
            session.counter = (session.counter or 0)+1
            three_counter=session.three_counter
        return dict(image=image)
    def download():
        return response.download(request, db)
    *View:*
    show:
    <h3>{{=0 if session.one_counter is None else
    session.one_counter}}</h3>
    <h3>{{=0 if session.two_counter is None else
    session.two_counter}}</h3>
    <h3>{{=0 if session.three_counter is None else
    session.three_counter}}</h3>
    -- Resources: - http://web2py.com - http://web2py.com/book
    (Documentation) - http://github.com/web2py/web2py
    <http://github.com/web2py/web2py> (Source code) -
    https://code.google.com/p/web2py/issues/list
    <https://code.google.com/p/web2py/issues/list> (Report Issues)
    --- You received this message because you are subscribed to the
    Google Groups "web2py-users" group. To unsubscribe from this
    group and stop receiving emails from it, send an email to
    [email protected] <javascript:>. For more options,
    visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.

-- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>. For more options, visit https://groups.google.com/d/optout.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to