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

index c63ae099bf36d21b3a5b0327300346262ffb8e9c..ef2e15eeacad5f042a1c3ba6515c302f4ba9ecb4 100755 (executable)
@@ -62,6 +62,21 @@ sub get_divisions {
        return @divisions;
 }
 
+sub get_subdivisions {
+       my ($dbh, $season, $division) = @_;
+
+       my @subdivisions = ();
+
+       my $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
+       $q->execute($season, $division);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               push @subdivisions, $ref->{'avdeling'};
+       }
+
+       return @subdivisions;
+}
+
 sub make_table {
        my ($lowest_division, $used_ratings, $used_cov) = @_;
 
@@ -229,23 +244,17 @@ print <<"EOF";
         <select name="avdeling" onchange="form.submit();">
 EOF
 
-$q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
-$q->execute($season, $division);
-
-my $found_subdivision = 0;
+my @subdivisions = get_subdivisions($dbh, $season, $division);
+$subdivision = 1 if (!grep { $_ == $subdivision } @subdivisions);
 
-while (my $ref = $q->fetchrow_hashref) {
-       my $sd = $ref->{'avdeling'};
+for my $sd (@subdivisions) {
        if ($sd == $subdivision) {
                print "        <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
-               $found_subdivision = 1;
        } else {
                print "        <option value=\"$sd\">$sd</option>\n";
        }
 }
 
-$subdivision = 1 if (!$found_subdivision);
-
 print <<"EOF";
         </select>
         <input type="submit" value="Vis" />