我想写一个分类信息的cms 
但是表结构不知道如何设计好,苦想了几天仍没有好的解决办法 我现在的想法是: 
所有分类共用一个主表,主表里面有字段:id,标题,.... 
但每个分类所用的字段类型和数目是不一样的,如果都放在主表当中,自己感觉不好. 
例如: 
如果是:房屋分类,就添加一附加表里面有字段:室型,面积,年限 ,房屋分类下面的子分类:租房,卖房,中介,也可以利用这个附加表 
如果是:汽车分类,就添加一附加表里面有字段:年限,颜色,公里数 ,汽车分类下的子分类也可以利用这个附加表 
如果是:交友分类,就添加一附加表里面有字段:年龄,爱好,工作 ,交友分类下的子分类也可以利用这个附加表 
但是如果每个分类都新建立一个附加表的话,我认为不太合理,请大家指点,给出表结构. 

解决方案 »

  1.   

    楼主的表结构其实已经挺好了,只不过有用些信息好像重复了。我觉得主表应该是一些附表通用的信息。可是你的主表和附表信息有很多的重复,汽车颜色放到主表中,同时又放到附表中,显然重复了。像这样的应该用一个表,不要主表,用附表代替主表。
    汽车表,交友表,房子表。只建一个表。对于房子表中的房子的类型 用一个字段就可以表示了。
    对于有关系的可以再建一个表 如 交友表中有人有房子 汽车等。
    可以建个关系表 have 字段只要人的编号,车子的编号,房子的编号,就行了可以用来存这些关系信息。
    有什么问题再讨论。qq 471202412
      

  2.   

    <?php
    //无限分类类
    class sortClass
    {
    /*
    getTree();返回排序好的节点数组,每个节点的深度已经整理好了
    getList($root,$self);//获取指定节点下面的子节点,$root指节点id,self指是否包括自身节点id,0为不包括,默认为1
    getList(0,0)获取所有节点,不包括“顶级节点”,因为顶级节点没有存放到lineRs数组当中去,一般也用不到
    其它情况正常使用
    getPosition($id,$self=1);返回当前位置,默认包括自己
    getRootFid($id)//获取当前分类的最顶级分类id
    */
    public $child = array();
    public $parent = array();
    public $name = array();
    public $lineRs = array();//行记录
    function __construct($sql)//构造函数
    {
    global $db;
    $this->setNode(0, -1, '顶极节点');
    $result = $db->query($sql);
    foreach($result as $val)
    {
    $this->lineRs[$val["id"]] = $val;
    $this->setNode($val["id"],$val["fid"]);
    }
    $this->initDepth();
    }
    function getTree()
    {
    return $this->lineRs;//构造函数返回初始化好的节点数组
    }
    function setNode($id,$parent)
    {
    $this->child[$parent][] = $id;
    $this->parent[$id] = $parent;
    }
    function initDepth($root=0,$depth=0)//初始化节点深度
    {
    if (!isset($this->child[$root])) return;
    foreach ($this->child[$root] as $key=>$id)
    {
    $this->lineRs[$id]["depth"] = $depth;
    if (isset($this->child[$id]))
    {
    if ($this->child[$id]) $this->initDepth($id,$depth+1);
    }
    }
    }
    function getList($root=0,$self=1)
    {
    $tree = array();
    if($self == 1)
    {
    $tree[] = $root;
    }
    $this->getList_1($root,$tree);
    return $tree;
    }
    function getList_1($root,&$tree)
    {
    if (!isset($this->child[$root])) return;
    foreach ($this->child[$root] as $key=>$id)
    {
    $tree[] = $id;
    if (isset($this->child[$id]))
    {
      $this->getList_1($id,$tree);
    }
    }
    }
    function getPosition($id,$self=1)
    {
    $position = array();
    if($self == 1)
    {
    $position[] = $id;
    }
    while($this->parent[$id] != 0)
    {
    $id = $this->parent[$id];
    $position[] = $id;
    }
    //return implode(array_reverse($position)," → ");
    return array_reverse($position);
    }
    function getRootFid($id)
    {
    while($this->parent[$id] != 0)
    {
    $id = $this->parent[$id];
    }
    return $id;
    }
    }
    //使用方法
    //include("include.php");
    //$sql = "select * from aa order by orderId asc,id asc";
    //$sort = new sortClass($sql);
    //$tree = $sort->getTree();
    //$childes = $sort->getList(0,0);
    //foreach($childes as $key=>$id)
    //{
    // echo str_repeat("-",$tree[$id]["depth"]) . $tree[$id]["name"] . "<br>";
    //}
    //echo "<hr>";
    //$postion = $sort->getPosition(5);
    //foreach($postion as $key=>$id)
    //{
    // echo $tree[$id]["name"] . " -> ";
    //}
    ?>