dsutuyxe088689 2016-08-22 08:59
浏览 18
已采纳

下拉列表中的选定值不应重复

Here is my database field.

    cat_id | cat_name   | parent    | parent_id
-----------------------------------------------
    1      | Electronics| --        | 0

    2      | Apparels   |--         |0

    3      | Mens       | Apparels  |2

Here is my database structure, Now I am developing some basic CRUD operations where if admin wants to edit category he can, so when he edit the selected category name should be shown in dropdown and also other options if he want to change then he can. I am getting the repeated category name here I show you.

Here is my fields:

Category Name: Mens
Dropdown Menu: --Root Level--
               Apparels(Selected Value)
               Electronics
               Apparels
                --Mens

This was design structure. Here is my php code:

<tr>
                <td><h4>Update category</h4></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><?php $result=mysql_query("SELECT * from category Where cat_id='$id'");
                            $row=mysql_fetch_array($result)?><input type="text" value="<?php echo $row["cat_name"]; ?>" /></td>
            </tr>
            <tr>
                <?php 
                      function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') {

                        if (!is_array($user_tree_array))
                          $user_tree_array = array();

                        $sql = "SELECT `cat_id`, `cat_name`, `parent_id` FROM `category` WHERE 1 AND `parent_id` = $parent ORDER BY cat_id ASC";
                        $query = mysql_query($sql);
                        if (mysql_num_rows($query) > 0) {
                          while ($row = mysql_fetch_object($query)) {
                            $user_tree_array[] = array("id" => $row->cat_id, "name" => $spacing . $row->cat_name);
                            $user_tree_array = fetchCategoryTree($row->cat_id, $spacing . '&nbsp;->', $user_tree_array);
                          }
                        }
                        return $user_tree_array;
                      }?>

                      <?php 
                      $categoryList = fetchCategoryTree();
                      ?>
                <td> Category:</td><td><select name="parentcat" id="parentcat" onchange="getText(this)">
                <option value="Root">--Root Level--</option>
                <option selected value="<?php echo $row["parent_id"];?>"><?php echo $row["parent"];?></option>
                  <?php foreach($categoryList as $cl) { 
                        ?>

                    <option value="<?php echo $cl["id"] ?>"><?php echo $cl["name"]; ?></option>
                  <?php } ?>
                  </select>
                    <input type="hidden" id="catnamehidden" name="hdnCatname">
  • 写回答

1条回答 默认 最新

  • drmy1050 2016-08-22 13:17
    关注

    when you are calling $categoryList = fetchCategoryTree(); this you are not passing parent category id that is why it is giving you all the category starting from the parent 0 and so you are getting repeating values. Try passing parent id in that function like this.

    $categoryList = fetchCategoryTree($row["parent_id"],$spacing = '', $user_tree_array = '');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输