若是QQ空间那种,这里无需再使用无限级分类。

而是帖子之下的评论回复使用字段标记分清从属后按时间排序就行

SouthEast

//根据回复评论表,查询出回复人和被回复人信息

select   
acrr.id,comment_id,content,time,acrr.account_id,type,mi.name as mename,mi.p_w_picpath as mimg, 
do.name as doname,do.p_w_picpath as dimg,replied_account_id,rtype,(select name from ll_member_info where account_id=replied_account_id) as rmename,  
(select name from ll_doctor_info where account_id=replied_account_id) as rdoname,  
(select p_w_picpath from ll_member_info where account_id=replied_account_id) as rmimg,  
(select p_w_picpath from ll_doctor_info where account_id=replied_account_id) as rdimg   
from   
ll_article_comment_reply_record as acrr   
left join ll_member_info as mi on acrr.account_id=mi.account_id   
left join ll_doctor_info as do on acrr.account_id=do.account_id   
where   
article_id=$article_id   
AND comment_id=$cid   
order by time asc

无限级分类:一个分类型可以有无限个分类

常见的无限分类实现

SouthEast

商品分类,子类导航,贴吧回帖

//=====1.递归方式实现无限级分类======

原理:每个分类都需记录它的父级id,当为顶级分类时,父级id0.每个分类都可通过父级id一层层查明它所在父级,以便知道它所属分类,层级深度为几。

 

递归——自己调用自身(一定要有判断)?

储存结果的变量迭代方法

function getList(&$i=1){

echo $i.'<br/>';

$i++;

if($i<20){

getList($i);

}

}

 

getList();

$i=1;

function getList(){

global $i;

echo $i.'<br/>';

$i++;

if($i<20){

getList($i);

}

}

 

getList();

function getList(){

static $i=1;

echo $i.'<br/>';

$i++;

if($i<20){

getList($i);

}

}

 

getList();

总结:

self::getList($row['id'],$result,$spac);不断递归pid

 

$sql='select * from imooc where pid='.$pid;不断按需要调整条件查询

 

function getList($pid=0,&$result=array(),$spac=0)  $result[]=$row;不断迭代

 

当使用迭代去除所有数据时,是怎样区分ta分级的。

getList($pid=0,&$result=array(),$spac=0)

用这个表示区分层数,每次调用都是new出新栈独立运算

 

 

//=====2.全路径无限分类:以一个字段把它所有父级ID按顺序记录下来=====

 

总结:

$sql="select id,catename,path,concat(path,',',id) as fullpath from imooc order by fullpath asc";

他在order by的时候就已经将序列搞好了

区分深度

$deep=count(explode(',', trim($row['fullpath'],',')));


///无限级分类_代码展示

<?
/*
 * Author:Abo
 * Date:2015/7/27
 */
/*
变成下拉列表
从pid=0递归下去
*/
class toList{
    public static $mysqli;
    
    function conn(){
        $mysqli=new mysqli('localhost','root','','test');
        $mysqli->query('set names utf8');
        if(!$mysqli) die("连接数据库失败".$mysqli->connect_error());
        return $mysqli;
    }
    
    function getList($pid=0,&$result=array(),$spac=0){
        $spac=$spac+4;
        $mysqli=self::conn();
        $sql='select * from imooc where pid='.$pid;
        $res=$mysqli->query($sql);
        while ($row=$res->fetch_assoc()) {
            $row['catename']=str_repeat(' ', $spac).'|--'.$row['catename'];
            $result[]=$row;
            self::getList($row['id'],$result,$spac);             //递归
        }
        return $result;
        if($mysqli!=null) $mysqli->close();
    }
}
/*
按要求封装数据,变成下拉列表
*/
function displayCate($pid = 0, $selected = 0) {
    $ts = new toList ();
    $ts->conn ();
    $rs = $ts->getList ();
    $temp=null;
    $str="<select name='cate'>";
    foreach ( $rs as $val ) {
        $temp=null;
        if($val['id']==$selected){
            echo $val['id'];
            $temp="selected='selected'";
        }    
        $str.="<option $temp >".$val ['catename'].'</option>';
    }
    $str.='</select>';
    return $str;
}
header("Content-Type: text/html;charset=utf8");
echo displayCate(0,2);

