My session.counter is not counting

67 views
Skip to first unread message

Joe

unread,
Feb 26, 2016, 8:05:29 PM2/26/16
to web2py-users
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 == 1
I'd appreciate some help with this.

Controller:

def index():
    images = db().select(db.image.ALL, orderby=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 == 1:
        session.counter = (session.counter or 0)+1
        one_counter=session.one_counter
    elif image.id == 2:
        session.counter = (session.counter or 0)+1
        two_counter=session.two_counter
    elif 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>

Kiran Subbaraman

unread,
Feb 26, 2016, 11:31:11 PM2/26/16
to web...@googlegroups.com
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/
--
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 web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe

unread,
Feb 27, 2016, 12:07:00 AM2/27/16
to web2py-users
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

Kiran Subbaraman

unread,
Feb 27, 2016, 6:20:25 AM2/27/16
to web...@googlegroups.com
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/

Joe

unread,
Feb 27, 2016, 9:06:22 AM2/27/16
to web2py-users
Hello Kiran,

Yes, I know I wont be able to track the total number of clicks across all users. I want to serve a targeted ad banner to a user based on which category of images he or she clicks the most. In other words, different users will see different banners based on their individual likes or dislikes.

Thank you so much for helping me with this. I appreciate your answer. Your solution works perfectly. My problem is solved now and I learned something about session variables.

Thanks again.

Cheers,

Joe

Kiran Subbaraman

unread,
Feb 28, 2016, 10:38:30 AM2/28/16
to web...@googlegroups.com
Joe,
Glad the issue is sorted out.
If you haven't already, I suggest you also take a look at the web2py book's 'Session' section: http://web2py.com/books/default/chapter/29/04/the-core#session

________________________________________
Kiran Subbaraman
http://subbaraman.wordpress.com/about/
Reply all
Reply to author
Forward
0 new messages