Hi drw158
The SQL needed for your bracket is quite simple.
You may want to look at using PDO as a connector for your php code. Here is the first example:
Simple class or could be a function if rewritten to be used to instatiate an instance of the db:
PHP Code:
class Db {
protected $db;
function __construct() {
return $this->db = new PDO("mysql:host=localhost;dbname=bracket", "dbuser", "secret_password");
}
}
Sample Bracket class used to handle your SQL and any additional Bracket server side logic
PHP Code:
class Bracket {
protected $o_DB;
protected $sql;
public function _construct($o_Db) {
$this->o_DB = $o_Db;
}
protected function getRandomTeam(){
$this->sql =
"SELECT
<field names here>
FROM
teams
ORDER BY
RAND()
LIMIT
2";
return $this->runSQL();
}
protected function runSQL(){
if($this->sql){
$this->stmt = $this->o_DB->prepare($this->sql);
try {
if ($this->stmt->execute()) {
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
return($result);
}
} catch (PDOException $e) {
$error =
"Could not execute statement.\n errorCode: $sth->errorCode () \n"
. "errorInfo: " . join (", ", $sth->errorInfo ()) . "\n";
throw new Exception($error);
}
} else {
throw new Exception('$sql parameter needs to be set');
}
}
}
HTML page that ties in your Database and Bracket Class and where you handle your Bracket logic.
HTML Code:
<?php
require_once('./libs/db.php');
require_once('./libs/bracket.php');
$o_Db = new PdoDb();
$o_Bracket = new Bracket($o_Db);
?>
/* Do your HTML CSS */
/* Get your Random Teams */
<?php
$results = $o_Bracket->getRandomTeam();
?>
<form>
/* FOR LOOP to load your Data into your HTML form */
<?php
foreach($results as $outer){
foreach($outer as $key => $value){
/* Use the $key and $value properties to propagate HTML options boxes or whatever form element you want to use.
}
}
?>
</form>
You would continue to add different SQL handling methods in the Bracket Class.
Hope this helps.
Steve
Bookmarks