Skip to content

Instantly share code, notes, and snippets.

@rahims
Created May 3, 2011 05:30
Show Gist options
  • Save rahims/952860 to your computer and use it in GitHub Desktop.
Save rahims/952860 to your computer and use it in GitHub Desktop.
<?php
class DB {
const DB_NAME = 'votes.sqlite';
protected $db;
function __construct() {
$this->db = new PDO('sqlite:'.self::DB_NAME);
}
function init() {
// Create two tables, one to store the teams being voted on and their vote counts (teams) and one to store the people that have voted (voters).
$this->db->exec('CREATE TABLE IF NOT EXISTS teams (id INTEGER PRIMARY KEY, name TEXT, votes INTEGER);');
$this->db->exec('CREATE TABLE IF NOT EXISTS voters (id INTEGER PRIMARY KEY, phone_number TEXT, voted_for INTEGER);');
}
function add_team($name) {
// Check to make sure the team name doesn't already exist
$stmt = $this->db->prepare('SELECT COUNT(*) FROM teams WHERE name=?');
$stmt->execute(array($name));
// If not, insert it
if ($stmt->fetchColumn() == 0)
{
$stmt = $this->db->prepare('INSERT INTO teams (name, votes) VALUES (?, 0)');
$stmt->execute(array($name));
}
}
function get_teams() {
$result = $this->db->query('SELECT * FROM teams');
foreach ($result as $row)
{
$team['id'] = $row['id'];
$team['name'] = $row['name'];
$team['votes'] = $row['votes'];
$teams[] = $team;
}
return $teams;
}
function save_vote($phone_number, $voted_for) {
// Just the digits, please
$phone_number = preg_replace('/\D/', '', $phone_number);
// Check to see if person has already voted
$stmt = $this->db->prepare('SELECT COUNT(*) FROM voters WHERE phone_number=?');
$stmt->execute(array($phone_number));
// If not, save their vote
if ($stmt->fetchColumn() == 0)
{
// Save voter
$stmt = $this->db->prepare('INSERT INTO voters (phone_number, voted_for) VALUES (?, ?)');
$stmt->execute(array($phone_number, $voted_for));
// Update vote count
$stmt = $this->db->prepare('UPDATE teams SET votes = votes + 1 WHERE id=?');
$stmt->execute(array($voted_for));
return 'Thank you, your vote has been recorded';
}
else {
return 'Sorry, you can only vote once.';
}
}
}
?>
<?php
require_once('db.php');
$db = new DB();
// Grab all the teams (and their vote counts) from the database
$teams = $db->get_teams();
echo '<ul>';
// Loop through each team and display how many votes they got
foreach ($teams as $team)
{
echo '<li>'.$team['name'].': '.$team['votes'].' votes</li>';
}
echo '</ul>';
?>
<?php
require_once('db.php');
header('Content-type: text/xml');
echo '<Response>';
$phone_number = $_REQUEST['From'];
$team_number = (int) $_REQUEST['Body'];
// If we've got good data, save the vote
if ( (strlen($phone_number) >= 10) && ($team_number > 0) )
{
$db = new DB();
// save_vote will check to see if the person has already voted
$response = $db->save_vote($phone_number, $team_number);
}
else {
// Otherwise, give the user an example of how to vote
$response = 'Sorry, I didn\'t understand that. Text the team number to vote. For example, texting 1 will vote for Team 1.';
}
// Send an SMS back to the person that voted letting them know that their vote was saved, or that there was an error of some sort
echo '<Sms>'.$response.'</Sms>';
echo '</Response>';
?>
<?php
// Sets up the database. Only needs to be run once. After that you can delete this file.
require_once('db.php');
$db = new DB();
// Create the database tables
$db->init();
// Add some teams
$db->add_team('Team 1');
$db->add_team('Team 2');
$db->add_team('Team 3');
echo 'Database created and teams added';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment