#!/usr/bin/perl -w

#
# Puts up a menu of nations from the nations database.
# Lets you pick a set of nation
# Puts up a new menu, with those choices highlighted

use CGI;
use DBI;

$query = new CGI;

# print header
print "Status: 200 Content follows\n";
print "Content-type: text/html\n\n";

# Print the start of the table
print "<table columns=4 border=1>\n";
print "<form method=post action=\"hard-data-transfer.cgi\">\n";
print "<input type=submit value=\"Send it\">\n";


# Print either an old or a new menu
if (defined($query->param("iterations"))) {
	&OldMenu;
}
else {
	&NewMenu;
}




sub NewMenu {
	print "<input type=hidden name=iterations value=1>\n";
	$dbh = DBI->connect("DBI:mysql:nations", "randy", "password");
	$sth = $dbh->prepare("select name, code from nations order by name");
	$sth->execute();
	while (($name, $code) = $sth->fetchrow_array()) {
		print "<tr>\n";
		print "<td>$name</td>\n";
		print "<td>$code</td>\n";
		print "<td><input type=radio name=$code value=1>One</td>\n";
		print "<td><input type=radio name=$code value=2>Two</td>\n";
		print "</tr>\n";
	}
	print "</table>\n";
	$dbh->disconnect;
}

sub OldMenu {
	$iterations = $query->param("iterations");
	print "Can't you get it right.  It's been $iterations tries!!\n";
	$iterations++;
	print "<input type=hidden name=iterations value=$iterations>\n";
        $dbh = DBI->connect("DBI:mysql:nations", "randy", "password");
        $sth = $dbh->prepare("select name, code from nations order by name");
        $sth->execute();
        while (($name, $code) = $sth->fetchrow_array()) {
                print "<tr>\n";
                print "<td>$name</td>\n";
                print "<td>$code</td>\n";
		if ($query->param("$code") eq "1") {
                	print "<td><input type=radio name=$code value=1 checked=yes>One</td>\n";
                	print "<td><input type=radio name=$code value=2 >Two</td>\n";
		}
		elsif ($query->param("$code") eq "2") {
			print "<td><input type=radio name=$code value=1 >One</td>\n";
                        print "<td><input type=radio name=$code value=2 checked=yes >Two</td>\n";
		}
		else {
                        print "<td><input type=radio name=$code value=1 >One</td>\n";
                        print "<td><input type=radio name=$code value=2 >Two</td>\n";
		}
                print "</tr>\n";
        }
        print "</table>\n";
        $dbh->disconnect;
}






