nz_mitch Posted March 14, 2009 Share Posted March 14, 2009 Hi there, I've searched the forums but haven't been able to find an understand I can understand - sorry, I'm pretty new to this! I'm trying to populate the drop down box of a simple HTML form using the data from an API. The data will change from time, adding and removing options but the form doesn't need to update dynamically when it's being used or anything - just when reloaded. I've managed to access the data from the API and it looks like this when I print the array with formatting: Array ( [anyType] => Array ( [ListCustomField] => Array ( [FieldName] => ReferringAgent [Key] => [ReferringAgent] [DataType] => MultiSelectOne [FieldOptions] => Array ( [string] => Array ( [0] => Mitch [1] => Madison [2] => Blake ) ) ) ) All I want is a drop down box that has the options Mitch, Blake and Madison. Can anyone help? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/149422-simple-drop-down-population-with-php/ Share on other sites More sharing options...
zq29 Posted March 14, 2009 Share Posted March 14, 2009 You could loop through the array with a foreach loop, like so... <?php echo "<select name='foo'>"; foreach($array['anyType']['ListCustomField']['FieldOptions']['string'] as $key => $value) { echo "<option value='$key'>$value</option>"; } echo "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/149422-simple-drop-down-population-with-php/#findComment-784787 Share on other sites More sharing options...
shlumph Posted March 14, 2009 Share Posted March 14, 2009 Try this: <?php $array = $APIarray['anyType']['ListCustomField']['FieldOptions']['string']; foreach($array as $person) { echo "<p>{$person}</p>"; } ?> And let me know if it works. Link to comment https://forums.phpfreaks.com/topic/149422-simple-drop-down-population-with-php/#findComment-784789 Share on other sites More sharing options...
nz_mitch Posted March 14, 2009 Author Share Posted March 14, 2009 Wow awesome response! Thanks @SemiApocalyptic: I used your code (adjusted it for the form I've built) and it worked perfectly! @shlumph: I placed your code at the bottom of the page and it outputted the three names as expected! Link to comment https://forums.phpfreaks.com/topic/149422-simple-drop-down-population-with-php/#findComment-784798 Share on other sites More sharing options...
zq29 Posted March 14, 2009 Share Posted March 14, 2009 Excellent, I'll mark this one as solved then! Link to comment https://forums.phpfreaks.com/topic/149422-simple-drop-down-population-with-php/#findComment-784801 Share on other sites More sharing options...
nz_mitch Posted March 14, 2009 Author Share Posted March 14, 2009 Thanks so much for you help! Got one final question to get it back into the API, but I'll put that in a separate thread as it's a different question altogether. Link to comment https://forums.phpfreaks.com/topic/149422-simple-drop-down-population-with-php/#findComment-784804 Share on other sites More sharing options...
nz_mitch Posted April 5, 2009 Author Share Posted April 5, 2009 Hi there, Finally got all the other issues at the other end resolved thanks to the helpful people here on this board and now I've just got to fix the form. The way I've had to structure things in the database I'm interacting with means that the API call I'm working with now outputs a more complicated array. Here's what it now looks like: Array ( [0] => Array ( [ListCustomField] => Array ( [0] => Array ( [FieldName] => ReferringAgent [Key] => [ReferringAgent] [DataType] => MultiSelectOne [FieldOptions] => Array ( [string] => Array ( [0] => REDACTED [1] => REDACTED [2] => REDACTED [3] => REDACTED ) ) ) [1] => Array ( [FieldName] => Referring Agent 1 [Key] => [ReferringAgent1] [DataType] => MultiSelectOne [FieldOptions] => Array ( [string] => JACK AND JANE ) ) [2] => Array ( [FieldName] => Signature Image [Key] => [signatureImage] [DataType] => MultiSelectOne [FieldOptions] => Array ( [string] => jackandjane ) ) ) ) ) I've tried all sorts of things to change and amend the following code provided by SemiApocalyptic, but whatever I do the drop down box doesn't get populated with anything. Here's the code from above which worked perfectly when there was a simple array with only one set of values: <?php echo "<select name='foo'>"; foreach($array['anyType']['ListCustomField']['FieldOptions']['string'] as $key => $value) { echo "<option value='$key'>$value</option>"; } echo "</select>"; ?> I now need to get the data from Array 1 (ReferringAgent1). There'll be more fields in that one obviously. Does anyone know how I can amend SemiApocalyptic's code so that it fetches that data and populates the drop down with it? Thanks so much! Link to comment https://forums.phpfreaks.com/topic/149422-simple-drop-down-population-with-php/#findComment-801528 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.