0

I would like to create dynamic dropdown on products in my webstore, but i have some problem looping data. I want to loop data with PHP. My output is data array and it looks like that:

array (size=6)
  0 => 
    object(stdClass)[34]
      public 'id_attribute_option' => string '8' (length=1)
      public 'option_name' => string 'Black' (length=5)
      public 'name' => string 'Color' (length=5)
      public 'id_attribute' => string '2' (length=1)
  1 => 
    object(stdClass)[35]
      public 'id_attribute_option' => string '10' (length=2)
      public 'option_name' => string 'Green' (length=6)
      public 'name' => string 'Color' (length=5)
      public 'id_attribute' => string '2' (length=1) 
  2 => 
    object(stdClass)[36]
      public 'id_attribute_option' => string '84' (length=2)
      public 'option_name' => string 'S' (length=1)
      public 'name' => string 'Size' (length=8)
      public 'id_attribute' => string '9' (length=1)
  3 => 
    object(stdClass)[37]
      public 'id_attribute_option' => string '85' (length=2)
      public 'option_name' => string 'M' (length=1)
      public 'name' => string 'Size' (length=8)
      public 'id_attribute' => string '9' (length=1)
  4 => 
    object(stdClass)[38]
      public 'id_attribute_option' => string '86' (length=2)
      public 'option_name' => string 'L' (length=1)
      public 'name' => string 'Size' (length=8)
      public 'id_attribute' => string '9' (length=1)
  5 => 
    object(stdClass)[39]
      public 'id_attribute_option' => string '87' (length=2)
      public 'option_name' => string 'XL' (length=2)
      public 'name' => string 'Size' (length=8)
      public 'id_attribute' => string '9' (length=1)

And desired effect should be like this.

Color:<select>
      <option value="8">Black</option>
      <option value="10">Green</option>
    </select>
Size:<select>
      <option value="84">S</option>
      <option value="85">M</option>
      <option value="86">L</option>
      <option value="87">XL</option>
    </select>

Can enybody help me?

7
  • 3
    Use a simple foreach loop. What have you tried so far? Commented Dec 17, 2013 at 18:10
  • I hava abused foreach loop and that didn't do it. :) Now i need new angle so that's whay i have posted that question! Commented Dec 17, 2013 at 18:15
  • What is you solution with foreach loop? Commented Dec 17, 2013 at 18:16
  • What do you get when using foreach loop. Commented Dec 17, 2013 at 18:22
  • 1
    @DavorBramorPečnik Please show us your attempt, so we can spot the error in your code. Commented Dec 17, 2013 at 18:39

2 Answers 2

2

First of all you may separate them using this code (DEMO)

$colors = $sizes = array();
array_map(function($item) use(&$colors, &$sizes){
    if($item->name == 'Color') $colors[] = $item;
    if($item->name == 'Size') $sizes[] = $item;
}, $array);

Then use two different foreach loops, one for colors and one for size like

// BUild the Color select
echo "<select id='color'>";
foreach($colors as $color){
    echo "<option value='$color->id_attribute_option'>$color->option_name</option>";
}
echo "</select>";

// Build the Size select
echo "<select id='size'>";
foreach($sizes as $size){
    echo "<option value='$size->id_attribute_option'>$size->option_name</option>";
}
echo "</select>";
Sign up to request clarification or add additional context in comments.

4 Comments

Very good point! Tnx! There is only one more problem... I must asume, that sometimes products have no attributes, some times they have size and gender, some times only gender... If you know what i mean? This selects have to be dynamical, based on db data foreach product...
Then you should have another array for genders as well but you have to make be determined about the data.
and what if you have x or let say 100 selects in your DB? I don't want to be determined about the data :)
You need a way to find out which select you want to build and the data source, if that source contain certain types of data for select then build it.
1
foreach($yourArray as $item)
{
  // you are dealing with objects so $item->option_name for example
  // this should be easy :p
  // there's no perfect way to do most things
  // so that bottom link you pasted in the comments isn't
  // going to teach you anything but how to copy others
}

Comments

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.