omskrivning fra txt til SQL
hejsa eksperter jeg har fået følgende kode af en ven, mit problem er scriptet bruger txt filer, og jeg vil meget gerne have det til at bruge en SQL database, så ville blive meget glad hvis nogen gad omskrive det til at bruge SQL istedet for txt.kode:
<?
$DATA_FILE = "pollData.txt";
$QUESTION = "Should Mike have a Poll?";
$ANSWERS = array("Yupperdoodle!", "Nopers", "I don't care");
$IMG_DIR = "./vote";
$voterHosts = array();
$voterCount = 0;
$votes = array();
$answerKey = count($ANSWERS) - 1;
for($i=0;$i<=$answerKey;$i++) $votes[$i] = 0;
$fp = @fopen($DATA_FILE,"r");
if($fp) {
while(!feof($fp)) {
$inData = fgets($fp, 4096);
$data = split(":",$inData);
if(count($data) != 3) continue;
$voterHosts[$voterCount++] = $data[0];
$votes[$data[1]]++;
}
fclose($fp);
}
if(isset($_POST["vote"]) && !in_array($_SERVER["REMOTE_ADDR"], $voterHosts)) {
if(is_numeric($_POST["vote"]) && $_POST["vote"] < $answerKey) {
$fp = fopen($DATA_FILE,"a");
fputs($fp, $_SERVER["REMOTE_ADDR"].":".$_POST["vote"].":\r\n");
fclose($fp);
$voterHosts[$voterCount++] = $_SERVER["REMOTE_ADDR"];
$votes[$_POST["vote"]]++;
}
}
function displayResults() {
GLOBAL $ANSWERS, $IMG_DIR, $voterCount, $votes;
echo "<h2>PHP Poll vote results :</h2>";
echo "<TABLE CELLSPACING=2 CELLPADDING=1 BORDER=1>";
echo "<tr><th>What</th><th>Percentage</th><th>Votes</th></tr>";
foreach($ANSWERS as $key => $ANSWER) {
$percent = $votes[$key] * 100 / $voterCount;
echo "<tr><td> ".$ANSWER." </td><td><img height=9 src=\"".$IMG_DIR."/vote_left.gif\"><img height=9 width=\"".floor($percent)."\" src=\"".$IMG_DIR."/vote_middle.gif\"><img height=9 src=\"".$IMG_DIR."/vote_right.gif\"> ".number_format($percent,1)." % </td><td>".$votes[$key]."</td></tr>";
}
echo "</TABLE><br>";
}
function displayPoll() {
GLOBAL $ANSWERS, $QUESTION;
echo "<FORM METHOD=\"POST\">\n";
echo "<TABLE WIDTH=100% BORDER=1><TR><TD><TABLE WIDTH=\"100%\" BORDER=0>\n";
echo "<TR><TH>".$QUESTION."</TH></TR>\n";
foreach($ANSWERS as $key => $ANSWER) {
echo "<TR><TD align=\"center\"><INPUT TYPE=\"radio\" NAME=\"vote\" VALUE=\"".$key."\"> ".$ANSWER."</TD></TR>\n";
}
echo "<TR><TD align=\"center\"><INPUT TYPE=\"Submit\" NAME=\"voteButton\" VALUE=\" Vote \"></TD></TR>\n";
echo "<TR><TD align=\"center\"><INPUT TYPE=\"Submit\" NAME=\"result\" VALUE=\" See Result \"></TD></TR>\n";
echo "</TABLE></TD></TR></TABLE></FORM>";
}
if(in_array($_SERVER["REMOTE_ADDR"], $voterHosts) || isset($_POST["result"])) displayResults();
else displayPoll();
?>