Undefined index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Undefined index

Post by Todlerone »

Hello all, thank-you in advance for any help/suggestions. In my original working code for a dynamic baseball league standings I used numerical indexed arrays. I'm currently trying to convert my vast overkilled code to functions and get some readability into it. I eliminated a step whereby I converted my schedule from the original "HIT", "ACE", etc to 0, 1, 2 inorder to make populating my arrays easier. Now in trying to do the same with my associative arrays I keep getting "Undefined index" when I run from localhost. The problem starts when it tries to do anything with $standings in the game1 function (the line....//game_vis games played+1 (game1)) Here is my code.

Code: Select all

<?php
$stats = find_scores();
 
foreach($stats as $stat): 
    if ($stat['game_status1']=="C"){
        game1($stat['game_vis'], $stat['game_home'], $stat['game_vis1'],$stat['game_home1']);
    }elseif ($stat['game_status1']=="R1"){
        $standings[$stat['game_vis']][10]+=1;
        $standings[$stat['game_home']][10]+=1;
    }elseif ($stat['game_status1']=="FV1"){
        $standings[$stat['game_vis']][9]+=1;
        game1($stat['game_vis'], $stat['game_home'], $stat['game_vis1'],$stat['game_home1']);
    }elseif ($stat['game_status1']=="FH1"){
        $standings[$stat['game_home']][9]+=1;
        game1($stat['game_vis'], $stat['game_home'], $stat['game_vis1'],$stat['game_home1']);
    }elseif ($stat['game_status1']=="CANC"){
        $standings[$stat['game_vis']][11]+=1;
        $standings[$stat['game_home']][11]+=1;
    }
endforeach;
 
 
function db_connect()
  {
        $connection = mysql_pconnect('*****','********','********');
        if(!$connection){
            return false;
        }
        if(!mysql_select_db('*********')){
            return false;
        }
            return  $connection;
  }
    
//------------------------------------------------------------------------------------------------------------------------------------------------------    
    function db_result_to_array($result)
    {
      $res_array = array();
        
        for ($count=0;  $row = mysql_fetch_array($result, MYSQL_ASSOC) ; $count++)
        {
          $res_array[$count] = $row;
        }
        
        return $res_array;
    }
    
//------------------------------------------------------------------------------------------------------------------------------------------------------        
    function find_scores()
    {
        db_connect();
      
        $query = "SELECT game_vis, game_home, game_vis1, game_home1, game_vis2,
        game_home2, game_status1, game_status2 FROM schedule08 RIGHT JOIN
        scores08 ON schedule08.game_id= scores08.game_id";
        $result = mysql_query ($query);
        $number_of_results = mysql_num_rows($result);
        
        if($number_of_results == 0)
      {
            return false;
        }
        
        $result = db_result_to_array($result);
        
        return $result;
    }
//------------------------------------------------------------------------------------------------------------------------------------------------------        
    function game1($vis, $home, $vis1, $home1)
    {
    global $outcome, $overall, $standings;
    $standings=array();
    $outcome=array();
    $overall=array();
    
    $v=sizeof($outcome[$vis]);      //used for  the visitor team streak
    $h=sizeof($outcome[$home]);     //used for  the home team streak
    $standings[$vis][1]+=1;         //game_vis games played+1 (game1)
    $standings[$home][1]+=1;            //game_home games played+1 (game 1)
    $standings[$vis][6]+=$vis1; //game_vis games runs for (game1)
    $standings[$home][6]+=$home1;   //game_home games runs for (game1)
    $standings[$vis][7]+=$home1;    //game_vis games runs against (game1)
    $standings[$home][7]+=$vis1;    //game_home games runs against (game1)
    if (vis1>home1){                //game1 loop (vis1>home1)
        $standings[$vis][2]+=1;     //game_vis wins+1   (game1)
        $standings[$home][3]+=1;        //game_home losses+1 (game1)
        $overall[$vis][$home]+=1;   //head to head (game_vis win+1 against game_home)
        $outcome[$vis][$v]="W";     //game_vis "win" outcome for game1
        $outcome[$home][$h]="L";        //game_home "Loss" outcome for loser
    }elseif (vis1<home1){           //game1 vis1<home1
        $standings[$vis][3]+=1;     //vis loses+1
        $standings[$home][2]+=1;        //home wins+1
        $overall[$home][$vis]+=1;   //head to head (home win-vis lose)+1
        $outcome[$vis][$v]="L";     //game_vis1 loser
        $outcome[$home][$h]="W";        //game_home1 winner 
    }else{                              //game1 vis1=home1
        $standings[$vis][4]+=1;     //vis team ties+1
        $standings[$home][4]+=1;        //home team ties+1
        $outcome[$vis][$v]="T";     //game_vis tie
        $outcome[$home][$h]="T";        //game_home tie
    }                                   //end game1 loop
    }
 
jthayne
Forum Newbie
Posts: 5
Joined: Fri Jan 09, 2009 10:51 am

Re: Undefined index

Post by jthayne »

The problem is that you are declaring your variables in the function as well as setting them as global. You need to move the variable declaration to the beginning of your code.

Ref: http://www.php.net/manual/en/language.v ... .php#85203
Post Reply