0

HI Following is the code , i am trying to print the array which contain values of drop down menu and then 2nd foreach loop is trying to selected the field. The output is the way i want , mean it print the all menu item, and the selected one,

foreach($results as $data): 
        $st = '';
        foreach($SelectedActor as $SelectedActor):
        if($SelectedActor['id']==$data['id']){$st='selected="selected"';}
         endforeach;
        $dd .="<option ".$st."  value=".$data['id']."> ".$data['artist_name']."    </option>";

        endforeach;

but the page also show this error

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'id'

Filename: controllers/replik.php

Line Number: 328

Vardump for these two array are

array (size=2)
  0 => 
    array (size=2)
      'id' => string '1' (length=1)
      'artist_name' => string ' oyuncu' (length=19)
  1 => 
    array (size=2)
      'id' => string '4' (length=1)
      'artist_name' => string 'hep brabir' (length=10)

array (size=2)
  0 => 
    array (size=2)
      'id' => string '1' (length=1)
      'artist_name' => string 'oyuncu' (length=19)
  1 => 
    array (size=2)
      'id' => string '4' (length=1)
      'artist_name' => string 'hep brabir' (length=10)

Can any one help me to get the reason of this error.

1
  • var_dump($SelectedActor, $data) - one of these is not an array. Commented Nov 18, 2013 at 12:51

2 Answers 2

2
 foreach($SelectedActor as $SelectedActor):
        if($SelectedActor['id']==$data['id']){$st='selected="selected"';}

here you use the same name of $SelectedActor, maybe

foreach($SelectedActor as $ItemSelectedActor):
            if($ItemSelectedActor['id']==$data['id']){$st='selected="selected"';}

is ok

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for the stupid mistake i make here. I was doing foreach($SelectedActor as $SelectedActor): i do it like foreach($SelectedActor as $SelectedActores): just changes the second variable name and it work.
but i dont unsdertand in this case why it gives error. mostly when do foreachloop way it work fines, in this case there were two foreach loop so its errror ? or thier is ome other reason.
0

Below line is wrong:

foreach($SelectedActor as $SelectedActor)

You need to do:

foreach($SelectedActor as $actor):
    if($actor['id']==$data['id']){
        $st='selected="selected"';
    }
endforeach;

1 Comment

You might want to consider fixing this line if($actor'id']==$data['id']){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.