Ho fatto, @bomberdini saresti così gentile di dare un giudizio al codice che ho scritto?
Questa è la classe che userò ogni volta che qualcosa necessita del sistema per i voti.
Codice PHP:
<?php
class Vote {
private $tablename;
private $name;
private $IP;
public function __construct($tablename, $name, $IP) {
$this->name = $name;
$this->tablename = $tablename;
$this->IP = $IP;
}
public function getVotes() {
require("connect.php");
$query = mysql_query("SELECT * FROM $this->tablename WHERE name='$this->name'");
$rows = mysql_num_rows($query);
mysql_close();
return $rows;
}
public function addVote() {
require("connect.php");
$query = mysql_query("SELECT * FROM $this->tablename WHERE IP='$this->IP'");
if (mysql_num_rows($query) > 0) {
echo "<p>You have already voted this program</p>";
return FALSE;
}
mysql_query("INSERT INTO $this->tablename (name, IP) VALUES ('$this->name', '$this->IP')");
mysql_close();
return TRUE;
}
}
?>
Questo è il codice che uso dove si da la possibilità di votare
Codice PHP:
<?php
require_once("vote.class.php"); //c'è tutto il percorso ma non lo scrivo perchè potrebbe essere spam
$vote = new Vote("votes", "javachat", $_SERVER["REMOTE_ADDR"]);
if ($_GET["voting"] == "true") {
if ($vote->addVote()) {
header("URL".$_SERVER["PHP_SELF"]); //anche qui c'è l'URL del sito che ho tolto
}
}
echo "<p>Votes: " . $vote->getVotes() . "</p>";
echo "<a href=\"". $_SERVER["PHP_SELF"]. "?voting=true\" >Vote</a>";
?>
C'è qualcosa che non va? Funzionare funziona, però se c'è un modo più elegante per scriverlo potresti dirmelo per favore?