Printing a drop down menu for a specific field.

Steven D'Aprano steve at pearwood.info
Mon Oct 21 02:58:31 EDT 2013


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



More information about the Python-list mailing list