Jump to content

Recommended Posts

Hey all,

 

I am still fairly new to PHP programming and I am trying to put together a page where you select search terms from a drop down menu and it keeps the menu at your search choice in the menu after the search.

 

Here is the code I was suggested to use, but I am having problems understanding how it keeps the choice in the menu.

 

I can almost understand how it works, but I need a little push to grasp it.

 

Here is the code:

 

foreach($options as $key => $content)

        {

            if ($key == $value) $select = ' selected="selected"';

            else                $select = NULL;

            $html .=

                '<option value="' . htmlspecialchars($key) . '"' . $select .

                '>' . htmlspecialchars($content) . '</option>' . "\n";

        }

 

Now I understand how to walk through the code, but I am having trouble understanding how it holds over the choice.

 

THanks in advance for the help...

Link to comment
https://forums.phpfreaks.com/topic/177919-solved-explanation-help/
Share on other sites

It simply loops through the contents of the options array echo'ing them out. If the current item in the array matches $value (which must have been set further up the code using something like $value = $_POST['name_of_select_node']) it includes selected="selected" as part of the node. In HTML this basically means select this item in the list.

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.