Skip to content

Instantly share code, notes, and snippets.

@evanpurkhiser
Forked from RSquaredSoftware/lotto.Class.php
Created May 3, 2012 05:38
Show Gist options
  • Save evanpurkhiser/2583559 to your computer and use it in GitHub Desktop.
Save evanpurkhiser/2583559 to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL | E_STRICT);
Class lotto{
private $_connection;
function __construct()
{
$auth = include("login.php");
try {
$this->_connection = new PDO($auth['server'], $auth['user'], $auth['password']);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public function init(){
$dbName = "lotto";
$dbh = $this->_connection;
//create the database
try{
$sth = $dbh->prepare("CREATE DATABASE IF NOT EXISTS $dbName");
$sth->execute();
}
catch (PDOException $e) {
die("Creating Database Error: ". $e->getMessage());
}
//create the pick3 table
try{
$dbh->exec("USE $dbName;");
$dbh->exec("CREATE TABLE IF NOT EXISTS pick3 (
date DATE NOT NULL,
pick3 INT(3),
PRIMARY KEY (date));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
//create the pick4 table
try{
$dbh->exec("USE $dbName;");
$dbh->exec("CREATE TABLE IF NOT EXISTS pick4 (
date DATE NOT NULL,
pick4 INT(4),
PRIMARY KEY (date));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
//create the mega table
try{
$dbh->exec("USE $dbName;");
$dbh->exec("CREATE TABLE IF NOT EXISTS mega (
date DATE NOT NULL,
b1 INT(2),
b2 INT(2),
b3 INT(2),
b4 INT(2),
b5 INT(2),
mb INT(2),
mp INT(1),
PRIMARY KEY (date));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
//create the powerball table
try{
$dbh->exec("USE $dbName;");
$dbh->exec("CREATE TABLE IF NOT EXISTS powerball (
date DATE NOT NULL,
b1 INT(2),
b2 INT(2),
b3 INT(2),
b4 INT(2),
b5 INT(2),
pb INT(2),
pp INT(1),
PRIMARY KEY (date));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
//create the classic ohio table
try{
$dbh->exec("USE $dbName;");
$dbh->exec("CREATE TABLE IF NOT EXISTS classic (
date DATE NOT NULL,
b1 INT(2),
b2 INT(2),
b3 INT(2),
b4 INT(2),
b5 INT(2),
b6 INT(2),
PRIMARY KEY (date));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
//create the ten-OH table
try{
$dbh->exec("USE $dbName;");
$dbh->exec("CREATE TABLE IF NOT EXISTS tenoh (
date DATE NOT NULL,
db1 INT(2),
db2 INT(2),
db3 INT(2),
db4 INT(2),
db5 INT(2),
db6 INT(2),
db7 INT(2),
db8 INT(2),
db9 INT(2),
db10 INT(2),
db11 INT(2),
db12 INT(2),
db13 INT(2),
db14 INT(2),
db15 INT(2),
db16 INT(2),
db17 INT(2),
db18 INT(2),
db19 INT(2),
db20 INT(2),
nb1 INT(2),
nb2 INT(2),
nb3 INT(2),
nb4 INT(2),
nb5 INT(2),
nb6 INT(2),
nb7 INT(2),
nb8 INT(2),
nb9 INT(2),
nb10 INT(2),
nb11 INT(2),
nb12 INT(2),
nb13 INT(2),
nb14 INT(2),
nb15 INT(2),
nb16 INT(2),
nb17 INT(2),
nb18 INT(2),
nb19 INT(2),
nb20 INT(2),
PRIMARY KEY (date));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
//create the rolling cash 5 table
try{
$dbh->exec("USE $dbName;");
$dbh->exec("CREATE TABLE IF NOT EXISTS rollingfive (
date DATE NOT NULL,
b1 INT(2),
b2 INT(2),
b3 INT(2),
b4 INT(2),
b5 INT(2),
PRIMARY KEY (date));");
}
catch (PDOException $e) {
die("Creating Table Error: ". $e->getMessage());
}
}
public function printpick3(){
$dbName = "lotto";
$dbh = $this->_connection;
if (isset($_GET['date']))
$datesort = !$_GET['date'];
else
$datesort = 'TRUE';
if($datesort)
$sort = 'DESC';
else
$sort = 'ASC';
try{
$dbh->exec("USE $dbName;");
$sth = $dbh->prepare("SELECT * FROM pick3 ORDER BY date $sort");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
die("Printing Data Error: ". $e->getMessage());
}
print_r($result);
echo "<table><tr><th><a name=date href='?date=".$datesort."'>Date</a></th><th><a name=nums href='#'>Pick 3</a></th><th></th></tr>";
foreach($result as $r){
echo "<tr><td>".$r[date]."</td><td>".$r[pick3]."</td>";}
echo "</table>";
}
public function parse($filename)
{
// Get the rows of data
$data = array_map('trim', explode("\n", file_get_contents($filename)));
// array('date' => array('pick1' => val, 'pick2' => val, ...))
$picks = array();
// Get the names of each of the different picks
$pick_names = array_slice(explode(',', array_shift($data)), 1);
foreach ($data as $pick_row)
{
$pick_data = explode(',', $pick_row);
// Get the date the picks happend
$date = array_shift($pick_data);
// Combine the pick data and pick names into an assoicative array.
// Also use array filter to ignore empty values
$values = array_filter(array_combine($pick_names, $pick_data));
// If there are NO picks for this date, just skip it
if (empty($values))
continue;
// Add the date to the array with the pick name and value
$picks[$date] = $values;
}
return $picks;
}
public function insertParsed(array $data, $table){
$dbName = "lotto";
$dbh = $this->_connection;
foreach ($data as $datekey => $drawing) {
$insert = "INSERT IGNORE INTO ".$table." SET date ='".$datekey."', ";
foreach ($drawing as $ballkey => $number) {
$insert .= "".$ballkey." = ".$number.", ";
}
$insert = substr($insert, 0, -2).";";
try{
$dbh->exec("USE $dbName;");
$sth = $dbh->prepare("$insert");
$sth->execute();
}
catch (PDOException $e) {
die("Printing Data Error: ". $e->getMessage());
}
}
}
public function display(array $games){
$dbName = "lotto";
$result = array();
if(in_array("none", $games))
return;
$dbh = $this->_connection;
if (isset($_GET['date']))
$datesort = !$_GET['date'];
else
$datesort = 'TRUE';
if($datesort)
$sort = 'DESC';
else
$sort = 'ASC';
foreach ($games as $game)
{
// The default values array.. all empty
$default = array_combine($games, array_fill(0, count($games), '-'));
try
{
$dbh->exec("USE $dbName;");
$sth = $dbh->prepare("SELECT * FROM $game ORDER BY date $sort");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $row)
{
// Get the date for this value
$date = array_shift($row);
// Get the comma separated values for this dates pick
$value = implode(',', $row);
// Get the other original values for the date
$previous = isset($result[$date]) ? $result[$date] : $default;
// Add these new game results to the array
$result[$date] = array_merge($previous, array($game => $value));
}
}
catch (PDOException $e)
{
die("Printing Data Error: ". $e->getMessage());
}
krsort($result);
}
?>
<table>
<thead>
<tr>
<th>Date</th>
<? foreach (current($result) as $column_name => $values): ?>
<th><?= $column_name; ?></th>
<? endforeach; ?>
</tr>
</thead>
<tbody>
<? foreach ($result as $date => $results): ?>
<tr>
<td><?= $date; ?></td>
<? foreach ($results as $game => $balls): ?>
<td><?= $balls; ?></td>
<? endforeach; ?>
</tr>
<? endforeach; ?>
</tbody>
</table>
<?php
}
public function tables(){
$dbName = "lotto";
$dbh = $this->_connection;
try {
$sth = $dbh->prepare("SHOW TABLES FROM $dbName");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
echo "<ul>";
foreach($data as $table)
foreach ($table as $value){
echo "<li><a href='?game=$value'>$value</a></li>";
}
echo "</ul>";
}
public function checknum($game){
$dbName = "lotto";
$dbh = $this->_connection;
try {
$dbh->exec("USE $dbName;");
$sth = $dbh->prepare("SHOW COLUMNS FROM $game");
$sth->execute();
$numbers = $sth->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$columns = array();
array_shift($numbers);
echo ucfirst($game)."<form action='' method='POST'>";
foreach ($numbers as $number) {
$temp = $number["Field"];
echo $temp." <input maxlength='2' size='2' type='text' id='$temp' name='$temp' value='' /> ";
array_push($columns, $number["Field"]);
}
echo "<input type='submit' name='check' value='Check' /></form>";
//if the user entered number to be checked
if (isset($_POST)){
//the last element is the submit button, get rid of it
array_pop($_POST);
//temporary array to hold each ball IN (number, number) etc so they can be joined by an " OR "
$innerselect = array();
//cycle through each ball name, joining them by a comma
foreach ($columns as $column)
array_push($innerselect, $column." IN (".implode(", ", $_POST).") ");
$select = "SELECT * FROM ".$game." WHERE ".implode(" OR ", $innerselect);
$result = array();
$previous = array();
try
{
$dbh->exec("USE $dbName;");
$sth = $dbh->prepare($select);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
die("Printing Data Error: ". $e->getMessage());
}
?>
<h3> Days that one or more of your numbers have been drawn:</h3>
<table>
<thead>
<tr>
<th>Date</th>
<? foreach ($data as $column_name => $values)
foreach ($values as $column_name => $value) ?>
<th><?= $column_name; ?></th>
</tr>
</thead>
<tbody>
<? foreach ($data as $date => $results): ?>
<tr>
<td><?= array_shift($results); $date; ?></td>
<? foreach ($results as $game => $balls): ?>
<td><?= $balls; ?></td>
<? endforeach; ?>
</tr>
<? endforeach; ?>
</tbody>
</table>
<?php
// print_r($data);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment