Jump to content

Simple Drop Down Population with PHP


nz_mitch

Recommended Posts

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
Share on other sites

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
Share on other sites

  • 3 weeks later...

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
Share on other sites

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.