【问题标题】:How to loop array data to generate HTML select dropdown?如何循环数组数据以生成 HTML 选择下拉列表?
【发布时间】:2014-01-05 15:43:49
【问题描述】:

我想在我的网上商店中创建产品的动态下拉列表,但我在循环数据时遇到了一些问题。我想用 PHP 循环数据。我的输出是数据数组,它看起来像这样:

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)

而想要的效果应该是这样的。

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>

有人可以帮我吗?

【问题讨论】:

  • 使用简单的foreach 循环。到目前为止,您尝试过什么?
  • 我滥用了 foreach 循环,但并没有这样做。 :) 现在我需要新的角度,这就是我发布这个问题的原因!
  • foreach 循环的解决方案是什么?
  • 使用foreach循环会得到什么。
  • @DavorBramorPečnik 请向我们展示您的尝试,以便我们发现您代码中的错误。

标签: php html select if-statement foreach


【解决方案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
}

【讨论】:

    【解决方案2】:

    首先,您可以使用此代码 (DEMO) 将它们分开

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

    然后使用两个不同的foreach循环,一个用于colors,一个用于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>";
    

    【讨论】:

    • 非常好!天呐!还有一个问题......我必须假设,有时产品没有属性,有时它们有尺寸和性别,有时只有性别......如果你明白我的意思吗?这种选择必须是动态的,基于每个产品的数据库数据...
    • 那么你也应该有另一个性别数组,但你必须确定数据。
    • 如果你的数据库中有 x 或者说 100 个选择怎么办?我不想确定数据:)
    • 您需要一种方法来找出您要构建的选择和数据源,如果该源包含某些类型的数据供选择然后构建它。
    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2021-09-29
    • 2011-05-20
    • 1970-01-01
    • 2016-01-29
    相关资源
    最近更新 更多