<?

$html_top = "<HTML><HEAD><TITLE>Finest Artist</TITLE></HEAD>
             <BODY BGCOLOR=black TEXT=white 
             VLINK=606060 LINK=909090>";

$f = "artists.db";

if(file_exists($f)) $mode = "w";
else                $mode = "c";

if(!($db = dba_open($f, $mode,"gdbm"))){
       print("problem opening  $db");
       exit();
}
if($mode == "c") dba_replace("total",0,$db);

# Person typed a new artist name
if( strlen($other)>1 )  $artist = $other;

# Prevent people from voting again until someone else has voted
$last = dba_fetch("last",$db);
print("last = $last and remoteaddr = $REMOTE_ADDR<p>");
dba_replace("last",$REMOTE_ADDR,$db);

if($last == $REMOTE_ADDR){
     print("$html_top<h3>You already voted.</h3>");
}
else if( !empty($artist) 
    && $artist != "total" 
    && $artist != "last" ){
    if(dba_exists($artist,$db)){
        $cur = dba_fetch($artist,$db);
    }
    else $cur = 0;
    dba_replace($artist,($cur+1),$db);
    $tot = dba_fetch("total",$db);
    dba_replace("total",($tot+1),$db);
        print("$html_top<P>[<A HREF=index.php>Back</A>]<P>");
}
else{
  print("$html_top<TABLE width=400><TR><TD><BIG>Mirror, mirror, on the wall, 
         who's the finest artist of them all?</BIG></TD></TR></TABLE>
         <FORM ACTION=index.php><SELECT SIZE=5 NAME=artist>");
  $key = dba_firstkey($db);
  do{
     if($key != "total" && $key != "last") print("<option>$key\n");
  }while($key = dba_nextkey($db));
  print("</SELECT><BR>Other:<INPUT NAME=other><BR><INPUT type=submit VALUE=Vote></FORM>");

}

#Show standings
$key = dba_firstkey($db);
$total = dba_fetch("total",$db);
do{
    if($key != "total" && $key != "last"){
        $cur = dba_fetch($key,$db);
        $wid = (int)(100*($cur/$total));
        print("$key<BR><TABLE BGCOLOR=001080 WIDTH=$wid%>
               <TD><SMALL>$wid %</SMALL></TD></TABLE>");
   }
}while($key = dba_nextkey($db));
dba_close($db);

?>