Pagination for Threaded Comments

Discussion in 'PHP' started by Netratrix, Sep 2, 2012.

  1. #1
    I'm using the threaded comment from http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/ and I have no idea how to implement a pagination system. If anybody could help me a little because I search for a solution but didn't find anything...

    
    <?php
    require_once 'config.php';
    
    class Threaded_comments
    {
    
        public $parents  = array();
        public $children = array();
    
        function __construct($comments)
        {
            foreach ($comments as $comment)
            {
                if ($comment['parent_id'] === NULL)
                {
                    $this->parents[$comment['id']][] = $comment;
                }
                else
                {
                    $this->children[$comment['parent_id']][] = $comment;
                }
            }
        }
    
        private function format_comment($comment, $depth)
        {
             If($depth == 0){
                 
                ?>
                <br /><?php echo $comment['name']; ?><br /><?php echo $comment['datetime']; ?><br /><?php echo $comment['text']; ?></div>
                <a href="javascript:toggleDiv('<?php echo $comment['id']; ?>');">Raspunde</a>
                <div id="<?php echo $comment['id']; ?>" style="display: none;">
                The content in this div will hide and show (toggle) when the toggle is pressed. 
                </div>
                <?php
             }
             
             If($depth > 0){
                ?>
                <div style="margin-left: 20px;">
                <br /><?php echo $comment['name']; ?><br /><?php echo $comment['datetime']; ?><br /><?php echo $comment['text']; ?></div>
                </div>
                <?php
             }   
                        
                  
        }
    
        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);
            }
        }
    
    }
         
        $username = "Netra";
        $SQL = "SELECT * FROM profile_comments WHERE name = '$username' ORDER BY datetime DESC";
        $result = mysql_query($SQL) or die(mysql_error());
        
        while($row = mysql_fetch_assoc($result)) {
            $id = $row['id'];
            $parent_id = $row['parent_id'];
            $name = $row['name'];
            $text = $row['text'];
            $datetime = $row['datetime'];
            
            $comments[] = array(
                'id' => $id,
                'parent_id' => $parent_id,
                'name' => $name,
                'text' => $text,
                'datetime' => $datetime
            );
        }
        
            $total_results = new Threaded_comments($comments);
            $total_results->print_comments();
                 
    ?>
    Code (markup):
     
    Last edited: Sep 2, 2012
    Netratrix, Sep 2, 2012 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    You're going to be hard pressed finding someone to go through all of that code and fix it all up for you (at least for free). Pagination is pretty easily. You could probably offer someone as little as $5-10 to fix it up. Otherwise you may want to have a quick Google for pagination classes/solutions to see how they're done.
     
    Alex Roxon, Sep 2, 2012 IP