EchoFool Posted February 20, 2009 Share Posted February 20, 2009 I have a problem with my array it gives me an offset error and have no idea how to fix it.. the idea is to find the lowest Group so that the user is assigned the lowest one thus hopefully keeping things as even as possible.... here is what i have: <?php $Get = mysql_query("SELECT ID FROM group WHERE Ended != '1' ORDER BY ID ASC") Or die(mysql_error()); $State = array(); While($row = mysql_fetch_assoc($Get)){ $ID = $row['ID']; $Networth = 0; $SELECT = mysql_query("SELECT Houses+Money AS NetWorth FROM city WHERE SID='{$row['ID']}'") Or die(mysql_error()); If(mysql_num_rows($SELECT)>0){ While($row2 = mysql_fetch_assoc($SELECT)){ $Networth = $Networth + $row2['NetWorth']; } }Else{ $Networth = 0; } $City[] = $Networth; unset($Networth); } $temp = $City; // make a temp array to sort, to preserve original array sort($temp); // sort $temp to have lowest val first $key = array_search($temp[0], $City); // search $array using first val in $temp, returning first key found $val = $City[$key]; // get the value using the key found $key = $key + 1; // as the IDs are 1 / 2 / 3 or 4 .. key in array is 0 / 1 / 2 / 3 so + 1 makes them match ?> The error recieved was: Notice: Undefined offset: 0 in council.php on line 87 Notice: Undefined offset: 0 in council.php on line 88 Hope you can help out what is causing it! Quote Link to comment Share on other sites More sharing options...
EchoFool Posted February 20, 2009 Author Share Posted February 20, 2009 Bump Quote Link to comment Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 The error is simple, you do not have an array with an index of 0, and in your code somewhere you are trying to access that. Where I do not know, there are not 87 lines of code posted and you failed to highlight where that would have been, which is why you have not received an answer yet. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted February 20, 2009 Author Share Posted February 20, 2009 Line 87 and 88 in this situation is: $temp = $City; // make a temp array to sort, to preserve original array sort($temp); Is there any echo's i can do on my script to aide in helping find the cause of my problem ? Quote Link to comment Share on other sites More sharing options...
Mad Mick Posted February 20, 2009 Share Posted February 20, 2009 Use print_r($temp) or print_r($City) to see what's in your arrays - will help you track in down... Quote Link to comment Share on other sites More sharing options...
EchoFool Posted February 20, 2009 Author Share Posted February 20, 2009 Argh, found the issue! Thanks for the tip on the print_r Solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.