Drop Down Menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Drop Down Menu

Post by michaelk46 »

I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.

THis was suggested to me:

Code: Select all

public function formOptions($options, $value = NULL)
    {
        $html = NULL;
        foreach($options as $key => $content)
        {
            if ($key == $value) $select = ' selected="selected"';
            else                $select = NULL;
             $html .= 
                '<option value="' . htmlspecialchars($key) . '"' . $select .
                '>' . htmlspecialchars($content) . '</option>' . "\n";
        }
        return $html;
    }

and this is the template:

Code: Select all

<select name="unit_type_id">
 
            <option value="0">Organization Type</option>
 
            <?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
 
        </select>
I am on the verge of understanding it, but I can't quite grasp it.

Could one of y'all explain how this accomplishes that goal? Thanks in advance
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Drop Down Menu

Post by markusn00b »

michaelk46 wrote:I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.

THis was suggested to me:

Code: Select all

public function formOptions($options, $value = NULL)
    {
        $html = NULL;
        foreach($options as $key => $content)
        {
            if ($key == $value) $select = ' selected="selected"';
            else                $select = NULL;
             $html .= 
                '<option value="' . htmlspecialchars($key) . '"' . $select .
                '>' . htmlspecialchars($content) . '</option>' . "\n";
        }
        return $html;
    }

and this is the template:

Code: Select all

<select name="unit_type_id">
 
            <option value="0">Organization Type</option>
 
            <?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
 
        </select>
I am on the verge of understanding it, but I can't quite grasp it.

Could one of y'all explain how this accomplishes that goal? Thanks in advance
This is why folks should document their code. Anyway, on to the explanation:

You make a call to the formOptions() method of whatever class (I'm assuming it's some controller), passing it 2 arguments: unitTypePickList, and unitTypeId - an array and an identifier, respectively.

The formOptions() method then creates an empty variable, $HTML, which will be used to store the rendered HTML. Duh. Then the method begins looping over the $options array (unitTypePickList) assigning the current index or the iteration to $key and the current index's value to $content. Within the foreach() loop, there is a conditional that checks whether $key == $value - this is where the 'selected' option element is decided; if a match is found, the $selected variable contains the HTML attribute 'selected="selected"'. Otherwise, it remains empty. Next, the HTML option element is constructed and concatenated onto the $HTML variable, including the $selected and $content variables in the output.

Once the loop is completed, the $HTML variable is returned to the caller (in this case, echo) and echo proceeds to output the data.

Simple.

Mark.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: Drop Down Menu

Post by michaelk46 »

I am still fairly new to PHP and that explanation has gone a long way in helping me understand, but I have a couple of questions...
there is a conditional that checks whether $key == $value - this is where the 'selected' option element is decided; if a match is found, the $selected variable contains the HTML attribute 'selected="selected"'. Otherwise, it remains empty.
This is where it still gets confusing to me... why would $key and $value be equal?
Once the loop is completed, the $HTML variable is returned to the caller (in this case, echo) and echo proceeds to output the data.
Second, does this line store the selected option in $html?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Drop Down Menu

Post by markusn00b »

michaelk46 wrote:I am still fairly new to PHP and that explanation has gone a long way in helping me understand, but I have a couple of questions...
there is a conditional that checks whether $key == $value - this is where the 'selected' option element is decided; if a match is found, the $selected variable contains the HTML attribute 'selected="selected"'. Otherwise, it remains empty.
This is where it still gets confusing to me... why would $key and $value be equal?
Once the loop is completed, the $HTML variable is returned to the caller (in this case, echo) and echo proceeds to output the data.
Second, does this line store the selected option in $html?
Sorry for my late reply, michaelk46.

The $value parameter is the index of the array ($options) that is intended to be the 'selected' option. The $key variable is what keeps track of the current index for the iteration of the foreach loop. So, it translates into human text as: if the current iteration's index is equal to the arbitrary $value, that is, the user defined element to be labelled as 'selected', append the selected text into the html. Does that help?

Second question: could you elaborate? What line would that be?

Mark.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Drop Down Menu

Post by Mirge »

markusn00b wrote:
michaelk46 wrote:I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.

THis was suggested to me:

Code: Select all

public function formOptions($options, $value = NULL)
    {
        $html = NULL;
        foreach($options as $key => $content)
        {
            if ($key == $value) $select = ' selected="selected"';
            else                $select = NULL;
             $html .= 
                '<option value="' . htmlspecialchars($key) . '"' . $select .
                '>' . htmlspecialchars($content) . '</option>' . "\n";
        }
        return $html;
    }

and this is the template:

Code: Select all

<select name="unit_type_id">
 
            <option value="0">Organization Type</option>
 
            <?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
 
        </select>
I am on the verge of understanding it, but I can't quite grasp it.

Could one of y'all explain how this accomplishes that goal? Thanks in advance
This is why folks should document their code. Anyway, on to the explanation:
Off-topic, but I read that... then read your signature. I chuckled. :lol:
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Drop Down Menu

Post by markusn00b »

Mirge wrote:
markusn00b wrote:
michaelk46 wrote:I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.

THis was suggested to me:

Code: Select all

public function formOptions($options, $value = NULL)
    {
        $html = NULL;
        foreach($options as $key => $content)
        {
            if ($key == $value) $select = ' selected="selected"';
            else                $select = NULL;
             $html .= 
                '<option value="' . htmlspecialchars($key) . '"' . $select .
                '>' . htmlspecialchars($content) . '</option>' . "\n";
        }
        return $html;
    }

and this is the template:

Code: Select all

<select name="unit_type_id">
 
            <option value="0">Organization Type</option>
 
            <?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
 
        </select>
I am on the verge of understanding it, but I can't quite grasp it.

Could one of y'all explain how this accomplishes that goal? Thanks in advance
This is why folks should document their code. Anyway, on to the explanation:
Off-topic, but I read that... then read your signature. I chuckled. :lol:
Are you insinuating I am a hypocrite? Off with his head!
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Drop Down Menu

Post by Mirge »

The irony was awesome :lol:
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Drop Down Menu

Post by markusn00b »

Mirge wrote:The irony was awesome :lol:
Of course I do document my writings; I just like the phrase. :)

/on-topic.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Drop Down Menu

Post by Mirge »

markusn00b wrote:
Mirge wrote:The irony was awesome :lol:
Of course I do document my writings; I just like the phrase. :)

/on-topic.
I figured :)
Post Reply