Jump to content

Fatal error: [] operator not supported for strings in


frankpetrov

Recommended Posts

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();
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();
        }
}

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

 

 

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.

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.