dtkvlj5386 2015-10-27 09:53
浏览 158

从表中获取父类和子类

I'm working myself into trouble on this one.

Currently working on a script to create slugs for products and categories in my opencart website. I've got a max 4 lvl deep structure before the product itself.

Each product stores an array with the categories. The client inserted the products in a way I'm not always having the same situation. Sometimes It's only the deepest (lvl 4) categories sometimes it's all the categories from the 4 lvl structure up to top. This works since I'm having opencart to show all products in parent categories.

So I'm working on a script that gathers all categories names upon one given category id. It has to look for possible parents and possible child.

This somehow is not leading me tot the satisfied result. But I see I'm almost at the desired point. Although my code is a mess like I've never seen before.

So.. The categories table holds the category id and his parent id. When no parent, parent id = 0 The categories names are stored in another table containing id and name.

Each product row holds a set of category id's of which I get one.

I'm sure I'm doing it all wrong since their must be a way to check for all child and parent categories and get their names via some nested loops that are a fraction of the code I have right here...

$products = $db->query("SELECT * FROM ".DB_PREFIX."product");
                          $products = $products->rows;
                          foreach ($products as $product) {
                                $url = $db->query("SELECT * FROM ".DB_PREFIX."url_alias WHERE query='product_id=".$product['product_id']."' LIMIT 1");
                                $url = $url->rows;
                                $url = $url[0];
                                $info = $db->query("SELECT * FROM ".DB_PREFIX."product_description WHERE product_id='".$product['product_id']."' LIMIT 1");
                                $info = $info->rows;
                                $info = $info[0];


                                $categories = $db->query("SELECT `bnc_category_description`.`name` , bnc_category_description.category_id FROM  `bnc_product_to_category` JOIN  `bnc_category_description` ON  `bnc_product_to_category`.`category_id` =  `bnc_category_description`.`category_id` WHERE  `bnc_product_to_category`.`product_id` ='".$product['product_id']."' LIMIT 1");
                                $categories = $categories->rows;
                                $categories = $categories[0];

                                //$i = 1;

                                $parent = $db->query("SELECT category_id, parent_id FROM bnc_category WHERE category_id ='" . $categories['category_id'] . "'");
                                $parent = $parent->rows;
                                $parent = $parent[0];


                                $link = $parent['category_id'] . "_";

                                $n = $parent['category_id'];

                                $pr = $parent['parent_id'];

                                $y = $db->query("SELECT category_id, parent_id FROM bnc_category WHERE parent_id ='" . $parent['category_id'] . "'");
                                $y = $y->rows;
                                $y = $y[0];

                                $x = $db->query("SELECT name FROM bnc_category_description WHERE category_id = '" . $y['category_id'] . "'");
                                $x = $x->rows;
                                $x = $x[0];

                                $final = $x['name'] . "_";

                                while($pr != 0){

                                    $s = $db->query("SELECT category_id, parent_id FROM bnc_category WHERE category_id ='" . $pr . "'");
                                    $s = $s->rows;
                                    $s = $s[0];
                                    $pr = $s['parent_id'];

                                    $n = $s['category_id'];

                                    $link .= $n . "_";
                                }

                                $name = $db->query("SELECT name FROM bnc_category_description WHERE category_id = '" . $test['category_id'] . "'");
                                $name = $name->rows;
                                $name = $name[0];

                                $test = $db->query("SELECT category_id FROM bnc_category WHERE parent_id='" . $n . "'");
                                $test = $test->rows;
                                $test = $test[0];

                               $final .= $categories['name'] . "_" . $name['name'];
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?