1

我有一个表 General 的结构是这样的

----------------------------------
id    |    name       | parent_id
------------------------------------
1     | root_category | Null
2     | Appare        | 1
3     | Accessories   | 1
4     | Shirt         | 2
5     | Pants         | 2
6     | hand Bags     | 3
7     | jewelry       | 3

我使用 from 在我的产品表中存储通用表的值

包含有关产品的所有信息的产品表

在产品表中


p_id       | 1 
p_name     | bla
p_weight   | 250
g_id       | 5      
g_name     | pants
g_parent   | 2

我想使用生成完整的树

->g_id -> g_name -> g_parent

树应该是这样的

Root Category
 - Apparel 
   -- Shirts
   -- Pants 
 - Accessories
   -- Handbags 
   -- Jewelry

我试过递归函数,但它不起作用

function get_categories($parent = $g_parent)
{
    $html = '<ul>';
    $query = mysql_query("SELECT * FROM `general` WHERE `parent_id` = '$parent'");
    while($row = mysql_fetch_assoc($query))
    {
        $current_id = $row['id'];
        $html .= '<li>' . $row['name'];
        $has_sub = NULL;
        $has_sub = mysql_num_rows(mysql_query("SELECT COUNT(`parent_id`) FROM `general` WHERE    `parent_id` = '$current_id'"));
        if($has_sub)
        {
        $html .= get_categories($current_id);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}

print get_categories();

但它不工作.... :(

4

3 回答 3

2

你应该试试这个......它的工作代码..还看到输出屏幕截图(输出屏幕

<?php

function get_categories(){

    $con = mysql_connect("localhost","root","");
    if (!$con){
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("stackoverflow", $con);

    $result = mysql_query("SELECT g.`name` AS `category`, GROUP_CONCAT(gj.`name`) AS `name`
    FROM `general` g INNER JOIN `general` gj ON g.`id` = gj.`parent_id` GROUP BY g.`id` ");


    $i=0;
    while($row = mysql_fetch_array($result)){
        $data[$i]['category'] = $row['category'];
        $data[$i]['name'] = $row['name'];
        $i++;
    }

    $html = $data[0]['category']."<br>";
    unset($data[0]);

    foreach($data as $key => $listing){

        $html           .= " - ".$listing['category']."<br>";
        $exp             = explode(",",$listing['name']);
        $count_exp   = count($exp);

        for($j=0; $j<=$count_exp-1;$j++){
            $html      .= "  -- ".$exp[$j]."<br>";
        }
    }

    return $html;
}

print_r(get_categories()); 
?>

输出画面

在此处输入图像描述

于 2012-05-08T08:37:09.810 回答
0

改变这个

print get_categories();

print get_categories(1);

空字符串不会匹配 MySQL 中的 NULL 值,因此您需要提供起始 id。除非你想修改你的函数来额外检查 $parent 是否为空。

于 2012-05-08T06:59:21.963 回答
0

尝试一些改变:

function get_categories($parent)
{
    $html = '<ul>';
    $query = mysql_query("SELECT * FROM `general` WHERE `parent_id` = '$parent'");
    while($row = mysql_fetch_assoc($query))
    {
        $current_id = $row['id'];
        $html .= '<li>' . $row['name'];
        $has_sub = NULL;
        $has_sub = mysql_num_rows(mysql_query("SELECT COUNT(`parent_id`) as count FROM `general` WHERE    `parent_id` = '$current_id'")); // change here
        if($has_sub['count'] > 0) // change here
        {
        $html .= get_categories($current_id);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}

print get_categories(1); // change here
于 2012-05-08T07:31:44.023 回答