#!/usr/bin/python2.2 -u
import sys
import cgitb; cgitb.enable()
import MySQLdb
from random import choice, shuffle

sys.stderr = sys.stdout
print "Content-Type: text/html\n\n"

conn = MySQLdb.connect(user="xxx",passwd="xxx") # actual values concealed
conn.select_db("usa")
curr = conn.cursor()
numrecs = curr.execute("select statename, postal, capital from states;")
recs = curr.fetchall()
conn.close()

cities = []
states = []

for record in recs:
    statename, postal, capital = record 
    cities.append(capital)
    states.append((statename, postal))

print "<html>"
print "<head><title>Quiz Demo</title></head>"
print "<body>"
print '<table width="400" border="0" align="center"><tr><td>'

randstate = choice(states)
print "<p>What is the capital of %s? (%s)</p>" % randstate

print """
<form name="quiz" action="checkanswer.cgi" method="POST">
<select name="capitals">
"""

shuffle(cities)

for city in cities:
    print '<option value="%s">%s' % (city, city)

print "</select>"
print '<input type="submit" value="Submit">'
print '<input name="thestate" type="hidden" value="%s">' % randstate[1]
print "</form>"
print "<br/>"
print "<br/>"

print "<a href='../ocn/geoquiz.html'>Return to geoquiz home page</a>"

print "</td></tr></table>"
print "</body>"
print "</html>"
# code highlighted using py2html.py version 0.8