/*
变成导航栏
从pid=10递归上来再倒序
*/
class toList{
    public $host='localhost';
    public $username='root';
    public $password='';
    public $db='test';
    
    function conn(){
        $mysqli=new mysqli($this->host,$this->username,$this->password,$this->db);
        $mysqli->query('set names utf8');
        if(!$mysqli) die('数据库连接失败'.$mysqli->connect_error);
        return $mysqli;    
    }
    
    function getList($id,&$result=array()){
        $sql='select * from imooc where id='.$id;
        
        $mysqli=self::conn();
        $res=$mysqli->query($sql);
        if($row=$res->fetch_assoc()){
            
            $result[]=$row;
            self::getList($row['pid'],$result);
        }

        return $result;
    }
}

$ts=new toList();
$rs=$ts->getList(10);
/*
按要求封装变成地址导航
*/
 echo krsort($rs);

header("Content-Type: text/html;charset=utf8");
echo '<pre>';
print_r($rs);

//===============================全路径无限分类==================================
/*
下拉列表
*/
class toList{
    public $host='localhost';
    public $username='root';
    public $password='';
    public $db='test';

    function conn(){
        $mysqli=new mysqli($this->host,$this->username,$this->password,$this->db);
        $mysqli->query('set names utf8');
        if(!$mysqli) die('数据库连接失败'.$mysqli->connect_error);
        return $mysqli;
    }

    function getList(){
        $sql="select id,catename,path,concat(path,',',id) as fullpath from imooc order by fullpath asc";

        $mysqli=self::conn();
        $res=$mysqli->query($sql);
        $result=array();

        while($row=$res->fetch_assoc()){
            $deep=count(explode(',', trim($row['fullpath'],',')));
            $row['catename']=str_repeat(' ', $deep).'|--'.$row['catename'];    
            $result[]=$row;
//             self::getList($row['pid'],$result);
        }

        return $result;
    }
}

$ts=new toList();
$rs=$ts->getList();

header("Content-Type: text/html;charset=utf8");
$str="<select name='cate'>";
foreach ($rs as $val){
    $str.='<option>'.$val['catename'].'<option>';
}
$str.="</select>";

echo $str;


class toList{
    public $host='localhost';
    public $username='root';
    public $password='';
    public $db='test';

    function conn(){
        $mysqli=new mysqli($this->host,$this->username,$this->password,$this->db);
        $mysqli->query('set names utf8');
        if(!$mysqli) die('数据库连接失败'.$mysqli->connect_error);
        return $mysqli;
    }

    function getList($cateid){
        $sql="select * ,concat(path,',',id) fullpath from imooc where id= $cateid";

        $mysqli=self::conn();
        $res=$mysqli->query($sql);
        $row=$res->fetch_assoc();
        $ids = $row['fullpath'];
        $sql="select * from imooc where id in ($ids) order by id asc";
        $res=$mysqli->query($sql);

        $result=array();

        while($row=$res->fetch_assoc()){
            $deep=count(explode(',', trim($row['fullpath'],',')));
            $row['catename']=str_repeat(' ', $deep).'|--'.$row['catename'];    
            $result[]=$row;
//             self::getList($row['pid'],$result);
        }

        return $result;
    }
}

$ts=new toList();
$rs=$ts->getList(4);
echo '<pre>';
print_r($rs);
header("Content-Type: text/html;charset=utf8");
// $str="<select name='cate'>";
/*
按要求封装变成地址导航
*/
foreach ($rs as $val){
    echo "<a href=''>".$val['catename'].
    "</a>>";
}