0

I have a little problem.

I'm searching the internet now for quite some time to find a proper solution but I was not successful so far.

This is what I want :

  • First, I choose Category

  • Then, in second selection contain all the store result from category selection.

this is my first drop down list which is contain Category :

<select name="category" class="form-control" id="select1">
    <option value="-1"> - Choose One -</option>
         <?php
           $StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
           $stores = $StoreCategoriesAPIAccessor->getStoreCategories();
           foreach ($stores as $store):      
         ?>
     <option value="<?php echo $store->getStoreCategoryId(); ?>"><?php echo $store->getCategory(); ?></option>
         <?php endforeach; ?>
 </select>

this is my second drop down list :

<select name="category" class="form-control" id="select2">
    <option value="-1"> - Choose One -</option>
         <?php
            $StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
            $stores = $StoreAPIAccessor->getStores();
            foreach ($stores as $store):      
         ?>
         <option value="<?php echo $store->getStoreId(); ?>"><?php echo $store->getStoreName(); ?></option>
          <?php endforeach; ?>
</select>

Anyone know how to implement dynamic drop down list for this case ?

1
  • What does the StoreAPIService class look like? Commented Jan 21, 2015 at 19:03

1 Answer 1

1

Well first of all I'd like to ask that you separate business logic from view logic as much as possible, so I will do that in this answer.

Secondly, the 2nd dropdown box logic that you have does not contain anything that would retrieve stores for a given category, I will therefore make some assumptions and you can adjust based on that.

<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$categories = $StoreCategoriesAPIAccessor->getStoreCategories();

if (!empty($_GET['category'])) {
    $category_id = $_GET['category'];
    $StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
    $stores = $StoreAPIAccessor->getStores($category_id); // Assumption, the call to getStores() accepts category_id as a parameter
} else { // Optional as you don't need to declare variables in PHP, but its better practice to do so
    $category_id = null;
    $stores = array();
}
?>
<select id="select_category" name="category" class="form-control" onchange="window.location='?category=' + this.value">
    <option value="">- Choose One -</option>
    <?php foreach ($categories as $category): ?>
        <option value="<?php echo $category->getStoreCategoryId() ?>"><?php echo $category->getCategory() ?></option>
    <?php endforeach ?>
</select>

<select id="select_store" name="store" class="form-control"<?php echo $category_id == null ? " disabled='disabled'" : "">
    <option value="">- Choose One -</option>
    <?php foreach ($stores as $store): ?>
        <option value="<?php echo $store->getStoreId() ?>"><?php echo $store->getStoreName() ?></option>
    <?php endforeach ?>
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

foreach ($stores as $store): will throw an error if $stores is null. Perhaps foreach ($stores?:array() as $store): or set $stores to an empty array in your else clause
@Steve yea setting it to an array is better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.