Need help translating these basic php ideas into working code

Hello, I recently posted a question on stackoverflow. Because sitepoint seems more specific, I thought I would post my next problem here.

Here is my stackoverflow post : http://stackoverflow.com/q/8582537/901834

You can read my proposition there.

You can see that someone posted an answer, and it looks great, but I have little php experience. I don’t have enough experience to translate his ideas into actual working code. I’m not asking for a fully developed working application, I just need a head start.

Here is his answer :

You’ll want a database. Start with a “teams” table, put in your 16 entries. Then, via PHP:

SELECT * 
FROM teams
ORDER BY RAND()
LIMIT 2

Now you’ll have to present the two teams to the user, and using an html form subiit or maybe some AJAX allow them to choose the winner. Now, make another table, say round_one_winners, and insert the user’s selection.

INSERT INTO round_one_winners
  (name)
VALUES
  ('team one')

Repeat x8. Then:

SELECT * 
FROM `round_one_winners`
ORDER BY RAND()
LIMIT 2

Repeat x4, using a round_two_winners table!

Specifically, I don’t know what he is talking about. I am not sure how to translate this into working code.
Any insight would be greatly appreciated!

Specifically, I don’t know what he is talking about. I am not sure how to translate this into working code.

You write a bit of PHP code that issues the SELECT query to choose the two teams (of which he gave you an example), and present them in a form for the user to pick one. You have another bit of code to receive the submission of that form and issue the INSERT query to save their answer (of which he gave you an example). You repeat this until the first bracket is complete, then move to the second bracket, then to the third, until the tournament is over. That’s some logic you have to put into code.

What part don’t you understand? If it’s “all of it”, then you need to pick up a beginners PHP book and read a few chapters. SitePoint sells one (Build Your Own Database Driven Website With PHP & MySQL) that covers everything involved.

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:


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


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.\
 errorCode: $sth->errorCode () \
" 
                . "errorInfo: " . join (", ", $sth->errorInfo ()) . "\
";
                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.


<?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