SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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.

    Code:
    INSERT INTO round_one_winners
      (name)
    VALUES
      ('team one')
    Repeat x8. Then:

    Code:
    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!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)

    Post

    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
    ictus==""


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •