Php mvc体系结构中的嵌套注释

Php mvc体系结构中的嵌套注释,php,model-view-controller,comments,Php,Model View Controller,Comments,我正在创建一个没有框架的MVC架构的网站。在博客中也有类似的评论。我希望能够回答一个评论。但是回答功能不起作用(当我想回答一条评论时,它与第一条评论相同),我很难找到原因?你能帮我吗?以下是网站地址:cedricjager.com/stream 下面是connection.php: class Connection { // Connection private function getBdd() { try { $bdd = ConfigDB::database(); $pdo =

我正在创建一个没有框架的MVC架构的网站。在博客中也有类似的评论。我希望能够回答一个评论。但是回答功能不起作用(当我想回答一条评论时,它与第一条评论相同),我很难找到原因?你能帮我吗?以下是网站地址:cedricjager.com/stream

下面是connection.php:

class Connection {

// Connection
private function getBdd() {
try {
  $bdd = ConfigDB::database();
  $pdo = new PDO("mysql:host={$bdd['host']}; dbname={$bdd['db_name']}", "{$bdd['username']}", "{$bdd['password']}");
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  print "Error!: " . $e->getMessage() . "<br/>";
  die();
}
return $pdo;
}

// Query
public function query($sql, $params = array(), $fetch = null) {
try {
  $req = self::getBdd()->prepare($sql);
  $req->execute($params);

  if ($fetch == 'one') {
    return $req->fetch();
  } else if ($fetch == 'all') {
    return $req->fetchAll();
  } else {
    return $req;
  }

 } catch (PDOException $e) {
  print "Error!: " . $e->getMessage() . "<br/>";
  die();
 }
 }
观点



海报评论

发布者

Comments.php

<div id="comment-<?= $critic['id'] ?>">
<p>
<b><?= $critic['author'] ?></b>
<span class="text-muted">le <?= $critic['date'] ?></span>
</p>
<div class="blockquote">
<blockquote>
  <?= htmlentities($critic['content']) ?>
</blockquote>
</div>
<div class="formulaire">
<form class="form-group"  method="post">
  <p class="text-left">
    <input type="hidden" name="valeur" value="<?= $critic['id_movie'] ?>">
    <input type="hidden" name="idval" value="<?= $critic['id'] ?>">
    <?php if($critic['depth'] <= 1): ?>
      <button  type="button" class="reply btn btn-default" data-id="<?= $critic['id'] ?>"><i class="fas fa-comments"></i></button>
    <?php endif; ?>
    <button type="submit" name="signal" class="btn btn-default"><i class="fas fa-bolt"></i></span></button>
  </p>
  </form>
  </div>
  </div>

 <div id="answer">
<?php if(isset($critic['children'])): ?>
    <?php foreach($critic['children'] as $critic): ?>
        <?php require('comments.php'); ?>
    <?php endforeach; ?>
<?php endif; ?>
</div>


首先,我相信您从未设置“comment.php”视图中提到的
子索引:

<?php if(isset($critic['children'])): ?>
    <?php foreach($critic['children'] as $critic): ?>
        <?php require('comments.php'); ?>
    <?php endforeach; ?>
<?php endif; ?>
现在评论家应该有一个“儿童”索引,其中列出了所有儿童。它允许您无限制地构建注释树。 您需要记住的是,在发布新评论时正确设置父id

如果它解决了你的问题,请告诉我

      <div class="sectioncomments" id="comments">
    <?php foreach($critics as $critic): ?>
        <?php require('comments.php'); ?>
    <?php endforeach; ?>
  </div>
  <hr>
  <div class="row">
    <div class="col-lg-12">
      <div id="form-comment" class=" panel panel-default formComment">
        <div class="panel panel-heading">
          <h4>Poster une critique</h4>
          <br>
          <a href="#"><span class="return"></span></a>
        </div>
        <div class="panel panel-body">
          <form method="post"  class="form-group form-horizontal">
            <div class="form-group">
              <div class="col-sm-9">
                <input type="text" class="form-control" id="nom" placeholder="Votre nom..." name="nom">
              </div>
            </div>
            <div class="form-group">
              <div class="col-sm-9">
                <textarea class="form-control" id="content" placeholder="Votre critique..." name="content"></textarea>
              </div>
            </div>
            <p class="text-right"><button type="submit" class="btn btn-success">Publier</button></p>
            <input type="hidden" name="parent_id" id="parent_id" value="0" >
          </form>
        </div>
      </div>
    </div>

  </div>
</div>
<div id="comment-<?= $critic['id'] ?>">
<p>
<b><?= $critic['author'] ?></b>
<span class="text-muted">le <?= $critic['date'] ?></span>
</p>
<div class="blockquote">
<blockquote>
  <?= htmlentities($critic['content']) ?>
</blockquote>
</div>
<div class="formulaire">
<form class="form-group"  method="post">
  <p class="text-left">
    <input type="hidden" name="valeur" value="<?= $critic['id_movie'] ?>">
    <input type="hidden" name="idval" value="<?= $critic['id'] ?>">
    <?php if($critic['depth'] <= 1): ?>
      <button  type="button" class="reply btn btn-default" data-id="<?= $critic['id'] ?>"><i class="fas fa-comments"></i></button>
    <?php endif; ?>
    <button type="submit" name="signal" class="btn btn-default"><i class="fas fa-bolt"></i></span></button>
  </p>
  </form>
  </div>
  </div>

 <div id="answer">
<?php if(isset($critic['children'])): ?>
    <?php foreach($critic['children'] as $critic): ?>
        <?php require('comments.php'); ?>
    <?php endforeach; ?>
<?php endif; ?>
</div>
<?php if(isset($critic['children'])): ?>
    <?php foreach($critic['children'] as $critic): ?>
        <?php require('comments.php'); ?>
    <?php endforeach; ?>
<?php endif; ?>
$critics = $this->getAllById($id);//movie id
$childs = []; //Will contain all childs indexed by parent_id
$index = []; //will contain all root critics (no parent)
foreach($critics as &$v){
     if($v['parent_id'] === 0){ //if no parent, then add to $index
          $indexes[$v['id']] = $v;
     } else{ //else create an array in $childs at index parent_id
         if(!isset($childs[$v['parent_id']])) $childs[$v['parent_id']] = [];
         $childs[$v['parent_id']][] = $v;
     }
}

//Then you can build your result : 
foreach($childs as $id=>&$child){
    $parent= $index[$id] ?? $child[$id] ?? null; // search for the parent of the current child
    if(is_null($parent)) continue; // a parent doesn't exists anymore, we ignor it, but you can throw an exception instead
    if(!isset($parent['children'])) $parent['children'] = [];
    $parent['children'][] = $child;
}
return $index;