Herkes php dişli yorumlarla bana yardımcı olabilir misiniz?

0 Cevap php

Ben dişli yorum oluşturmak için bir sınıf önceden yazılmış komut dosyası bulundu ama hiçbir şey baskılar bunu uygulamak için denedikten sonra. Dizi si verilerini tutan ve ben işlevi çağrıldığında ancak, hiçbir şey basacaktır, doğruladı, bu yüzden kimse yardım olabilir lütfen merak ediyordum.

betik burada bulabilirsiniz:

Ayrıca aşağıdaki şekilde de aşağıdaki gibidir:

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);
        }
    }

}

aşağıdaki gibi i germannrumm yardımıyla kullanmış kodu:

$q = $DBH->prepare("SELECT id, parent_id, comment FROM comments WHERE page = ?");
$q->bind_param("i", $page);
$q->execute();

$q->bind_result($id, $parent_id, $text);

$all_results = array();

while ($q->fetch()) {
    $all_results[] = array(
        'id' => $id, 
        'parent_id' => $parent_id, 
        'text' => $text);
}
$q->close();

Herhangi bir yardım büyük beğeni topluyor! teşekkürler!

$tc = new Threaded_comments($all_results);
$tc->print_comments();

0 Cevap