PHP MySQL评论系统及回复

PHP MySQL评论系统及回复,php,comments,system,reply,Php,Comments,System,Reply,我的代码能够捕获初始评论和第一级回复,但它似乎无法捕获回复的回复。我知道它需要使用某种形式的递归的不确定代码,但不太确定如何正确地实现它。 下面是我的phpMyAdmin表的外观: id名称评论回复\u id 我喜欢这个vst!我一直在用它!0 2德鲁玛男孩504嘿,我是鼓队的德鲁玛!0 3迈克·史密斯你是怎么让vst听起来这么好的。。。1 是的,我从YouTube上学会了如何调整它Mike S。。。3 老兄,我一直在找这样的vst有一段时间了。。。0 FanBoy123嘿,Drumma,你打算

我的代码能够捕获初始评论和第一级回复,但它似乎无法捕获回复的回复。我知道它需要使用某种形式的递归的不确定代码,但不太确定如何正确地实现它。 下面是我的phpMyAdmin表的外观:

id名称评论回复\u id
我喜欢这个vst!我一直在用它!0
2德鲁玛男孩504嘿,我是鼓队的德鲁玛!0
3迈克·史密斯你是怎么让vst听起来这么好的。。。1
是的,我从YouTube上学会了如何调整它Mike S。。。3
老兄,我一直在找这样的vst有一段时间了。。。0
FanBoy123嘿,Drumma,你打算什么时候发布一个新的hi。。。2
迈克·约翰逊嘿,歌迷123,你为什么是鼓迷。。。六,

代码如下:


文件
递归 下面是递归解决方案的一个快速示例,使用数组作为数据库查询的模型

一些重要注意事项:

  • 注意结构:所有的数据获取和操作都是在打印出一件东西之前完成的

  • 作为一个对象,这会更好,这样可以避免函数中的
    GLOBAL
    标记,但这会让您指向那个方向

  • 递归函数应该总是有一个测试来停止递归,以防出现错误。这个例子没有!您可以使用静态变量对递归进行计数,并在达到某个限制时停止



  • 要查找2的所有后代吗<代码>从回复为“/0/2%”的注释中选择*

    一些基本想法:递归不是唯一的方法。查看“物化路径”:您必须重做reply_id,但其好处可能是值得的。如果您选择继续使用递归,那么您需要采取步骤来实现更复杂的编码风格。目前,您使用的是一种非常程序化的风格:处理页面显示中发生的事情。你需要采用更多的OOP风格;至少你需要一个调用自身的函数。我建议使用中的样式,当您在顶部得到逻辑,在底部得到表示时,那么合并函数来执行递归就更容易了。感谢Tim的概念。你有我可以测试的示例或代码片段吗?我需要把它放在数据库里,但我愿意尝试你的想法。再次感谢。
    <?php
    
    // mock data of "select * from comments"
    $dbRows = [
      1 => ['name' => 'BigBadProducer1', 'comment' => 'I love this vst! I use it all the time!', 'reply_id' => 0],
      2 => ['name' => 'DrummaBoy504', 'comment' => 'Hey, this is Drumma from Drum Squad!', 'reply_id' => 0],
      3 => ['name' => 'Mike Smith', 'comment' => 'How did you get the vst to sound so good like that...', 'reply_id' => 1],
      4 => ['name' => 'BigBadProducer1', 'comment' => 'Yes, I learned how to tweak it from YouTube Mike S...', 'reply_id' => 3],
      5 => ['name' => 'SmoothBeatz3', 'comment' => 'Dude, Ive been looking for a vst like this for a l...', 'reply_id' => 0],
      6 => ['name' => 'FanBoy123', 'comment' => 'Hey Drumma, when are you going to release a new hi...', 'reply_id' => 2],
      7 => ['name' => 'Mike Johnson', 'comment' => 'Hey Fanboy123, why are you such a fanboy of Drum S...', 'reply_id' => 6],
      ];
    
    // mock data of "select id from comments where reply_id=?"
    $children = [];
    foreach($dbRows as $id => $row) {
      $reply_id = $row['reply_id'];
      $children[$reply_id][] = $id;
    }
    
    // format row into html
    function formatRow($row, $reply_name='unknown') {
        $out =<<<FORMATROW
    <div class="replies" style="position:relative; margin:auto; width:500px; border:1px solid black; margin-top:1px;">
    <div style="width:80%; text-align:center;">{$row['name']} replied to {$reply_name}</div>
    <div style="width:80%; text-align:center;">{$row['comment']}<br><br></div>
    </div>
    FORMATROW;
    
        return $out;
    }
    
    // mock of database CRUD function "select * from comments where id=?"
    function find($id) {
        global $dbRows;
        if(array_key_exists($id, $dbRows)) {
            return $dbRows[$id];
        }
    }
    
    // mock of database CRUD function "select id from comments where reply_id=?"
    function findChildren($id) {
        global $children;
        if($id == 0) { return []; }
    
        $out = [];
        if(array_key_exists($id, $children)) {
    
            $out = $children[$id];
        }
        return $out;
    }
    
    function getRepliesTo($id) {
    
        // test to end recursion
        // if(!$id) { return; }
    
        // get parent name
        $row = find($id);
        $parent_name = $row['name'];
    
        // start indented list every time a traversal is called
        $out = "<ul>\n";
    
        // list of child ids. if no children are found, 
        // assigns an empty array so that foreach won't barf
        $children = findChildren($id);
    
        foreach($children as $cid) {
    
            // if there are children, capture their reply and then call this same function to get replies to this reply            
            if($reply= find($cid)) {
                $out .= '  <li>' . formatRow($reply, $parent_name);
                $out .= getRepliesTo($cid);
                $out .= "</li>\n";
            }
        }
    
        $out .= "</ul>\n";
        return $out;
    }
    
    // print_r(find(1));
    // print_r(findChildren(1));
    // print_r(findChildren(2));
    // print traverse(1);
    
    $id = 1; // $id = (int)$_GET['id'];
    
    $commentData = find($id);
    $replyHtml = getRepliesTo($id);
    
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
      <div class="comments" style="position:relative; margin:auto; width:500px; border:1px solid black; margin-top:1px;">
        <div><?= $commentData['name'] ?></div>
        <div><?= $commentData['comment'] ?><br><br></div>
      </div>
      <?= replyHtml ?>
    </body>
    </html>
    
    $dbRows = [
      1 => ['name' => 'BigBadProducer1', 'comment' => 'I love this vst! I use it all the time!',               'reply_id' => '/0/1'],
      2 => ['name' => 'DrummaBoy504',    'comment' => 'Hey, this is Drumma from Drum Squad!',                  'reply_id' => '/0/2'],
      3 => ['name' => 'Mike Smith',      'comment' => 'How did you get the vst to sound so good like that...', 'reply_id' => '/0/1/3'],
      4 => ['name' => 'BigBadProducer1', 'comment' => 'Yes, I learned how to tweak it from YouTube Mike S...', 'reply_id' => '/0/1/3/4'],
      5 => ['name' => 'SmoothBeatz3',    'comment' => 'Dude, Ive been looking for a vst like this for a l...', 'reply_id' => '/0/5'],
      6 => ['name' => 'FanBoy123',       'comment' => 'Hey Drumma, when are you going to release a new hi...', 'reply_id' => '/0/2/6'],
      7 => ['name' => 'Mike Johnson',    'comment' => 'Hey Fanboy123, why are you such a fanboy of Drum S...', 'reply_id' => '/0/2/6/7'],
      ];