0

I am building a form using the podio API and Podio-php library. It connects to an app in podio, with fields already defined, and the form creates an item for that app. One of the fields is a category field. I would like to be able to get the categories, loop through them and display them in a dropdown so that users chan choose one. I could simply write the categories in the html and use the category id as the value attribute like this : (I found the categories ID through here : https://developers.podio.com/doc/applications/get-app-22349 ) (I removed the rest of the form to simplify)

<form method="POST" action="send_form.php">
    <div>
        <label for="name">Name :</label>
        <input type="text" id="name" name="name">
    </div>

    <div>
        <label for="category">Category :</label>
        <select id="category" name="category">
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
         </select>
    </div>
</form>

And here is the relevant part of of the php file that sends the form :

$name = ucfirst(format($_POST['name']));
$category = (int)$_POST['category'];

    // assign values to fields
    $item_fields = array(
        'name' => $name,
        'category' => $category,
    );

    // Create the Podio item
    $item = PodioItem::create($app_test_id, array('fields' => $item_fields));

But instead of doing this, I would like to make a loop where I get the possible categories, assign their name to the content of the tag and assign their id number to the value attribute. The goal is to make it easier to add a new category to the list in Podio without editing the code of the form.

Is there a way to do this?

I wrote a loop to get all the items in an app and make them options in a dropdown that works. I would like to modify this loop so that instead of items in an app it gets the options of a category field. Here is my example : Of course here $i would have to be the ID number of the option, not simply an incremented variable (as the options can be reordered and their ID doesnt change, the IDs are not ordered).

$items_list = PodioItem::filter($app_that_contains_the_list_of_categories_as_items);

function fillDropdown($items_list) {
    $i = 1;
    foreach ($items_list as $item) {
        echo "<option value='" . $i . "'>" . $item->title . "</option>";
        $i = $i + 1;
    }
}

and the html for the would simply be : (the html is in a php file)

<select id="category" name="category">
    <option value=''>Please choose an option</option>
    <?php fillDropdown($items_list); ?>
</select>

Note: There shouldnt ever be a need to have more than 15 category options.

omnikin
  • 1
  • 1

0 Answers0