funkafaction Posted June 29, 2008 Share Posted June 29, 2008 Quick question, can you tell me how to turn an array into a select field? Thanks. Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/ Share on other sites More sharing options...
DarkWater Posted June 29, 2008 Share Posted June 29, 2008 Show me how the array is formatted. Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/#findComment-577457 Share on other sites More sharing options...
funkafaction Posted June 29, 2008 Author Share Posted June 29, 2008 $timezone_array = array( ); $timezone_array = array( "GMT -12:00" => "International Date Line West"; ); Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/#findComment-577460 Share on other sites More sharing options...
DarkWater Posted June 29, 2008 Share Posted June 29, 2008 How do you want the select option formatted? Do you want the key of the array to be the value of the select and the value of the array to be the "text" of the option? Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/#findComment-577461 Share on other sites More sharing options...
funkafaction Posted June 29, 2008 Author Share Posted June 29, 2008 Yes. Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/#findComment-577472 Share on other sites More sharing options...
DarkWater Posted June 29, 2008 Share Posted June 29, 2008 foreach ($timezone_array as $k=>$v) { printf('<option value="%s">%s</option>', $k, $v); } Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/#findComment-577475 Share on other sites More sharing options...
funkafaction Posted June 29, 2008 Author Share Posted June 29, 2008 It's not quite working, %s is showing up. I have no idea what I'm doing when it comes to PHP, so do you mind explaining it to me? Thanks. Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/#findComment-577479 Share on other sites More sharing options...
thatsgreat2345 Posted June 29, 2008 Share Posted June 29, 2008 It is taking the array that you had loaded with data, and then foreach , $k which refers to the key which would be GMT -12:00 set its value to $v which would be "International Date Line West". Then go through till the end of the array. Printf is the same as echo but allows formatting. so the %s refers to a string. And then what ever order you put the variables in is the places they will be placed and then echoed. So $k (which is the key) is set to the value, and the value of key ($v) is set to be showed. full code would be <? $timezone_array = array( "GMT -12:00" => "International Date Line West" ); foreach ($timezone_array as $k=>$v) { printf('<option value="%s">%s</option>', $k, $v); } ?> Link to comment https://forums.phpfreaks.com/topic/112467-turning-an-array-into-a-select-field/#findComment-577480 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.