3

I have this function for show selected value in multiple dropdown:

function _is_dropdown_( $name,array $selected=null,$table ,$class,$required,$type_name,$type, $size )
{
        $options = DB::fetch("SELECT name, id FROM " . $table . " WHERE ". $type_name ." = ? ORDER BY name",$type);
      //$options = array();
        /*** begin the select ***/
        $dropdown = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'" required = "'.$required.'" size="'.$size.'" multiple>'."\n";

        /*** loop over the options ***/
        foreach($options as $key=>$option )
        {
                /*** assign a selected value ***/
                $select = in_array( $option, $selected ) ? ' selected' : null;

                /*** add each option to the dropdown ***/
                $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option.'</option>'."\n";
        }

        /*** close the select ***/
        $dropdown .= '</select>'."\n";

        /*** and return the completed dropdown ***/
        return $dropdown;
}

for result:

$name = 'book_cover[]';
$selected = json_decode($DB_QUERY[0]['book_cover'] , true);
echo _is_dropdown_( $name,$selected ,NEWS_TYPE ,'contentgroup','required','book_type','4', '4' );

Now in result dropdown not show name of option:

<select name="book_cover[]" class="contentgroup" required = "required" size="4" multiple>
<option value="0"  >Array</option>
<option value="1"  >Array</option>
<option value="2"  >Array</option>
<option value="3"  >Array</option>
<option value="4"  >Array</option>
<option value="5"  >Array</option>
<option value="6"  >Array</option>
<option value="7"  >Array</option>
<option value="8"  >Array</option>
<option value="9"  >Array</option>
<option value="10"  >Array</option>
<option value="11"  >Array</option>
<option value="12"  >Array</option>
</select>

And show this error :

[2015-09-09 08:28:06] [E_NOTICE] [8] Array to string conversion in C:\xampp\htdocs\cms\class\functions.php:1032 

EDIT: i print_r $options:

(
    [0] => Array
        (
            [name] => test
        )

    [1] => Array
        (
            [name] => test2
        )

    [2] => Array
        (
            [name] => test3
        )
)

EDIT 2: i change $options to manual array : $options = array( 'test', 'test1', 'test2' ); this worked fine. now i need to convert my db result to this.

how do fix this error ?!

1
  • Clearly $option is an array. Would need to see the result set of the query to further debug. Commented Sep 9, 2015 at 4:04

4 Answers 4

1

Here your key are 0,1,2... and values are

$option = Array
(
    [name] => test
)

So instead of $option use $option["name"].

foreach($options as $key=>$option ) {

      $select = in_array( $option["name"], $selected ) ? ' selected' : null;
      $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option["name"].'</option>'."\n";

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

Comments

0

You have $options which are rows. So each $option is a row, an array with 2 elements (name, id).

Comments

0

Updated the function, Please use this:

 function _is_dropdown_( $name,array $selected=null,$table ,$class,$required,$type_name,$type, $size )
    {
            $options = DB::fetch("SELECT name, id FROM " . $table . " WHERE ". $type_name ." = ? ORDER BY name",$type);
          //$options = array();
            /*** begin the select ***/
            $dropdown = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'" required = "'.$required.'" size="'.$size.'" multiple>'."\n";

            /*** loop over the options ***/
            foreach($options as $key=>$option )
            {
                    /*** assign a selected value ***/
                    $select = ($key==$selected ? ' selected' : null );

                    /*** add each option to the dropdown ***/
                    $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option[$key]['name'].'</option>'."\n";
            }

            /*** close the select ***/
            $dropdown .= '</select>'."\n";

            /*** and return the completed dropdown ***/
            return $dropdown;
    }

2 Comments

<b>Parse error</b>: syntax error, unexpected 'if' (T_IF) in
not work. i change $options to manual array : $options = array( 'test', 'test1', 'test2' ); this worked fine. now i need to convert my db result to this.
0

check this

$dropdown .= '<option value="'.$key.'" '.$select.' >'.$option['name'].'</option>'."\n";

Comments

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.