Code: Select all
<?php
class Match
{
// constant for open Match
const STATUS_OPEN = 0;
// constant for completed Match
const STATUS_COMPLETED = 1;
// constant for cancelled Match
const STATUS_CANCELLED = 2;
// constant for match that was result of error
const STATUS_ERROR = 3;
// match id
private $matchID = array();
// challenger of match
private $challenger = array();
// defender of match
private $defender = array();
// instance of database
protected $db;
/**
* class constructor
* @param $db database class instance
*/
public function __construct(Database $db)
{
// get instance of database
$this->db = $db;
}
/**
* returns match id array
* @return match id array
*/
public function getID()
{
return $this->matchID;
}
/**
* returns challenger array
* @return challenger array
*/
public function getChallengers()
{
return $this->challenger;
}
/**
* returns defender array
* @return defender array
*/
public function getDefenders()
{
return $this->defender;
}
/**
* adds the attack pattern from the challenger to the database
* @param $challenger's attack/block pattern as attack:block
*/
public function add($pattern)
{
// prepare the sql statement
$statement = $this->db->prepare('INSERT INTO ' . TABLE_MATCHES . ' VALUES (?,?)');
// bind the variables
$statement->bind_param('ss', $pattern, "");
// if the statement executed successfully
if ($statement->execute())
{
// get the number of results
$statement->bind_result($row);
// fetch the results
$statement->fetch();
// if the user was added successfully
if($row)
{
// return true
return true;
}
// otherwise return false
return false;
}
}
/**
* adds a match index to the database
* @param $challenger the person initiating the challenge
* @param $defender the person being challenged
*/
public function addIndex($challenger, $defender)
{
// prepare the sql statement
$statement = $this->db->prepare('INSERT INTO ' . TABLE_MATCHESINDEX . ' VALUES (?,?,?)');
// bind the variables
$statement->bind_param('sss', $challenger, $defender, self::STATUS_OPEN);
// if the statement executed successfully
if ($statement->execute())
{
// get the number of results
$statement->bind_result($row);
// fetch the results
$statement->fetch();
// if the user was added successfully
if($row)
{
// return true
return true;
}
// otherwise return false
return false;
}
}
/**
* counts the number of pending matches a player has
* @param $username username to check number of pending challenges
*/
public function CheckPendingMatches($username)
{
// prepare the sql statement to change the password
$statement = $this->db->prepare("SELECT id FROM " . TABLE_MATCHESINDEX . " WHERE status = ? AND challenger = ? OR defender = ? LIMIT 5");
// convert constant to variable
$status = self::STATUS_OPEN;
// bind the variables
$statement->bind_param('ssi', $username, $username, $status);
// if the statement executed successfully
if ($statement->execute())
{
// store the result
$statement->store_result();
// get the number of results
$totalPendingMatches = $statement->num_rows;
// return the total number of pending matches
return $totalPendingMatches;
}
// query didn't execute so return false
return false;
}
/**
* get the current matches the playerhas pending
* @username username to check for matches for
*/
public function getInfo($username)
{
// prepare the sql statement to change the password
$statement = $this->db->prepare("SELECT id, challenger, defender FROM " . TABLE_MATCHESINDEX . " WHERE status = ? AND challenger = ? or defender = ? LIMIT 5");
// convert constant to variable
$status = self::STATUS_OPEN;
// bing the variables
$statement->bind_param('ssi', $username, $username, $status);
// if the statement executed successfully
if ($statement->execute())
{
// get the number of results
$statement->bind_result($id, $challenger, $defender);
// fetch all the results
while($statement->fetch())
{
// set the match id
$this->id[] = $id;
// set the challenger
$this->challenger[] = $challenger;
// set the defender
$this->defender[] = $defender;
}
// so matches were found so return true
return true;
}
// the query failed so return false
return false;
}
/**
* unrolls the matches from the database
* @id id match id to unroll
*/
public function unRoll($id)
{
// prepare the sql statement
$statement = $this->db->prepare('DELETE FROM ' . TABLE_MATCHESINDEX . ' WHERE id = ? LIMIT 1');
// bing the variables
$statement->bind_param('i', $id);
// if the statement executed successfully
if ($statement->execute())
{
// get the number of results
$statement->bind_result($row);
// fetch the results
$statement->fetch();
// if the match was removed successfully
if($row)
{
// return true
return true;
}
// otherwise return false
return false;
}
}
}
?>
Code: Select all
<?php
// disable display or error messages and log them instead
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'On');
ini_set('log_errors', 'On');
ini_set('error_log', '/logs/error_log');
// include smarty library
require('libs/Smarty.class.php');
// include database library
require_once('libs/Database.class.php');
// include user library
require_once('libs/User.class.php');
// include user library
require_once('libs/Match.class.php');
// create instance of database class
$database = new Database();
// create instance of match class
$matches = new Match($database);
// create instance of smarty library
$smarty = new Smarty();
// set content header
header("Content-Type: " . USER_CONTENT_TYPE);
// start session
session_start();
// if the user isn't logged in
if(!isset($_SESSION['username'], $_SESSION['password'])) {
// redirect to the login form
header("Location: index.php");
}
// otherwise if the form hasn't been submitted
else
{
if($matches->CheckPendingMatches($_SESSION['username']) == 0)
{
// set the dialog title
$smarty->assign('title', 'Info');
// set the dialog message stating there are no pending matches
$smarty->assign('message', 'There are no pending matches');
// set the back url
$smarty->assign('backurl', 'showchallenges.php');
// display it
$smarty->display('messagedialog.tpl');
}
else
{
$rows = $matches->CheckPendingMatches($_SESSION['username']);
echo "rows = $rows<br />";
if($matches->getInfo($_SESSION['username']))
{
// set the challenger field
$smarty->assign('challengers', $matches->getChallengers());
// set the defender field'
$smarty->assign('defenders', $matches->getDefenders());
// set the match id
$smarty->assign('ids', '1');
// display it
$smarty->display('viewchallenges.tpl');
}
// set the dialog title
//$smarty->assign('title', 'Error');
// set the dialog message stating the feature isn't implemented'
//$smarty->assign('message', 'This feature isn\'t implemented yet');
// set the back url
//$smarty->assign('backurl', 'main.php');
// display it
//$smarty->display('messagedialog.tpl');
}
}
?>
http://adobe.ly/PaUFoc as you can see it's returning strange results, more then 2 and they aren't even correct.The database data is
Any assistance in this matter would be greatly appreciated.id, challenger, defender, status
1, admin, logster897, 0
2, pineapplelime, admin, 0