Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57160 > unrolled thread

Printing a drop down menu for a specific field.

Started byΝίκος Αλεξόπουλος <nikos.gr33k@gmail.com>
First post2013-10-21 02:30 +0300
Last post2013-10-21 11:20 -0400
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Printing a drop down menu for a specific field. Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-21 02:30 +0300
    Re: Printing a drop down menu for a specific field. Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-21 09:07 +0300
      Re: Printing a drop down menu for a specific field. Ben Finney <ben+python@benfinney.id.au> - 2013-10-21 17:51 +1100
      Re: Printing a drop down menu for a specific field. Steven D'Aprano <steve@pearwood.info> - 2013-10-21 06:58 +0000
        Re: Printing a drop down menu for a specific field. Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-21 10:31 +0300
      Re: Printing a drop down menu for a specific field. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-21 08:31 +0100
      Re: Printing a drop down menu for a specific field. Joel Goldstick <joel.goldstick@gmail.com> - 2013-10-21 11:20 -0400

#57160 — Printing a drop down menu for a specific field.

FromΝίκος Αλεξόπουλος <nikos.gr33k@gmail.com>
Date2013-10-21 02:30 +0300
SubjectPrinting a drop down menu for a specific field.
Message-ID<l41p2o$il4$1@dont-email.me>
try:
	cur.execute( '''SELECT host, city, useros, browser, ref, hits, 
lastvisit FROM visitors WHERE counterID = (SELECT ID FROM counters WHERE 
url = %s) ORDER BY lastvisit DESC''', page )
	data = cur.fetchall()
		
	for row in data:
		(host, city, useros, browser, ref, hits, lastvisit) = row
		lastvisit = lastvisit.strftime('%A %e %b, %H:%M')
			
		print( "<tr>" )
		for item in (host, city, useros, browser, ref, hits, lastvisit):
			print( "<td><center><b><font color=white> %s </td>" % item )
except pymysql.ProgrammingError as e:
	print( repr(e) )
===========================================

In the above code i print the record of the mysql table visitors in each 
row like this:  http://superhost.gr/?show=log&page=index.html

Now, i wish to write the same thing but when it comes to print the 
'lastvisit' field to display it in a <select></select> tag so all prior 
visits for the same host appear in a drop down menu opposed to as i have 
it now which i only print the datetime of just the latest visit of that 
host and not all its visit datetimes.

I hope i made it clear what i want to achieve.

[toc] | [next] | [standalone]


#57170

FromΝίκος Αλεξόπουλος <nikos.gr33k@gmail.com>
Date2013-10-21 09:07 +0300
Message-ID<l42gak$c17$1@dont-email.me>
In reply to#57160
Στις 21/10/2013 2:30 πμ, ο/η Νίκος Αλεξόπουλος έγραψε:
> try:
>      cur.execute( '''SELECT host, city, useros, browser, ref, hits,
> lastvisit FROM visitors WHERE counterID = (SELECT ID FROM counters WHERE
> url = %s) ORDER BY lastvisit DESC''', page )
>      data = cur.fetchall()
>
>      for row in data:
>          (host, city, useros, browser, ref, hits, lastvisit) = row
>          lastvisit = lastvisit.strftime('%A %e %b, %H:%M')
>
>          print( "<tr>" )
>          for item in (host, city, useros, browser, ref, hits, lastvisit):
>              print( "<td><center><b><font color=white> %s </td>" % item )
> except pymysql.ProgrammingError as e:
>      print( repr(e) )
> ===========================================
>
> In the above code i print the record of the mysql table visitors in each
> row like this:  http://superhost.gr/?show=log&page=index.html
>
> Now, i wish to write the same thing but when it comes to print the
> 'lastvisit' field to display it in a <select></select> tag so all prior
> visits for the same host appear in a drop down menu opposed to as i have
> it now which i only print the datetime of just the latest visit of that
> host and not all its visit datetimes.
>
> I hope i made it clear what i want to achieve.


Any help would be appreciated.

[toc] | [prev] | [next] | [standalone]


#57172

FromBen Finney <ben+python@benfinney.id.au>
Date2013-10-21 17:51 +1100
Message-ID<mailman.1297.1382338312.18130.python-list@python.org>
In reply to#57170
Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> writes:

> Any help would be appreciated.

Please stop posting merely for grabbing attention.

If someone is going to answer, they'll answer. Don't annoy the forum
with pleas for attention.

-- 
 \            “Program testing can be a very effective way to show the |
  `\        presence of bugs, but is hopelessly inadequate for showing |
_o__)                              their absence.” —Edsger W. Dijkstra |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#57174

FromSteven D'Aprano <steve@pearwood.info>
Date2013-10-21 06:58 +0000
Message-ID<5264d096$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#57170
On Mon, 21 Oct 2013 09:07:17 +0300, Νίκος Αλεξόπουλος wrote:

>>      for row in data:
>>          (host, city, useros, browser, ref, hits, lastvisit) = row
>>          lastvisit = lastvisit.strftime('%A %e %b, %H:%M')
>>
>>          print( "<tr>" )
>>          for item in (host, city, useros, browser, ref, hits,
>>          lastvisit):
>>              print( "<td><center><b><font color=white> %s </td>" % item
>>              )
[...]
>> In the above code i print the record of the mysql table visitors in
>> each row like this:  http://superhost.gr/?show=log&page=index.html
>>
>> Now, i wish to write the same thing but when it comes to print the
>> 'lastvisit' field to display it in a <select></select> tag so all prior
>> visits for the same host appear in a drop down menu opposed to as i
>> have it now which i only print the datetime of just the latest visit of
>> that host and not all its visit datetimes.
>>
>> I hope i made it clear what i want to achieve.
> 
> 
> Any help would be appreciated.


Step 1:

Decide what counts as "the same visitor". Is it...?

- anyone with the same IP address?
- anyone with the same IP address and the same useros?
- anyone with the same IP address, the same useros, and the same browser?
- something else?


Step 2:

Scan the data, pulling out the record of the same unique visitor, and 
collecting the dates for that record. For example, you might write code 
like this:

# Untested, probably buggy.
visitors = []
records = []
for row in data:
    host, city, useros, browser, ref, hits, lastvisit = row
    if (host, ref) in visitors:
        # Seen this visitor before. Add the date to that record.
        record_no = visitors.index((host, ref))
    else:
        # New visitor, never seen before!
        visitors.append((host, ref))
        records.append([])
        record_no = len(visitors)
    records[record_no].append(lastvisit)
        

There may be more efficient ways to do the same thing, if you can think 
of a way to associate a list of values with a visitor key.

However you do it, by the time you have finished, you'll have a list of 
unique visitors, and a corresponding list containing the date of each of 
their visits. 

Step 3: When you go to build the table:

- identify which unique visitor this record represents
- look up the list of dates from that record
- build a <select></select> menu from that list of dates
- insert the menu into the table


and you're done.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#57179

FromΝίκος Αλεξόπουλος <nikos.gr33k@gmail.com>
Date2013-10-21 10:31 +0300
Message-ID<l42l8r$25o$1@dont-email.me>
In reply to#57174
Στις 21/10/2013 9:58 πμ, ο/η Steven D'Aprano έγραψε:
> On Mon, 21 Oct 2013 09:07:17 +0300, Νίκος Αλεξόπουλος wrote:
>
>>>       for row in data:
>>>           (host, city, useros, browser, ref, hits, lastvisit) = row
>>>           lastvisit = lastvisit.strftime('%A %e %b, %H:%M')
>>>
>>>           print( "<tr>" )
>>>           for item in (host, city, useros, browser, ref, hits,
>>>           lastvisit):
>>>               print( "<td><center><b><font color=white> %s </td>" % item
>>>               )
> [...]
>>> In the above code i print the record of the mysql table visitors in
>>> each row like this:  http://superhost.gr/?show=log&page=index.html
>>>
>>> Now, i wish to write the same thing but when it comes to print the
>>> 'lastvisit' field to display it in a <select></select> tag so all prior
>>> visits for the same host appear in a drop down menu opposed to as i
>>> have it now which i only print the datetime of just the latest visit of
>>> that host and not all its visit datetimes.
>>>
>>> I hope i made it clear what i want to achieve.
>>
>>
>> Any help would be appreciated.
>
>
> Step 1:
>
> Decide what counts as "the same visitor". Is it...?
>
> - anyone with the same IP address?
> - anyone with the same IP address and the same useros?
> - anyone with the same IP address, the same useros, and the same browser?
> - something else?

First let me show you the database insertion to start form there:

The definition of the same visitor in my case is basically a combination 
of they page the visitor tries to visit along with its hostname. At 
MySQL's definition iam implementing this as:

   unique index (counterID, host)


Up until now i was updating the record of the same visitor as follows:

============================
# if first time visitor on this page, create new record, if visitor 
exists then update record
cur.execute('''INSERT INTO visitors (counterID, host, city, useros, 
browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s)
		ON DUPLICATE KEY UPDATE city = %s, useros = %s, browser = %s, ref = 
%s, hits = hits + 1, lastvisit = %s''',
	(cID, host, city, useros, browser, ref, lastvisit, city, useros, 
browser, ref, lastvisit) )
=============================


Since now i have decided to have more records for the same visitor if 
i'm gonna save its history of visits, i'm thinking that i can no longer 
update the same unique visitor record but save many records related to 
the same visitor. so i use this:


=============================
# ~ DATABASE INSERTS ~
=============================
try:
	# if first time for webpage; create new record( primary key is 
automatic, hit is defaulted ), if page exists then update record
	cur.execute('''INSERT INTO counters (url) VALUES (%s) ON DUPLICATE KEY 
UPDATE hits = hits + 1''', page )
	# get the primary key value of the new added record
	cID = cur.lastrowid

	# if first time visitor on this page, create new record, if visitor 
exists then update record
	cur.execute('''INSERT INTO visitors (counterID, host, city, useros, 
browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s)''',
					   (cID, host, city, useros, browser, ref, lastvisit) )

	con.commit()
except pymysql.ProgrammingError as e:
	print( repr(e) )
	con.rollback()
=============================


Are we good up until this point as it concerns the database insertions?
If we are then we can discuss how to present the saved data.

[toc] | [prev] | [next] | [standalone]


#57181

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-21 08:31 +0100
Message-ID<mailman.1302.1382340905.18130.python-list@python.org>
In reply to#57170
On 21/10/2013 07:07, Νίκος Αλεξόπουλος wrote:
>
> Any help would be appreciated.

It is considered polite to wait for at least 24 hours before pinging. 
If waiting for this time isn't an option then paying for support is.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#57196

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-10-21 11:20 -0400
Message-ID<mailman.1313.1382369131.18130.python-list@python.org>
In reply to#57170
On Mon, Oct 21, 2013 at 3:31 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
>
> On 21/10/2013 07:07, Νίκος Αλεξόπουλος wrote:
>>
>>
>> Any help would be appreciated.
>
>
> It is considered polite to wait for at least 24 hours before pinging. If waiting for this time isn't an option then paying for support is.
>
> --
> Python is the second best programming language in the world.
> But the best has yet to be invented.  Christian Tismer
>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list


There is nothing about this question that relates to python.

1. You are asking in a very ill defined way how to collect all of the
times your visitors visited.  That's really a sql issue.
2. Your present html is written in a style that was considered ok in
1998.  CSS has replaced most of the stuff in your html.  This
highlights your lack of diligence to learn proper techniques in any
area -- not just python, or http, but now html.

My suggestion is that you should write the html by hand, and see if it
does what you want it to do.  Once you figure that out, go back to
your code and see if you can write python code that will mimic what
you wrote by hand.

You have been told in previous discussions, that writing html code in
your python code that performs back end logic is a TERRIBLE idea as it
leads to a mess in the code that is difficult to change even in minor
ways, and difficult to debug.  You have proven this to be true with
your long miandering debug pleas that you bring here.

best wishes Iron head


-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web