Jump to content

How can I use isset() to solve this Undefined Offset error?


PHP_Idiot

Recommended Posts

Hi Freaks

 

I have a large script that pulls data from my db and does some calculations then presents the results, however, I've recently noticed my error log is HUGE!

And it's all because of this: 

Notice: Undefined offset: 8 in .../includes/finalists.php on line 98

 

This is line 98:

$arr[] =  array(1 => $r[1], 2 => $r[2], 3 => $r[3], 4 => $r[4], 5 => $r[5], 6 => $r[6], 7 => $r[7], 8 => $r[8]);

 

Now $r[7] & $r[8[ refer to checkboxes that are often left blank, so I figured an isset() here would solve the issue, but I can't for the life of me get it too work.

 

Oh, and the username isn't a lie, I really do suck at this!

 

Thanks for any help :)

Link to comment
Share on other sites

Wow you guys are quick off the mark, ok here is the full script (i've excluded the previous bits as they're not important to this bit)

 

This is used to calculate league standings and chips stacks for a poker league. stored in the database are all the results for every player at every venue for each date played, this is extracted from the db using this:

 

Nearly forgot... Results.MemCard, Results.EarlyReg are the two checkboxes, everything else is always filled in but these could be left blank.

$top40 = "SELECT Player.MembershipNo , Player.FirstName , Player.LastName,  Venue.VenueName as Venue, Results.MemCard, Results.EarlyReg, Position.Points as Venue_Points, Results.Date
FROM Position , Player , Results , Venue 
WHERE Player.MembershipNo =Results.MembershipNo 
AND Results.Position =Position.Position 
AND Venue.VenueID =Results.VenueID 
AND Results.Date 
BETWEEN '$BeginDate' AND '$EndDate' 
ORDER BY MembershipNo, Venue"; //query pulls all info required to calculate final chip stack over the current season

$result = mysql_query($top40) or die("Couldn't execute query because: ".mysql_error()); //creates variable holding query results



$AvePlayers = "SELECT Venue.VenueName, COUNT( Results.MembershipNo ) , COUNT( DISTINCT Results.Date ) , cast( coalesce( COUNT( Results.MembershipNo ) / COUNT( DISTINCT Results.Date ) ) AS decimal( 10, 1 ) ) AS 'Average'
FROM Position, Player, Results, Venue
WHERE Player.MembershipNo = Results.MembershipNo
AND Results.Position = Position.Position
AND Venue.VenueID = Results.VenueID
AND Results.Date
BETWEEN '$BeginDate'
AND '$EndDate'
GROUP BY Venue.VenueName
ORDER BY Average DESC"; //query pulls all data needed to calculate venue average over the duration of the season
$result2 = mysql_query($AvePlayers) or die("Couldn't execute query because: ".mysql_error());//creates variable holding query results

$pos=1; //creates variable pos with initial value of 1



$average_by_venue = array(); // creates an array 


while ($r2 = mysql_fetch_array($result2) ) { 

  $average_by_venue[ $r2['VenueName'] ] = $r2['Average']; 

} //close while

Then a table is drawn and this fills the details in:

// Above creates table and headers. Now start the loop.





$LastSameVenue =0; //create variable and give value 0







while($r = mysql_fetch_array($result)){                                    // THIS FIRST LOOP CALLCULATES THE CHIP STACK FOR EACH RESULTw
                                                                        //AND BUILDS ARRAY.

$arr[] =  array(1 => $r[1], 2 => $r[2], 3 => $r[3], 4 => $r[4], 5 => $r[5], 6 => $r[6], 7 => $r[7], 8 => $r[8]);
// array values are [1]Player.FirstName , [2]Player.LastName,  [3]Venue.VenueName as Venue, [4]Results.MemCard, [5]Results.EarlyReg, [6]Position.Points as Venue_Points, [7]Results.Date
}

foreach ($arr as $key => $row) {                                         // THIS SECOND LOOP BUILD AN ARRAY FOR EACH PRSON AT EACH VENUE
                                                                        // ADDING THE POINTS FOR EACH VENUE AND ADDING THE NUMBER OF TIMES
                                                                        // PLAYED AT EACH VENUE. BEAUTY

    $bonusGROUPED[$row[1].$row[2].$row[3]] = $bonusGROUPED[$row[1].$row[2].$row[3]] + $row[4] + $row[5]; //grouped data with bonus chips
    $pointsGROUPED[$row[1].$row[2].$row[3]] = $pointsGROUPED[$row[1].$row[2].$row[3]] + $row[6]; //creates grouped data for each player with points
    $totalpointsGROUPED[$row[1].$row[2].$row[3]] = $totalpointsGROUPED[$row[1].$row[2].$row[3]] + $row[4] + $row[5] + $row[6];//creates grouped data for each player with points
    $playedGROUPED[$row[1].$row[2].$row[3]] = $playedGROUPED[$row[1].$row[2].$row[3]] +1; //creates grouped data for number of times played
    $GBPCPoints = $totalpointsGROUPED[$row[1].$row[2].$row[3]] * $average_by_venue[$row[3]] ; // calculates number of chips
    $CHIPS = ceil($GBPCPoints / 1) * 1; //rounds up chipstack to nearest 25        

$arrGROUPED[$row[1].$row[2].$row[3]] =  array(1 => $row[1], 2 => $row[2], 3 => $row[3], 4 => $bonusGROUPED[$row[1].$row[2].$row[3]], 5 =>$pointsGROUPED[$row[1].$row[2].$row[3]], 6 => $totalpointsGROUPED[$row[1].$row[2].$row[3]], 7 => $playedGROUPED[$row[1].$row[2].$row[3]], 8 =>$CHIPS); //creates new array combining all the above data

}


foreach ($arrGROUPED as $key => $row2) {                                // THIS LOOP NOW TAKES THE ARRAY AND SORTS IT IN ORDER OF NAME
                                                                        // AND THEN CHIPS. 
    $FirstName[$key]  = $row2['1'];
    $LastName[$key] = $row2['2'];
    $Venue[$key] = $row2['3'];
    $bonus[$key] = $row2['4'];
    $Venue_Points[$key] = $row2['5'];
    $Total_Points[$key] = $row2['6'];
    $Venue_Play_Count[$key] = $row2['7'];
    $chipstack[$key] =     $row2['8'];


//print("-  $row2[1] $row2[2] $row2[3] $row2[4] <B>$row2[5]</B> $row2[6] - <BR>");

}
array_multisort($FirstName, SORT_DESC, $LastName, SORT_DESC,  $chipstack, SORT_DESC, $arrGROUPED);





$printedFirst = "NULL";
                                                                                    
foreach ($arrGROUPED as $key => $row3) {                                // THIS LOOP MAKES A NEW ARRAY WITH ONLY THE FIRST OF EACH PESONS RESULT
                                                                        // WHICH IS THERE BEST. WOOHOOO

       if($row3[1].$row3[2] !=  $printedFirst){
         $arrHIGHEST_RESULT[] =  array(1 => $row3[1], 2 => $row3[2], 3 => $row3[3], 4 => $row3[4], 5 => $row3[5], 6 => $row3[6], 7 => $row3[7], 8 => $row3[8]);
 //        print("--  $row3[1] $row3[2] $row3[3] $row3[4] <B>$row3[5]</B> $row3[6] -- <BR>");
         }
      $printedFirst =  $row3[1].$row3[2];
    
}

foreach ($arrHIGHEST_RESULT as $key => $row4) {                            // AND THEN THIS LOOP REORDERS THE NEW ARRAY TO PUT IN ORDER
                                                                        // OF CHIPS. FANTASTIC
    $FirstName4[$key]  = $row4['1'];
    $LastName4[$key] = $row4['2'];
    $Venue4[$key] = $row4['3'];
    $bonus4[$key] = $row4['4'];
    $Venue_Points4[$key] = $row4['5'];
    $Total_Points4[$key] = $row4['6'];
    $Venue_Play_Count4[$key] = $row4['7'];
    $chipstack4[$key] = $row4['8'];

//print("#  $row4[1] $row4[2] $row4[3] $row4[4] <B>$row4[5]</B> $row4[6] # <BR>");

}

array_multisort($chipstack4, SORT_DESC, $arrHIGHEST_RESULT);



foreach ($arrHIGHEST_RESULT as $key => $row3) {                                // THIS LAST LOOP THEN PRINTS THE ARRAY. FIN

if($pos < 51){
if($pos == 51){
$style = "style=\"border-top: solid #F00 4px;\"";    
}else{
$style = "";    
}
    
    echo"
    <tr >
        <td align=\"center\" $style><a href=\"playerDates.php?FName={$row3['1']}& LName={$row3['2']}\">" . $row3[1] .' '. $row3[2] .  "</a></td>
        
        
        <td align=\"center\" $style>" . $row3[8] . "</td>
        
    </tr>";
    $pos++;
      $printedFirst =  $row3[1].$row3[2];
    
}
}
//And close the table.

 

 

The result is then displayed in this table onscreen as can be seen on the right hand side of this page: www.gbpokerclub.co.uk

if you look at www.gbpokerclub.co.uk/script_test.php you'll see the same page with error reporting

 

And if you're wondering no I didn't write all this, I had a huge amount of help from several sources and pieced it together over many many weeks!

Edited by PHP_Idiot
Link to comment
Share on other sites

$arr[] =  $r + array_fill_keys(range(1,,null);
The array_fill_keys bit will generate an array with indexes 1-8 and their values set to null. Then $r gets union'ed with that array so any of the entries missing from $r will be filled with the null values from the generated array.

 

The result is the value stored in $arr will always have keys 1-8 set either with what was contained in $r, or NULL if the entry was missing.

Link to comment
Share on other sites

Thanks for the help kicken, I'm not sure I did it right but I replaced line 98 with your suggestion and its certainly fixed the issue with line 98 but now I'm getting 

Undefined variable: bonusGROUPED

on line 107, 108, 109 & 110

 

I have to leave for work now, but I'm wondering if a similar approach to those lines would solve those issues as well.

Thanks again for the help :)

Link to comment
Share on other sites

Hmm I can't resolve this undefine variable!

 

I tried to set the variables to '=0' but that creates this warning instead:

warning: Cannot use a scalar value as an array

 

and outputs everything as 0.

at least with the undefined variable the output is still correct!

 

Anybody got any ideas I could try?

Cheers

Link to comment
Share on other sites


    $arr[] =  $r + array_fill_keys(range(1,,null);
    // $arr[] =  array(1 => $r[1], 2 => $r[2], 3 => $r[3], 4 => $r[4], 5 => $r[5], 6 => $r[6], 7 => $r[7], 8 => $r[8]);
// array values are [1]Player.FirstName , [2]Player.LastName,  [3]Venue.VenueName as Venue, [4]Results.MemCard, [5]Results.EarlyReg, [6]Position.Points as Venue_Points, [7]Results.Date
}

 

As you can see all I've done is blanks out the previous $arr[] and replaced it with kicken's suggestion and this has solves the initial warning.

 

These are the lines the undefined error is showing for:


    $bonusGROUPED[$row[1].$row[2].$row[3]] = $bonusGROUPED[$row[1].$row[2].$row[3]] + $row[4] + $row[5]; //grouped data with bonus chips
    $pointsGROUPED[$row[1].$row[2].$row[3]] = $pointsGROUPED[$row[1].$row[2].$row[3]] + $row[6]; //creates grouped data for each player with points
    $totalpointsGROUPED[$row[1].$row[2].$row[3]] = $totalpointsGROUPED[$row[1].$row[2].$row[3]] + $row[4] + $row[5] + $row[6];//creates grouped data for each player with points
    $playedGROUPED[$row[1].$row[2].$row[3]] = $playedGROUPED[$row[1].$row[2].$row[3]] +1; //creates grouped data for number of times played

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.