douleng3463 2017-09-05 18:26
浏览 14

无法根据PHP中的类别显示MySQL列表

I have two tables in MySQL, categories and subcategories and I'm using them to populate a dropdown list.

The arrangement that I want is like this; where when a specific category is selected the dropdown-item should be according to the main category

Problem: When a category is selected the dropdown-item is the same like this to all parent categories

This is the method I used to get the categories from the database

public function get_categories($category_table)
{
    // PURPOSE: get categories from the database
    $sql = "SELECT * FROM $category_table";
    $conn = $this->mysql_conn;
    $result = $conn->query($sql);

    while ($row = $result->fetch_assoc()) {
        // var_dump($row['category_name']);
        $html = '<a href="#" class="dropdown-toggle list-group-item" data-toggle="dropdown">' . $row["category_name"] . '<span class="caret"></span></a>
          <ul class="dropdown-menu">'; 

        //PURPOSE: get subcategories from database and pass it to the get_categories method
        $subsql = "SELECT subcategory_name FROM subcategories WHERE category_id={$row['id']}";
        $conn = $this->mysql_conn;
        $subresult = $conn->query($subsql);

        while ($subrow = $subresult->fetch_assoc()) {
            // extract($subresult);
            // echo $subrow['subcategory_name'] . "<br>";
            $subcat_name = array();
            $subcat_name = $subrow['subcategory_name'];
            // var_dump($subrow);
         $html .= '<li><a href="#">' . $subcat_name . '</a></li>';
        }
         $html .= '</ul>';
        echo $html;
    }
}

THis is the html where I insert this method

<div class="dropdown list-group">
        <a href="#" class="list-group-item">Latest Products</a>
        <?php $store->get_categories('categories'); ?>
      </div>
  • 写回答

1条回答 默认 最新

  • douyingyu5573 2017-09-06 02:56
    关注

    You are using a list to behave like a dropdown. This will require some fancy javascript and CSS. Which library (if any) are you using to automate that.

    I use bootstrap Bootstrap dropdown documentation

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错