首页 > 解决方案 > 如何从它的孩子那里得到父母的名字?

问题描述

我将为我的最后一年项目建立一个动态的产品类别。我尝试了树方式,但不知何故让我感到困惑,所以我决定让它变得简单。我想显示 Clothe 的父名。我想这样显示我可以这样做吗?

这是我的PHP代码

<form method="post" action="product_category_add_exec.php" enctype="multipart/form-data">

                                    <div class="form-group">
                                        <label for="recipient-level" class="control-label"> Parent Category</label>
                                        <select class="form-control" name="admin_lid" required="">                                    

                                            <option></option>
                                    <?php
                                            $sql_pcat = "SELECT * FROM product_category";
                                            $select_pcat = mysqli_query($db,$sql_pcat) or die (mysqli_error().$sql_pcat);

                                            $x =1;

                                            while($list_pcat = mysqli_fetch_array($select_pcat))
                                            {

                                                $product_cat_id = $list_pcat['product_cat_id'];
                                                $parent_id = $list_pcat['parent_id'];
                                                $product_cat_name = $list_pcat['product_cat_name'];
                                    ?>

                                            <?php 
                                                    if ($parent_id == 0)
                                                    {
                                            ?>
                                            <option value = "<?php echo $product_cat_id;?>"><?php echo $product_cat_name; ?></option>
                                            <?php
                                                } else 
                                                    {
                                                $sql_cat = "SELECT * FROM product_category WHERE parent_id= $parent_id ORDER BY product_cat_name ASC";
                                                $select_cat = mysqli_query($db,$sql_cat) or die (mysqli_error().$sql_cat);



                                                $list_cat = mysqli_fetch_array($select_cat);


                                                $product_cat_id = $list_cat['product_cat_id'];
                                                $parent_id = $list_cat['parent_id'];
                                                $product_cat_name = $list_cat['product_cat_name'];  

                                            ?>

                                            <option value = "<?php echo $parent_id;?>">--<?php echo $parent_id;?><?php echo $product_cat_name; ?></option>
                                            <?php
                                                }
                                            ?>
                                    <?php
                                            $x++;
                                            }
                                    ?>

                                        </select>
                                    </div>

                                    <div class="form-group">
                                        <label for="recipient-category" class="control-label">Product Name </label>
                                        <input type="text" class="form-control" id="recipient-category" name="product_cat_name">
                                    </div>
                            </div>


                                <div class="modal-footer">
                                    <button type="submit" class="btn btn-info">Add</button>
                                    <button type ="reset" class ="btn btn-danger">Reset</button>

                                </div>
                                    </form>


这是我的数据库表 图像

我想让它看起来像这样

图像

标签: phpmysql

解决方案


推荐阅读