Printing a drop down menu for a specific field.

Nick the Gr33k nikos.gr33k at gmail.com
Sat Oct 26 20:11:24 EDT 2013


Στις 27/10/2013 2:52 πμ, ο/η Nick the Gr33k έγραψε:
> Ah foun it had to change in you code this line:
>              key = host, city, useros, browser, ref
>
> to this line:
>
>              key = host, city, useros, browser
>
> so 'ref' wouldnt be calculated in the unique combination key.
>
> I'am still trying to understand the logic of your code and trying to
> create a history list column for the 'referrers'
>
> I dont know how to write it though to produce the sam

Iam trying.

Ah foun it had to change in you code this line:
             key = host, city, useros, browser, ref

to this line:

             key = host, city, useros, browser

so 'ref' wouldnt be calculated in the unique combination key.

I'am still trying to understand the logic of your code and trying to 
create a history list column for the 'referrers'

I dont know how to write it though to produce the same output for referrers.

The bast i came up with is:

[code]
def coalesce( data ):
		newdata = []
		seen = {}
		for host, city, useros, browser, ref, hits, visit in data:
			# Here i have to decide how to group the rows together.
			# I want an html row for every unique combination of (host, city, 
useros, browser) and that hits should be summed together.
			key = host, city, useros, browser
			if key not in seen:
				newdata.append( [host, city, useros, browser, [ref], hits, [visit]] )
				seen[key] = len( newdata ) - 1		# Save index (for 'newdata') of this 
row.
			else:		# This row is a duplicate row with a different visit time.
				rowindex = seen[key]
				newdata[rowindex][4].append( ref )
				newdata[rowindex][5] += hits
				newdata[rowindex][6].append( visit )
		return newdata

		
	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()

	
	newdata = coalesce( data )
	for row in newdata:
		(host, city, useros, browser, refs, hits, visits) = row
		# Note that 'ref' & 'visits' are now lists of visit times.
		
		print( "<tr>" )
		for item in (host, city, useros, browser):
			print( "<td><center><b><font color=white> %s </td>" % item )
			
		print( "<td><select>" )
		for n, ref in enumerate( refs ):
			if n == 0:
				op_selected = 'selected="selected"'
			else:
				op_selected = ''
		print( "<option %s>%s</option>" % (op_selected, ref) )
		print( "</select></td>" )

		for item in (hits):
			print( "<td><center><b><font color=white> %s </td>" % item )
			
		print( "<td><select>" )
		for n, visit in enumerate( visits ):
			visittime = visit.strftime('%A %e %b, %H:%M')
			if n == 0:
				op_selected = 'selected="selected"'
			else:
				op_selected = ''
			print( "<option %s>%s</option>" % (op_selected, visittime) )
		print( "</select></td>" )
		
		print( "</tr>" )
[/code]

But this doesnt work correctly for refs and also doenst not print for 
some reason the hits and visit colums.



More information about the Python-list mailing list