Generate a Select selection box with a function

Source: Internet
Author: User

1 //generate an HTML select option tag2 functionBuild_options ($options,$opt= ",$k= ' ') {3     $frags=Array();4     if(is_string($options))5     {6         if(FALSE!==Strpos($options, ‘,‘)) {7             $options=Explode(‘,‘,$options);8}Else if(FALSE!==Strpos($options, ‘ ‘)) {9             $options=Explode(‘ ‘,$options);Ten}Else{ One             return‘‘; A         } -         if($opt&&!In_array($opt,$options)) -         { the             Array_unshift($options,$opt); -         } -  -         $options=Array_combine($options,$options); +     } -     if(Is_array($options)) +     { A         if($opt&&!In_array($opt,$options)) at         { -             Array_unshift($options,$opt); -         } -         foreach($options  as $key=$val) -         { -             if($opt!== ' &&$k!== ' &&$key==$opt) in             { -                 //and then there's the key in the element that was added to the K to                 $key=$k; +             } -             $frags[] =sprintf(' <option value= '%s '%s>%s</option> ',$key,$opt! = ' &&$key==$opt? ' Selected ': ',$val); the         } *     } $ Panax Notoginseng     return  implode("\ n",$frags); -}

Pass in two parameters the first one is the array the second is the column to be selected key value

If just passing in the string in such a

And if you want to pass in a key value that is not in the array, you can do this

By default, you need to select

Generate a Select selection box with a function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.