您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在选择列表中显示类别,子类别,子子类别-php / mysql?

如何在选择列表中显示类别,子类别,子子类别-php / mysql?

假设给定的数组在$ array中,则可以使用它。但是,正如我 已经告诉您的那样,您应该选择ID以处理具有相同名称的类别,并将 其用作选择框中的选项值:

  $options = get_options($array);
  echo "<select>";
  foreach($options as $val) {
    echo "<option>".$val."</option>";
  }
  echo "</select>";

  function get_options($array, $parent="", $indent="") {
    $return = array();
    foreach($array as $key => $val) {
      if($val["parent category"] == $parent) {
        $return[] = $indent.$val["category name"];
        $return = array_merge($return, get_options($array, $val["category name"], $indent."&nbsp;&nbsp;&nbsp;"));
      }
    }
    return $return;
  }

假设您现在在数组中拥有的ID为“ category_id”和 “ parent_category_id”,则可以使用它。$ return键之前的“ x” 只是为了避免PHP更改您的键,因为它们是数字。

  $options = get_options($array);
  echo "<select>";
  foreach($options as $key => $val) {
    echo "<option value='".substr($key,1)."'>".$val."</option>";
  }
  echo "</select>";

  function get_options($array, $parent=0, $indent="") {
    $return = array();
    foreach($array as $key => $val) {
      if($val["parent_category_id"] == $parent) {
        $return["x".$val["category_id"]] = $indent.$val["category name"];
        $return = array_merge($return, get_options($array, $val["category_id"], $indent."&nbsp;&nbsp;&nbsp;"));
      }
    }
    return $return;
  }
php 2022/1/1 18:39:36 有466人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