【问题标题】:Nested comments pagination will not work for children comments嵌套评论分页不适用于儿童评论
【发布时间】:2021-03-04 15:37:39
【问题描述】:

由于 Luuk,硬限制有效,但分页仍然无效。

基于我之前的问题,我目前正在为一个博客构建 cmets 系统,并希望使它像 reddit 一样。

我目前正在使用 http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/ 的嵌套 cmets

class Threaded_comments  
{  
      
    public $parents  = array();  
    public $children = array();  
  
    /** 
     * @param array $comments  
     */  
    function __construct($comments)  
    {  
        foreach ($comments as $comment)  
        {  
            if ($comment['parent_id'] === NULL)  
            {  
                $this->parents[$comment['id']][] = $comment;  
            }  
            else  
            {  
                $this->children[$comment['parent_id']][] = $comment;  
            }  
        }          
    }  
     
    /** 
     * @param array $comment 
     * @param int $depth  
     */  
    private function format_comment($comment, $depth)  
    {     
        for ($depth; $depth > 0; $depth--)  
        {  
            echo "\t";  
        }  
          
        echo $comment['text'];  
        echo "\n";  
    }  
      
    /** 
     * @param array $comment 
     * @param int $depth  
     */   
    private function print_parent($comment, $depth = 0)  
    {     
        foreach ($comment as $c)  
        {  
            $this->format_comment($c, $depth);  
  
            if (isset($this->children[$c['id']]))  
            {  
                $this->print_parent($this->children[$c['id']], $depth + 1);  
            }  
        }  
    }  
  
    public function print_comments()  
    {  
        foreach ($this->parents as $c)  
        {  
            $this->print_parent($c);  
        }  
    }  
    
} 

这是以数组形式提供的数据的示例用法。请记住,如果您的数据是其他格式,则必须修改类。

$comments = array(  array('id'=>1, 'parent_id'=>NULL,   'text'=>'Parent'),  
                    array('id'=>2, 'parent_id'=>1,      'text'=>'Child'),  
                    array('id'=>3, 'parent_id'=>2,      'text'=>'Child Third level'),  
                    array('id'=>4, 'parent_id'=>NULL,   'text'=>'Second Parent'),  
                    array('id'=>5, 'parent_id'=>4,   'text'=>'Second Child')  
                );  
  
$threaded_comments = new Threaded_comments($comments);  
  
$threaded_comments->print_comments();  

示例输出:

Parent
    Child
        Child Third level
Second Parent
    Second Child

我已经设置了深度限制,创建了 html 输出和所有内容,但问题是分页。每当我尝试通过偏移限制进行分页时,它会破坏子 cmets 和结构,因为下一页没有原始父级在哪里附加子级等。

例如:

select count(*) as found_comments from comments where blog_post = 1 and parent_id is null;
result = 20; // lets say its 20 parent comments limit from query above.

    $query = "select * from comments where blog_post = 1 LIMIT 0,20";

    $comments = [];
    foreach ($query as $row)
    {
    $comments[] = [
'id' => $row['id'], 
'parent_id' => $row['parent_id'], 
'text' = $row['text'], 
'added' => $row['added']];
    }

$threaded_comments = new Threaded_comments($comments);  
  
$threaded_comments->print_comments(); 

此代码将限制父和子 cmets,但我需要将其设置为仅限制父母,因此每页 2​​0 个父母。

如果我在计数查询中设置WHERE blog_post = 1 AND parent_id = 0 它只会计算父母,但会在 $comments 查询中达到 20 个配额时删除子 cmets

找到一种方法来做到这一点真的很头疼。任何帮助表示赞赏。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    如何将代码更改为仅打印 2 个父母的示例:

    public function print_comments($count =2)  
    {  
        foreach ($this->parents as $c)  
        {  
            $this->print_parent($c);  
            $count--;
            if($count == 0) exit;
        }  
    }  
    

    【讨论】:

    • 哇,这太棒了,而且是一个如此简单的解决方案。非常感谢。
    • 哦,但现在的问题是分页。我仍然需要在 $cmets 查询中设置偏移量限制,以便我可以分页,如果我在那里设置它,它仍然会削减 cmets。
    • 你想做什么只有部分线程适合?你会打印那部分,然后继续你离开的下一页吗?还是别的什么?存在太多变体,无法给出 1 个解决方案。
    • 例如我们限制 10 个父母,它会将所有孩子打印到这 10 个父母,并且在下一页将给接下来的 10 个父母及其所有孩子,依此类推。我猜 $cmets 查询必须在 PS 类中:这就是一篇博文的全部内容。
    • 这里是示例查询$limit = 10; $offset = $_GET['page']; $query = SELECT * FROM comments WHERE blog_post = 1 LIMIT $offest, $limit,现在是foreach($query as $row) { $comments[] = ['id' => $row['id'], 'parent_id' => $row['parent_id'], 'text' => $row['text']];}
    猜你喜欢
    • 1970-01-01
    • 2021-09-09
    • 2012-09-17
    • 2014-08-28
    • 1970-01-01
    • 2021-02-01
    • 2015-09-01
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多