Location: PHPKode > scripts > Cup Scheduler > cup-scheduler/class.cupScheduler.ext.php
<?php
###################################################################################################
#  Cup Scheduler LITE v1.
#  Copyright (c) Martin "duno" Polacik <hide@address.com> 2009. All Rights Reserved.
#
#  Cup Scheduler LITE is free php script writen to generate soccer schadule for league cup.
#  Hope it will be usefull. This script is distributed in GNU GPL licence.
#  See the GNU General Public License for more details http://www.gnu.org/licenses/.
#
#  CHANGELOG
#  - added function array_is_valid, which checks if items count in array is devided by 2, and also checks if
#  number of teams is valid (valid counts 4, 8, 16, 32, 64 etc due to tournaments rulles)
#  - added variable allow_count by using allow_count=1 you can turn of tournaments rulles validation
#  - added variable show_error_msq
#  TO DO
#  - graphical view
###################################################################################################
Class CupScheduler
{
  //variables
  var $allow_count = 0;    //allow invalid teams count, values are 0 - no, 1 - yes. If you select
                           //teams count must be from $valid_counts array
  var $show_error_msg = 1; //display error message, 1 - display on 0 - dispay of
  var $error_msg;          //error message
###################################################################################################
  function generate_schedule($names)
  {
    $fixture = $this->custom_shuffle($names);
    $n_elements = array_chunk($fixture, 2);
    /*
      if your php knowledge  is better you can change next steps, all is done now .
      Fixtures was allready genereated and stored in array $fixture, and matches are stored in this array

      Array structure:

      Array
      (
          [0] => Array
          // fist match
              (
                  [0] => home team
                  [1] => away team
              )

          [1] => Array
          // second match
              (
                  [0] => home team
                  [1] => away team
              )
            etc
      )
     */
    $start = 0;
    while($start<count($n_elements))
    {
      /*
      you can use while and put data into db, or just print them
      $start_one = 0;
      $count_one = count($n_elements[$start]);
      while($start_one < $count_one)
      {
        echo $n_elements[$start][$start_one]." - ";
        $start_one++;
      }
      */
      echo $n_elements[$start][0]." vs ".$n_elements[$start][1]."<br>";
      $start++;
    }
  }
  function custom_shuffle($my_array = array())
  {
    $copy = array();
    while (count($my_array))
    {
      $element = array_rand($my_array);
      $copy[$element] = $my_array[$element];
      unset($my_array[$element]);
    }
    return $copy;
  }
  function array_is_valid($my_array)
  {
     /*
       at first we will validate if team count can be devided by 2, using module %
       this is necessary...
     */
     if((count($my_array)%2)==0)
     {
         /*
         valid count of items in array, class can shufle any, but if you creating a cup you can allow only
         this values or more :)
         */
         $valid_counts = array(4,8,16,32,64,128,254);
         if (in_array(count($my_array), $valid_counts))
         {
            $result = true;
         }
         else
         {

            if($this->allow_count==0)
            {
              $this->error_msg = "ERROR 001 - Number of teams must be power of number 2";
              $result = false;
            }
            elseif($this->allow_count==1)
            {
               $result = true;
            }
         }

       }
       else
       {
          $this->error_msg = "ERROR 002 - Number of teams must be divisible by 2";
          $result = false;
       }
       return $result;
  }
}
?>
Return current item: Cup Scheduler