Ehab Fawzy
Ehab Fawzy

Reputation: 21

The select option in php

If I have a plugin in my server account which has this code:

function script_options() {
    $scripts = array(
        'Forum' => array(
            '403' => 'phpBB2',
            '5' => 'phpBB3',
            '417' => 'Simple Machines Forum',
        ),
        'Blog' => array(
            '410' => 'b2evolution',
            '418' => 'Movable Type',
            '409' => 'Textpattern',
            '400' => 'WordPress',
        ),
        'Wiki' => array(
            '413' => 'DokuWiki',
        ),
    );
    $result = '<option value="">'.__rp('none','none').'</option>';
    foreach ($scripts as $group => $list) {
        $result .= "<optgroup label=\"$group\">";
        foreach ($list as $script_key => $script_value) {
            $result .= "<option value=\"$script_key\"".($script_key == isset($_GET['script'])?$_GET['script']:false ? ' selected="selected"':'').">$script_value</option>";
        }
        $result .= '</optgroup>';
    }
    return $result;
}

How can I make the first choice be ('400' 'Wordpress'); if the user did not choose anything it chooses Wordpress by itself.

Upvotes: 2

Views: 185

Answers (1)

tdammers
tdammers

Reputation: 20721

It looks like you don't fully understand what this code does, so let me boil it down for you.

Let's start at the end and work our way backwards: When the function returns, the $result variable contains an HTML fragment with a bunch of <optgroup> tags in it, and those contain <option> tags. I assume this HTML gets pasted into a <select> tag somewhere outside this function (BTW, <select> is an HTML thing; PHP has a select() function that is completely unrelated to this, so to avoid confusion, don't refer to HTML's <select> as "select option in PHP").

The foreach loops right before that construct the $result value by concatenating individual chunks of HTML, and those are in turn derived from the nested associative array declared at the beginning of the function. If you follow those loops carefully, you'll see that the tree structure of the resulting HTML follows that of the nested array: each top-level element becomes an <optgroup>, and the name is derived from the array key; each second-level element becomes an <option>, where the key goes into the value attribute (which determines the value used when the containing form is submitted), and the value goes into the user-visible tag content. The array elements are visited in order, so what comes first in the array also comes first in the resulting HTML.

There are two things you need to know about <select> in this context: first, you can define which option is selected by default by adding a selected tag to it (the standard way to express this is <option selected="selected">...</option>); there should be at most one selected option in your <select>. And second, if none of your options has a select attribute, the first one becomes the default.

Now, what does this mean for you? Simple: you can either make it so that your code sets the selected attribute on the WordPress entry; this way, your options remain ordered exactly as they are now, optgroups and all, but WordPress will be preselected. Or you can put the WordPress one as the very first element, which puts it first in the <select>, and should get it preselected because no option has a selected attribute.

Upvotes: 3

Related Questions