]> git.sesse.net Git - wloh/commitdiff
Factor out division-fetching code.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 25 Mar 2012 15:12:21 +0000 (17:12 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 25 Mar 2012 15:12:21 +0000 (17:12 +0200)
www/index.pl

index 06babf6e22e66e7944992dbf833c34e15021c573..c63ae099bf36d21b3a5b0327300346262ffb8e9c 100755 (executable)
@@ -47,6 +47,21 @@ sub get_max_season {
        return $ref->{'max_sesong'};
 }
 
+sub get_divisions {
+       my ($dbh, $season) = @_;
+
+       my @divisions = ();
+
+       my $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
+       $q->execute($season);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               push @divisions, $ref->{'divisjon'};
+       }
+
+       return @divisions;
+}
+
 sub make_table {
        my ($lowest_division, $used_ratings, $used_cov) = @_;
 
@@ -191,31 +206,23 @@ printf <<"EOF", $match_stddev;
 EOF
 
 my $season = get_max_season($dbh);
+my @divisions = get_divisions($dbh, $season);
+$division = 1 if (!grep { $_ == $division } @divisions);
+my $max_division = $divisions[$#divisions];
 
 print <<"EOF";
      <p>Divisjon:
         <select name="divisjon" onchange="form.submit();">
 EOF
 
-$q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
-$q->execute($season);
-
-my $found_division = 0;
-my $max_division;
-
-while (my $ref = $q->fetchrow_hashref) {
-       my $d = $ref->{'divisjon'};
+for my $d (@divisions) {
        if ($d == $division) {
                print "        <option value=\"$d\" selected=\"selected\">$d</option>\n";
-               $found_division = 1;
        } else {
                print "        <option value=\"$d\">$d</option>\n";
        }
-       $max_division = $d;
 }
 
-$division = 1 if (!$found_division);
-
 print <<"EOF";
         </select>
         Avdeling: