Php 如果产品与类别关联,则返回类别的非常复杂的查询

Php 如果产品与类别关联,则返回类别的非常复杂的查询,php,mysql,function,loops,Php,Mysql,Function,Loops,我有以下功能,仅当至少有1个产品与类别关联时,才返回带有产品类别的ul菜单。函数如下所示: function getProductCategorieshome() { $query = 'select id, category, title from products_categories where visible="1" and

我有以下功能,仅当至少有1个产品与类别关联时,才返回带有产品类别的ul菜单。函数如下所示:

function getProductCategorieshome() {

    $query = 'select id, 
                     category, 
                     title 
              from products_categories 
              where visible="1" and 
                    parent="0" 
              group by category 
              order by category ASC';

    $result = mysql_query($query) or 
                die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);

    $num_rows = mysql_num_rows($result);

    if($num_rows){
        echo '<ul id="menu" style="list-style:none;">';
        $htm = '';

        for($i=0; $i<$num_rows; $i++) {
            $row = mysql_fetch_row($result);

            //sub category
            $query = 'select pc.id, 
                             category, 
                             pc.title, 
                             p.id, 
                             p.new 
                      from products_categories pc,
                           products_to_categories ptc, 
                           products p 
                      where visible="1" and 
                            parent="'.$row[0].'" and 
                            pc.id=ptc.category_id and 
                            p.id=ptc.product_id and 
                            p.new="1" and 
                            ( expire_date>now() or expire_date=0) 
                      group by category 
                      order by category ASC';

            $result1 = mysql_query($query) or 
                         die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);

            $num_rows1 = mysql_num_rows($result1);

            $q = 'select pc.id, 
                         category, 
                         pc.title, 
                         p.id, 
                         p.new 
                  from products_categories pc, 
                       products_to_categories ptc, 
                       products p 
                  where pc.id="'.$row[0].'" and 
                        pc.id=ptc.category_id and 
                        p.id=ptc.product_id and 
                        p.new="1" and 
                        ( expire_date>now() or expire_date=0) 
                  group by category 
                  order by category ASC';

            $r = mysql_query($q) or 
                    die('Mysql Error:'.mysql_error().'<br /> Query:'.$q); 

            $num_rows2 = mysql_num_rows($r);

            if($num_rows1>0) {
                $sub_htm='';
                for($j=0; $j<$num_rows1; $j++){
                    $row1 = mysql_fetch_row($result1);
                    $sub_htm .= '<li style="list-style:none;text-align:left;">
                                 <a href="./shop/index.php?offers='.$row1[0].'" title="'.$row1[2].'">'.$row1[1].'</a></li>';
                }
                if(!empty($sub_htm)) {
                    $htm .= '<li style="list-style:none;text-align:left;">
                            <a href="./shop/index.php?offers='.$row[0].'" title="'.$row[2].'">'.$row[1].'</a><ul>'.$sub_htm.'</ul></li>';
                }
            } else {
                for($s=0; $s<$num_rows2; $s++){
                    $rr = mysql_fetch_row($r);
                    $htm .= '<li style="margin:0px;padding:0px;text-align:left;">
                    <a href = "./shop/index.php?offers='.$rr[0].'" title="'.$rr[2].'">'.$rr[1].'</a></li>';
                }
            }
        }

        echo $htm;
        echo '</ul>';
    }
}        
函数getProductCategorieshome(){ $query='选择id, 类别 标题 从产品类别 其中可见=“1”和 parent=“0” 按类别分组 按类别ASC'排序; $result=mysql\u query($query)或 die('Mysql错误:'.Mysql_错误()。
查询:'.$Query); $num\u rows=mysql\u num\u rows($result); 如果($num_行){ echo'
    ; $htm=''; 对于($i=0;$i0){ $sub_htm='';
    对于($j=0;$j而言,最简单的解决方案是从产品类别中选择不同的类别:

    从可见=1的产品类别中选择不同的类别

    然后制作一个db程序,用于构建从每个叶节点到根节点的树路径

    DELIMITER $$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `treePath`(in category_id int)
    BEGIN
    DECLARE parent int;
    DECLARE current int;
    SET @parent := NULL;
    SET @current := category_id;
    CREATE TEMPORARY TABLE tree_path_tmp(id int);
    -- TRUNCATE tree_path_tmp;
    WHILE @current DO
        INSERT INTO tree_path_tmp SELECT @parent := parent_id FROM categories WHERE id = @current;
                SET @current := @parent;
    
    END WHILE;
        SELECT * FROM tree_path_tmp;
    END$$
    
    DELIMITER ;
    
    从现在起,一旦将proc存储在RDBMS中,就可以通过调用

    CALL treePath($categoryThatHasVisibleProducts_id);
    
    从那里…祝你好运!:)现在可以构建所有非隐藏类别的数组

    顺便说一句,请参见嵌套集模型


    您对SQL查询或PHP循环有问题吗?请帮助我们使您的代码易于阅读。这里看起来是使用JOIN的理想场所。JOIN where?在函数的哪个阶段?如果没有更多上下文,很难提供帮助下一次:请格式化您的代码。尝试阅读itEnterx,这让我很头疼这是一个非常有趣的方法,这将是我第一次处理它。它不是生成菜单的较短版本吗?在链接示例中,它看起来很长。?entrex我会接受你的答案,即使我以另一种方式成功了!感谢Core7,较短版本?我不知道,没有研究过这个主题,但无论如何,这使p编程是一门艺术。