frankpetrov Posted April 24, 2012 Share Posted April 24, 2012 As title says, I'm getting this error: Fatal error: [] operator not supported for strings in C:\Users\Administrator\Desktop\xampp\htdocs\arpg\locations_modules\2dmap\generic_map.php on line 225 Line 225 is: $monsters[ ] = $r->fields[0]; And this is the whole section if it helps any. $nbmonsters=0; if($sql != "") { $r=$db->Execute($sql); if($r !== false) while(!$r->EOF) { if(($r->fields[0]+0) == 0) { $r->MoveNext(); continue; } $p=mt_rand(0,100); if(($r->fields[2]+0) != 100 && $p < ($r->fields[2]+0)) { $r->MoveNext(); continue; } if($r->fields[1] != "") { $res=false; $code='if ('.$r->fields[1].') $res=true;'; eval($code); if($res == false) { $r->MoveNext(); continue; } } $monsters[ ] = $r->fields[0]; $nbmonsters++; $r->MoveNext(); } if($r !== false) $r->Close(); } Quote Link to comment Share on other sites More sharing options...
silkfire Posted April 24, 2012 Share Posted April 24, 2012 I can't from your code determine what $monsters is, but it seems like it's a string and not an array. You use the [] operator to add an item to an array, you can't do that on strings. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 24, 2012 Share Posted April 24, 2012 if $monsters is not already declared as a string try taking the space out from between the [ ] when you are assigning the value to it. Quote Link to comment Share on other sites More sharing options...
cpd Posted April 24, 2012 Share Posted April 24, 2012 You should define your $monsters array before the while loop and get rid of the white space as Muddy said. Quote Link to comment Share on other sites More sharing options...
frankpetrov Posted April 24, 2012 Author Share Posted April 24, 2012 You should define your $monsters array before the while loop and get rid of the white space as Muddy said. as for what $monsters is: for($i=0;$i < $nbmonsters;$i++) echo "monsterid[$i]=".$monsters[$i].";\n"; Removing the whitespace doesn't change anything nor does moving it to before the while statement I saw something about var_dump() and print_r(), but I eally don't understand them. How can I use one of these to print out on the browser to see if maybe there's a DB error? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 24, 2012 Share Posted April 24, 2012 I usualy use die(var_dump($variable)); at strategic points to debug code. var_dump is better for debuging as it gives you the variable type as well as the contnent. Quote Link to comment Share on other sites More sharing options...
cpd Posted April 24, 2012 Share Posted April 24, 2012 Try the following. I've shrunk it as it makes more logical sense in this form but ultimately your conditions should be reversed and where you set the monsters and increment should go inside the if with the MoveNext function only being written once. $nbmonsters=0; if($sql != ""){ $r=$db->Execute($sql); $monsters = array(); if($r !== false){ while(!$r->EOF){ if(($r->fields[0] == 0) || (!$r->fields[1]) || ($r->fields[2] != 100 && mt_rand(0,100) < $r->fields[2])){ $r->MoveNext(); continue; } $monsters[] = $r->fields[0]; $nbmonsters++; $r->MoveNext(); } $r->Close(); } } If you wanted to do it the way I described above $nbmonsters=0; if($sql != ""){ $r=$db->Execute($sql); $monsters = array(); if($r !== false){ while(!$r->EOF){ if(Reverse of all the conditions){ $monsters[] = $r->fields[0]; $nbmonsters++; } $r->MoveNext(); } $r->Close(); } } Quote Link to comment Share on other sites More sharing options...
silkfire Posted April 24, 2012 Share Posted April 24, 2012 You should define your $monsters array before the while loop and get rid of the white space as Muddy said. as for what $monsters is: for($i=0;$i < $nbmonsters;$i++) echo "monsterid[$i]=".$monsters[$i].";\n"; Removing the whitespace doesn't change anything nor does moving it to before the while statement I saw something about var_dump() and print_r(), but I eally don't understand them. How can I use one of these to print out on the browser to see if maybe there's a DB error? I don't really understand what you want to achieve here? We still don't know how the array $monsters looks like. Does it have associative keys? If you want to strip the keys you can do like this: $monsterid = array_values($monsters) Quote Link to comment Share on other sites More sharing options...
frankpetrov Posted April 24, 2012 Author Share Posted April 24, 2012 If you wanted to do it the way I described above $nbmonsters=0; if($sql != ""){ $r=$db->Execute($sql); $monsters = array(); if($r !== false){ while(!$r->EOF){ if(Reverse of all the conditions){ $monsters[] = $r->fields[0]; $nbmonsters++; } $r->MoveNext(); } $r->Close(); } } This one did fix the problem... sorta. It now has this error. Parse error: parse error, unexpected T_STRING in C:\Users\Administrator\Desktop\xampp\htdocs\arpg\locations_modules\2dmap\generic_map.php on line 238 which is relevant to if(Reverse of all the conditions){ I do appreciate all the help from you all. I'm trying to learn this stuff but it can be quite confusing when there's just some books. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 24, 2012 Share Posted April 24, 2012 That's psuedo code, you have to replace the text with actual code. 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.