Auto populate a select box using an array in PHP

后端 未结 3 1482
清酒与你
清酒与你 2020-12-11 07:15

If i had a select box


and I had an array of values from 1-12

using ph

3条回答
  •  囚心锁ツ
    2020-12-11 07:59

    There's no single way to do it but you might do something like:

    
    

    where getMyOptions() is a function call that retrieves the options (from a database for example) and prints each one in the format

    
    

    Personally, I tend to use a generic function which I call printAsOptions(). This function takes an array of objects. It expects that the objects in the array have a field called "id" and a field called "name". It iterates through the array and prints an option as above for each item. This way, you can create one function for fetching the array of objects (from a database for example) without mixing in presentation logic. The presentation logic is handled by the generic printAsOptions() function.

提交回复
热议问题