Partage
  • Partager sur Facebook
  • Partager sur Twitter

TP adoptez une architechture MVC en PHP modifier u

TP comment modifier un commentaire dans un blog

    15 août 2018 à 10:11:46

    Bonjour à tous!

    Je suis sur le TP adoptez une architecture MVC en PHP modifier un commentaire, j'ai trouvé des soucis. quand j'envoie le commentaire modifié j'obtiens une page blanche. Si c'est possible un aide de vos part et merci d'avance.

    Voilà mon code:

    frontend.php

    <?php
     
    // Chargement des classes
    require_once('model/PostManager.php');
    require_once('model/CommentManager.php');
     
    function listPosts()
    {
        $postManager = new \OpenClassrooms\Blog\Model\PostManager();
        $posts = $postManager->getPosts();
     
        require('view/frontend/listPostsView.php');
    }
     
    function post()
    {
        $postManager = new \OpenClassrooms\Blog\Model\PostManager();
        $commentManager = new \OpenClassrooms\Blog\Model\CommentManager();
         
        $post = $postManager->getPost($_GET['id']);
        $comments = $commentManager->getComments($_GET['id']);
         
        require('view/frontend/postView.php');
    }
     
    function addComment($postId, $author, $comment)
    {
        $commentManager = new \OpenClassrooms\Blog\Model\CommentManager();
     
        $affectedLines = $commentManager->postComment($postId, $author, $comment);
     
        if ($affectedLines === false) {
            throw new Exception('Impossible d\'ajouter le commentaire !');
        }
        else {
            header('Location: index.php?action=post&id=' . $postId);
        }
    }
    function edit($newComment, $commentID, $postID)
    {
        $commentManager = new \Valentin\Blog\Model\CommentManager();
      
        $affectedComment = $commentManager->editComment($newComment,$commentID);
      
        require('view/frontend/commentView.php');
      
        if ($affectedComment == false){
            throw new Exception('Impossible d\'editer le commentaire !');
        }
        else {
            header('Location : index.php?action=post&id=' .$postID);
        }
    }

    CommentManager.php

    <?php
     
    namespace OpenClassrooms\Blog\Model;
     
    require_once("model/Manager.php");
     
    class CommentManager extends Manager
    {
        public function getComments($postId)
        {
            $db = $this->dbConnect();
            $comments = $db->prepare('SELECT post_id, author, comment, DATE_FORMAT(comment_date, \'%d/%m/%Y à %Hh%imin%ss\') AS comment_date_fr FROM comments WHERE post_id = ? ORDER BY comment_date DESC');
            $comments->execute(array($postId));
     
            return $comments;
        }
     
        public function postComment($postId, $author, $comment)
        {
            $db = $this->dbConnect();
            $comments = $db->prepare('INSERT INTO comments(post_id, author, comment, comment_date) VALUES(?, ?, ?, NOW())');
            $affectedLines = $comments->execute(array($postId, $author, $comment));
     
            return $affectedLines;
        }
      
        //Edit comment
        public function editComment($newComment, $commentID)
        {
            $db = $this->dbConnect();
            $newComments = $db->prepare('UPDATE comments SET comment = ? WHERE id=?');
            $affectedComment = $newComments->execute(array($newComment, $commentID)); //ligne 31
      
            return $affectedComment;
        }
     }

    Manager.php

    <?php
     
    namespace OpenClassrooms\Blog\Model;
     
    class Manager
    {
        protected function dbConnect()
        {
            $db = new \PDO('mysql:host=localhost;dbname=test;charset=utf8', 'dbuser', '');
            return $db;
        }
    }

    PostManager.php

    <?php
     
    namespace OpenClassrooms\Blog\Model;
     
    require_once("model/Manager.php");
     
    class PostManager extends Manager
    {
        public function getPosts()
        {
            $db = $this->dbConnect();
            $req = $db->query('SELECT id, title, content, DATE_FORMAT(creation_date, \'%d/%m/%Y à %Hh%imin%ss\') AS creation_date_fr FROM posts ORDER BY creation_date DESC LIMIT 0, 5');
     
            return $req;
        }
     
        public function getPost($postId)
        {
            $db = $this->dbConnect();
            $req = $db->prepare('SELECT id, title, content, DATE_FORMAT(creation_date, \'%d/%m/%Y à %Hh%imin%ss\') AS creation_date_fr FROM posts WHERE id = ?');
            $req->execute(array($postId));
            $post = $req->fetch();
     
            return $post;
        }
    }

    listPostsView.php

    <?php $title = 'Mon blog'; ?>
    
    <?php ob_start(); ?>
    <h1>Mon super blog !</h1>
    <p>Derniers billets du blog :</p>
    
    
    <?php
    while ($data = $posts->fetch())
    {
    ?>
        <div class="news">
            <h3>
                <?= htmlspecialchars($data['title']) ?>
                <em>le <?= $data['creation_date_fr'] ?></em>
            </h3>
    
            <p>
                <?= nl2br(htmlspecialchars($data['content'])) ?>
                <br />
                <em><a href="index.php?action=post&id=<?= $data['id'] ?>">Commentaires</a></em>
            </p>
        </div>
    <?php
    }
    $posts->closeCursor();
    ?>
    <?php $content = ob_get_clean(); ?>
    
    <?php require('template.php'); ?>

    postView.php

    <?php $title = htmlspecialchars($post['title']); ?>
     
    <?php ob_start(); ?>
    <h1>Mon super blog !</h1>
    <p><a href="index.php">Retour à la liste des billets</a></p>
     
    <div class="news">
        <h3>
            <?= htmlspecialchars($post['title']) ?>
            <em>le <?= $post['creation_date_fr'] ?></em>
        </h3>
         
        <p>
            <?= nl2br(htmlspecialchars($post['content'])) ?>
        </p>
    </div>
     
    <h2>Commentaires</h2>
     
    <form action="index.php?action=addComment&id=<?= $post['id'] ?>" method="post">
        <div>
            <label for="author">Auteur</label><br />
            <input type="text" id="author" name="author" />
        </div>
        <div>
            <label for="comment">Commentaire</label><br />
            <textarea id="comment" name="comment"></textarea>
        </div>
        <div>
            <input type="submit" />
        </div>
    </form>
     
    <?php
    /*while ($comment = $comments->fetch())
    {
    ?>
        <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?>(<a href="modification_form.php" >Modifier</a></p>
        <p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
    <?php
    }
    ?> */
    while ($comment = $comments->fetch())
    {
        $comment['id'] = isset($_POST['id']) ? ($_POST['id']) : NULL;
    ?>
        <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?> <a href="modification_form.php?action=edit&id=<?= $comment['id']?>&postID=<?= $post['id'] ?>">(modifier)</a></p>
        <p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
    <?php
    }
    ?>
    <?php $content = ob_get_clean(); ?>
     
    <?php require('template.php'); ?>
    
    

    Template.php

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title><?= $title ?></title>
            <link href="public/css/style.css" rel="stylesheet" /> 
        </head>
             
        <body>
            <?= $content ?>
        </body>
    </html>

    index.php

    <?php
    require('controller/frontend.php');
    
    try {
        if (isset($_GET['action'])) {
            if ($_GET['action'] == 'listPosts') {
                listPosts();
            }
            elseif ($_GET['action'] == 'post') {
                if (isset($_GET['id']) && $_GET['id'] > 0) {
                    post();
                }
                else {
                    throw new Exception('Aucun identifiant de billet envoyé');
                }
            } 
            elseif ($_GET['action'] == 'addComment') {
                if (isset($_GET['id']) && $_GET['id'] > 0) {
                    if (!empty($_POST['author']) && !empty($_POST['comment'])) { 
                        addComment($_GET['id'], $_POST['author'], $_POST['comment']); 
                    }
                    else {
                        throw new Exception('Tous les champs ne sont pas remplis !');
                    }
                }
                else {
                    throw new Exception('Aucun identifiant de billet envoyé');
                }
            }
            elseif($_GET['action'] == 'edit'){
            if (isset($_GET['id']) && $_GET['id'] > 0 && isset($_GET['postID']) && $_GET['postID'] > 0){
              //if (isset($_POST['newComment'])) {
                edit($_POST['newComment'], $_GET['id'], $_GET['postID']); //ligne 37
              //}
              //else {
              //  throw new Exception ('Bug');
              //}
            }
          }
        }
        else {
            listPosts();
        }
    }
    catch(Exception $e) {
        echo 'Erreur : ' . $e->getMessage();
    }

    modification_form.php

    <?php ob_start(); ?>
    <h1>Mon super blog !</h1>
    <p><a href="index.php">Retour à la liste des billets</a></p>
      
      
    <h2>Editer le commentaire</h2>
     <?php $commentId = isset($_POST['commentId']) ? ($_POST['commentId']) : NULL;
     $comment = isset($_POST['comment']) ? ($_POST['comment']) : NULL;
      ?>
    <form action="index.php?action=edit&id=<?= $commentId ?>" method="post">
            <p><label for="author">Auteur</label>
            <input type="text" name="author" id="author" value="<?=htmlspecialchars($comment['author']);?>">
            <label for="newComment">Nouveau commentaire</label><br />
            <textarea id="newComment" name="newComment" value = "<?=htmlspecialchars($comment['comment']);?>"></textarea>
        </div>
        <div>
            <input type="Submit" />
        </div>
    </form>
    <?php 
    $content = ob_get_clean(); ?>
      
    <?php require('view/frontend/template.php'); ?>

    MERCI d'avance.









    -
    Edité par MohamedSamki 15 août 2018 à 10:30:09

    • Partager sur Facebook
    • Partager sur Twitter
      15 août 2018 à 10:32:03

      Bonjour Mohamed,

      Pourquoi la classe Valentin dans la function edit ?

      En ce qui me concerne je ferai comme ceci pour le frontend :

      function edit($id, $comment, $postId )
      {
          $commentManager = new \OpenClassrooms\Blog\Model\CommentManager();
       	
          $affectedComment = $commentManager->editComment($id, $comment, $postId);
       
          if ($affectedComment === false) {
       
              throw new Exception('Impossible de modifier le commentaire !');
          }
          else {
              echo 'commentaire : ' . $id;
              header("Location: index.php?action=post&id=" .$postId);
          }
      }

      Puis dans l'index :

      elseif ($_GET['action'] == 'edit') {
               if (isset($_GET['id']) && $_GET['id'] > 0 ) {
                      
                   if (!empty($_POST['newComment'])) {
                           edit($_GET['id'], $_POST['newComment'], $_GET['postID']);   
                          }
                  else {
                          throw new Exception('Tous les champs ne sont pas remplis !!!!!');
                              }
                  }
                  else {
                      throw new Exception('Aucun identifiant de billet envoyé');
                  }
              }
                  
      




      -
      Edité par Max Imus 15 août 2018 à 10:58:17

      • Partager sur Facebook
      • Partager sur Twitter
        15 août 2018 à 13:30:52

        après cette modif que tu m'as proposée sur frontend et index, quand j'envoie lo commentaire modifié je reçois le MSG: Erreur : Aucun identifiant de billet envoyé.
        • Partager sur Facebook
        • Partager sur Twitter
          15 août 2018 à 13:44:41

          Et en mettant dans modification_form.php comme ceci, est-ce que l'identifiant est transmit ?

          <form action="index.php?action=edit&id=<?= $_GET['id'] ?>" method="post">



          • Partager sur Facebook
          • Partager sur Twitter
            15 août 2018 à 13:52:49

            Lorsque vous êtes sur la page de modification_form.php, qu'avez-vous dans l'URL ?
            • Partager sur Facebook
            • Partager sur Twitter
              15 août 2018 à 13:54:13

              J'ai ça : modification_form.php?action=edit&id=&postID=2
              • Partager sur Facebook
              • Partager sur Twitter
                15 août 2018 à 14:00:26

                L'id n'est pas transmit depuis la page postView.php, si vous faites comme ceci sur cette page en mettant la ligne 45 en commentaire :

                <?php $title = htmlspecialchars($post['title']); ?>
                  
                <?php ob_start(); ?>
                <h1>Mon super blog !</h1>
                <p><a href="index.php">Retour à la liste des billets</a></p>
                  
                <div class="news">
                    <h3>
                        <?= htmlspecialchars($post['title']) ?>
                        <em>le <?= $post['creation_date_fr'] ?></em>
                    </h3>
                      
                    <p>
                        <?= nl2br(htmlspecialchars($post['content'])) ?>
                    </p>
                </div>
                  
                <h2>Commentaires</h2>
                  
                <form action="index.php?action=addComment&id=<?= $post['id'] ?>" method="post">
                    <div>
                        <label for="author">Auteur</label><br />
                        <input type="text" id="author" name="author" />
                    </div>
                    <div>
                        <label for="comment">Commentaire</label><br />
                        <textarea id="comment" name="comment"></textarea>
                    </div>
                    <div>
                        <input type="submit" />
                    </div>
                </form>
                  
                <?php
                /*while ($comment = $comments->fetch())
                {
                ?>
                    <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?>(<a href="modification_form.php" >Modifier</a></p>
                    <p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
                <?php
                }
                ?> */
                while ($comment = $comments->fetch())
                {
                    //$comment['id'] = isset($_POST['id']) ? ($_POST['id']) : NULL;
                ?>
                    <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?> <a href="modification_form.php?action=edit&id=<?= $comment['id']?>&postID=<?= $post['id'] ?>">(modifier)</a></p>
                    <p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
                <?php
                }
                ?>
                <?php $content = ob_get_clean(); ?>
                  
                <?php require('template.php'); ?>

                Puis recommencez comme si vous vouliez modifié un commentaire depuis la page postView.php et dîtes moi l'URL de la page modification_form.php SVP.

                -
                Edité par Max Imus 15 août 2018 à 14:09:14

                • Partager sur Facebook
                • Partager sur Twitter
                  15 août 2018 à 14:07:12

                    (index.php?action=post&id=1) (= à l'adresse d'en haut)

                   je reçois ça sur index.php (adresse d'en haut)

                  ( ! ) Notice: Undefined index: id in C:\wamp\www\Nouveau TP MVC PHP\view\frontend\postView.php on line 47

                  • Partager sur Facebook
                  • Partager sur Twitter
                    15 août 2018 à 14:12:51

                    Puis recommencez comme si vous vouliez modifié un commentaire depuis la page postView.php et dîtes moi l'URL de la page modification_form.php SVP.
                    • Partager sur Facebook
                    • Partager sur Twitter
                      15 août 2018 à 14:20:11

                      j'ai ça:

                      modification_form.php?action=edit&id=%3Cbr%20/%3E%3Cfont%20size='1'%3E%3Ctable%20class='xdebug-error%20xe-notice'%20dir='ltr'%20border='1'%20cellspacing='0'%20cellpadding='1'%3E%3Ctr%3E%3Cth%20align='left'%20bgcolor='#f57900' colspan=

                      • Partager sur Facebook
                      • Partager sur Twitter
                        15 août 2018 à 14:25:24

                        Si vous mettez comme ceci dans la page postView pour le lien modifier :

                        <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?> <a href="modification_form.php?action=edit&amp;id=<?= $comment['id']?>&amp;postID=<?= $post['id'] ?>&amp;comment=<?= $comment['comment']?>">(modifier)</a></p>



                        -
                        Edité par Max Imus 15 août 2018 à 14:27:36

                        • Partager sur Facebook
                        • Partager sur Twitter
                          15 août 2018 à 14:31:09

                          j'ai cette URL:

                          modification_form.php?action=edit&id=%3Cbr%20/%3E%3Cfont%20size='1'%3E%3Ctable%20class='xdebug-error%20xe-notice'%20dir='ltr'%20border='1'%20cellspacing='0'%20cellpadding='1'%3E%3Ctr%3E%3Cth%20align='left'%20bgcolor='#f57900' colspan=

                          • Partager sur Facebook
                          • Partager sur Twitter
                            15 août 2018 à 14:36:12

                            Faites un var_dump dans postView.php comme ceci :

                            <?php $title = htmlspecialchars($post['title']); ?>
                               
                            <?php ob_start(); ?>
                            <h1>Mon super blog !</h1>
                            <p><a href="index.php">Retour à la liste des billets</a></p>
                               
                            <div class="news">
                                <h3>
                                    <?= htmlspecialchars($post['title']) ?>
                                    <em>le <?= $post['creation_date_fr'] ?></em>
                                </h3>
                                   
                                <p>
                                    <?= nl2br(htmlspecialchars($post['content'])) ?>
                                </p>
                            </div>
                               
                            <h2>Commentaires</h2>
                               
                            <form action="index.php?action=addComment&id=<?= $post['id'] ?>" method="post">
                                <div>
                                    <label for="author">Auteur</label><br />
                                    <input type="text" id="author" name="author" />
                                </div>
                                <div>
                                    <label for="comment">Commentaire</label><br />
                                    <textarea id="comment" name="comment"></textarea>
                                </div>
                                <div>
                                    <input type="submit" />
                                </div>
                            </form>
                               
                            <?php
                            /*while ($comment = $comments->fetch())
                            {
                            ?>
                                <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?>(<a href="modification_form.php" >Modifier</a></p>
                                <p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
                            <?php
                            }
                            ?> */
                            while ($comment = $comments->fetch())
                            {var_dump($comment);

                                //$comment['id'] = isset($_POST['id']) ? ($_POST['id']) : NULL; ?>     <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?> <a href="modification_form.php?action=edit&id=<?= $comment['id']?>&postID=<?= $post['id'] ?>">(modifier)</a></p>     <p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p> <?php } ?> <?php $content = ob_get_clean(); ?>     <?php require('template.php'); ?>

                            Et dîtes moi ce que révèle le var_dump mais ne mettez pas le <br >.

                            -
                            Edité par Max Imus 15 août 2018 à 14:42:40

                            • Partager sur Facebook
                            • Partager sur Twitter
                              15 août 2018 à 14:49:02

                              sur   index.php?action=post&id=1     j'ai

                              Mon super blog !

                              Retour à la liste des billets

                              Le PHP à la conquête du m le 01/08/2018 à 09h40min41s

                              C'est officiel, l'éléPHPant a annoncé à la radio hier soir "J'ai l'intention de conquérir le monde !".
                              Il a en outre précisé que le monde serait à sa botte en moins de temps qu'il n'en fallait pour dire "éléPHPant". Pas dur, ceci dit entre nous...

                              Commentaires

                              <form style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;" action="index.php?action=addComment&amp;id=1" method="post">
                              <label for="author">Auteur</label>
                              <input id="author" type="text" name="author" />
                              <label for="comment">Commentaire</label>
                              <textarea id="comment" name="comment" />
                              <input type="submit" value="Soumettre la requête" />
                              </form>
                              array (size=8)
                                'post_id' => 
                              <small>string</small>
                               '1' (length=1)
                                0 => 
                              <small>string</small>
                               '1' (length=1)
                                'author' => 
                              <small>string</small>
                               'SEGUIN' (length=6)
                                1 => 
                              <small>string</small>
                               'SEGUIN' (length=6)
                                'comment' => 
                              <small>string</small>
                               'jjjjjjjjjjj' (length=11)
                                2 => 
                              <small>string</small>
                               'jjjjjjjjjjj' (length=11)
                                'comment_date_fr' => 
                              <small>string</small>
                               '08/08/2018 à 20h01min55s' (length=25)
                                3 => 
                              <small>string</small>
                               '08/08/2018 à 20h01min55s' (length=25)
                              

                              SEGUIN le 08/08/2018 à 20h01min55s (modifier)

                              array (size=8)
                                'post_id' => 
                              <small>string</small>
                               '1' (length=1)
                                0 => 
                              <small>string</small>
                               '1' (length=1)
                                'author' => 
                              <small>string</small>
                               'REMI' (length=4)
                                1 => 
                              <small>string</small>
                               'REMI' (length=4)
                                'comment' => 
                              <small>string</small>
                               'iiiiiiiiiii' (length=11)
                                2 => 
                              <small>string</small>
                               'iiiiiiiiiii' (length=11)
                                'comment_date_fr' => 
                              <small>string</small>
                               '08/08/2018 à 20h01min35s' (length=25)
                                3 => 
                              <small>string</small>
                               '08/08/2018 à 20h01min35s' (length=25)
                              

                              REMI le 08/08/2018 à 20h01min35s (modifier)

                              array (size=8)
                                'post_id' => 
                              <small>string</small>
                               '1' (length=1)
                                0 => 
                              <small>string</small>
                               '1' (length=1)
                                'author' => 
                              <small>string</small>
                               'RENE' (length=4)
                                1 => 
                              <small>string</small>
                               'RENE' (length=4)
                                'comment' => 
                              <small>string</small>
                               'hhhhhhhhh' (length=9)
                                2 => 
                              <small>string</small>
                               'hhhhhhhhh' (length=9)
                                'comment_date_fr' => 
                              <small>string</small>
                               '08/08/2018 à 20h01min20s' (length=25)
                                3 => 
                              <small>string</small>
                               '08/08/2018 à 20h01min20s' (length=25)
                              

                              RENE le 08/08/2018 à 20h01min20s (modifier)

                              array (size=8)
                                'post_id' => 
                              <small>string</small>
                               '1' (length=1)
                                0 => 
                              <small>string</small>
                               '1' (length=1)
                                'author' => 
                              <small>string</small>
                               'JOLI' (length=4)
                                1 => 
                              <small>string</small>
                               'JOLI' (length=4)
                                'comment' => 
                              <small>string</small>
                               'ggggggggg' (length=9)
                                2 => 
                              <small>string</small>
                               'ggggggggg' (length=9)
                                'comment_date_fr' => 
                              <small>string</small>
                               '08/08/2018 à 20h01min00s' (length=25)
                                3 => 
                              <small>string</small>
                               '08/08/2018 à 20h01min00s' (length=25)
                              

                              JOLI le 08/08/2018 à 20h01min00s (modifier)

                              array (size=8)
                                'post_id' => 
                              <small>string</small>
                               '1' (length=1)
                                0 => 
                              <small>string</small>
                               '1' (length=1)
                                'author' => 
                              <small>string</small>
                               'MIKI' (length=4)
                                1 => 
                              <small>string</small>
                               'MIKI' (length=4)
                                'comment' => 
                              <small>string</small>
                               'ffffffffff' (length=10)
                                2 => 
                              <small>string</small>
                               'ffffffffff' (length=10)
                                'comment_date_fr' => 
                              <small>string</small>
                               '08/08/2018 à 20h00min37s' (length=25)
                                3 => 
                              <small>string</small>
                               '08/08/2018 à 20h00min37s' (length=25)
                              

                              Si je clic modifier pour l'autteur SEGUIN qui a comme commentaire jjjjjjjjjjjj j'obtiens l'URL:  modification_form.php?action=edit&id=&postID=1&comment=jjjjjjjjjjjj

                              • Partager sur Facebook
                              • Partager sur Twitter
                                15 août 2018 à 14:55:12

                                Je ne vois pas l'id donc dans CommentManager.php essayez comme ceci sur la function getComments :

                                public function getComments($postId)
                                    {
                                        $db = $this->dbConnect();
                                        $comments = $db->prepare('SELECT id, author, comment, DATE_FORMAT(comment_date, \'%d/%m/%Y à %Hh%imin%ss\') AS comment_date_fr FROM comments WHERE post_id = ? ORDER BY comment_date DESC');
                                        $comments->execute(array($postId));
                                  
                                        return $comments;
                                    }

                                Et dîtes moi pour le var_dump mais avant remettez le lien modifier comme demander plus haut.

                                -
                                Edité par Max Imus 15 août 2018 à 15:01:15

                                • Partager sur Facebook
                                • Partager sur Twitter
                                  15 août 2018 à 15:03:41

                                  clic sur commentaire d'un billet ça affiche le billet et le formulaire du commentaire sans afficher les commentaires du billet en bas du formulaire
                                  • Partager sur Facebook
                                  • Partager sur Twitter
                                    15 août 2018 à 15:06:27

                                    Faîtes-moi voir la page postView.php avec les modifs SVP ?
                                    • Partager sur Facebook
                                    • Partager sur Twitter
                                      15 août 2018 à 15:08:39

                                      postView

                                      <?php $title = htmlspecialchars($post['title']); ?>
                                      
                                      <?php ob_start(); ?>
                                      <h1>Mon super blog !</h1>
                                      <p><a href="index.php">Retour à la liste des billets</a></p>
                                      
                                      <div class="news">
                                          <h3>
                                              <?= htmlspecialchars($post['title']) ?>
                                              <em>le <?= $post['creation_date_fr'] ?></em>
                                          </h3>
                                          
                                          <p>
                                              <?= nl2br(htmlspecialchars($post['content'])) ?>
                                          </p>
                                      </div>
                                      
                                      <h2>Commentaires</h2>
                                      
                                      <form action="index.php?action=addComment&amp;id=<?= $post['id'] ?>" method="post">
                                          <div>
                                              <label for="author">Auteur</label><br />
                                              <input type="text" id="author" name="author" />
                                          </div>
                                          <div>
                                              <label for="comment">Commentaire</label><br />
                                              <textarea id="comment" name="comment"></textarea>
                                          </div>
                                          <div>
                                              <input type="submit" />
                                          </div>
                                      </form>
                                      
                                      <?php
                                      /*while ($comment = $comments->fetch())
                                      {
                                      ?>
                                          <p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?>(<a href="modification_form.php" >Modifier</a>)</p>
                                          <p><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
                                      <?php
                                      }
                                      ?> */
                                      while ($comment = $comments->fetch())
                                      {  var_dump($comment);
                                         // $comment['id'] = isset($_POST['id']) ? ($_POST['id']) : NULL;
                                      ?>  
                                          <p><p><strong><?= htmlspecialchars($comment['author']) ?></strong> le <?= $comment['comment_date_fr'] ?> <a href="modification_form.php?action=edit&amp;id=<?= $comment['id']?>&amp;postID=<?= $post['id'] ?>&amp;comment=<?= $comment['comment']?>">(modifier)</a></p>  
                                      <?php
                                      }
                                      ?>
                                      <?php $content = ob_get_clean(); ?>
                                      
                                      <?php require('template.php'); ?>
                                      



                                      • Partager sur Facebook
                                      • Partager sur Twitter
                                        15 août 2018 à 15:15:26

                                        Et si vous remettez comme ceci pour CommentManager, les commentaires s'affichent ?

                                        public function getComments($postId)
                                            {
                                                $db = $this->dbConnect();
                                                $comments = $db->prepare('SELECT post_id, author, comment, DATE_FORMAT(comment_date, \'%d/%m/%Y à %Hh%imin%ss\') AS comment_date_fr FROM comments WHERE post_id = ? ORDER BY comment_date DESC');
                                                $comments->execute(array($postId));
                                          
                                                return $comments;
                                            }



                                        -
                                        Edité par Max Imus 15 août 2018 à 15:17:41

                                        • Partager sur Facebook
                                        • Partager sur Twitter
                                          15 août 2018 à 15:20:39

                                          s'affiche comme je t'ai fait voir avant comme ça par exemple:

                                          Commentaires

                                          <form style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;" action="index.php?action=addComment&amp;id=2" method="post">
                                          <label for="author">Auteur</label>
                                          <input id="author" type="text" name="author" />
                                          <label for="comment">Commentaire</label>
                                          <textarea id="comment" name="comment" />
                                          <input type="submit" value="Soumettre la requête" />
                                          </form>
                                          array (size=8)
                                            'post_id' => 
                                          <small>string</small>
                                           '2' (length=1)
                                            0 => 
                                          <small>string</small>
                                           '2' (length=1)
                                            'author' => 
                                          <small>string</small>
                                           'KOKO' (length=4)
                                            1 => 
                                          <small>string</small>
                                           'KOKO' (length=4)
                                            'comment' => 
                                          <small>string</small>
                                           'LLLLLLLLLL' (length=10)
                                            2 => 
                                          <small>string</small>
                                           'LLLLLLLLLL' (length=10)
                                            'comment_date_fr' => 
                                          <small>string</small>
                                           '15/08/2018 à 15h00min42s' (length=25)
                                            3 => 
                                          <small>string</small>
                                           '15/08/2018 à 15h00min42s' (length=25)
                                          

                                          KOKO le 15/08/2018 à 15h00min42s ( ! ) Notice: Undefined index: id in C:\wamp\www\Nouveau TP MVC PHP\view\frontend\postView.php on line 47 Call Stack #TimeMemoryFunctionLocation 10.0031253848{main}( )..\index.php:0 20.0149286888post( )..\index.php:11 30.0385311856require( 'C:\wamp\www\Nouveau TP MVC PHP\view\frontend\postView.php' )..\frontend.php:23 &postID=2&comment=LLLLLLLLLL">(modifier)

                                          • Partager sur Facebook
                                          • Partager sur Twitter
                                            15 août 2018 à 15:23:05

                                            Pourriez-vous me montrer la structure de la table comments SVP ?
                                            • Partager sur Facebook
                                            • Partager sur Twitter
                                              15 août 2018 à 15:29:09

                                              -- phpMyAdmin SQL Dump
                                              -- version 4.1.14
                                              -- http://www.phpmyadmin.net
                                              --
                                              -- Client :  127.0.0.1
                                              -- Généré le :  Mer 15 Août 2018 à 15:27
                                              -- Version du serveur :  5.6.17
                                              -- Version de PHP :  5.5.12
                                              SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
                                              SET time_zone = "+00:00";

                                              /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
                                              /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
                                              /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
                                              /*!40101 SET NAMES utf8 */;
                                              --
                                              -- Base de données :  `test`
                                              --
                                              -- --------------------------------------------------------
                                              --
                                              -- Structure de la table `comments`
                                              --
                                              CREATE TABLE IF NOT EXISTS `comments` (
                                                `post_id` int(11) NOT NULL,
                                                `author` varchar(25) NOT NULL,
                                                `comment` text NOT NULL,
                                                `comment_date` datetime NOT NULL
                                              ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
                                              --
                                              -- Contenu de la table `comments`
                                              --
                                              INSERT INTO `comments` (`post_id`, `author`, `comment`, `comment_date`) VALUES
                                              (2, 'SAMKI', 'aaaaaaaa', '2018-08-08 19:59:18'),
                                              (2, 'MOMO', 'bbbbbbbb', '2018-08-08 19:59:27'),
                                              (2, 'PIERRE', 'ccccccccc', '2018-08-08 19:59:38'),
                                              (2, 'JULIEN', 'dddddddddd', '2018-08-08 19:59:56'),
                                              (2, 'LOIC', 'eeeeeeeeeee', '2018-08-08 20:00:07'),
                                              (1, 'MIKI', 'ffffffffff', '2018-08-08 20:00:37'),
                                              (1, 'JOLI', 'ggggggggg', '2018-08-08 20:01:00'),
                                              (1, 'RENE', 'hhhhhhhhh', '2018-08-08 20:01:20'),
                                              (1, 'REMI', 'iiiiiiiiiii', '2018-08-08 20:01:35'),
                                              (1, 'SEGUIN', 'jjjjjjjjjjj', '2018-08-08 20:01:55'),
                                              (2, 'ADRIEN', 'kkkkkkkkkk', '2018-08-14 16:53:23'),
                                              (2, 'KOKO', 'LLLLLLLLLL', '2018-08-15 15:00:42');
                                              /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
                                              /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
                                              /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
                                              • Partager sur Facebook
                                              • Partager sur Twitter
                                                15 août 2018 à 15:38:52

                                                Je ne vois pas l'id dans votre table et aucun champ n'est en auto_incrément, rajouter une colonne ID (Voir la structure de ma table) :

                                                • Partager sur Facebook
                                                • Partager sur Twitter
                                                  15 août 2018 à 15:45:14

                                                  je l'ai ajouté et j'ai essayé 

                                                  mais rien n'est changé

                                                  • Partager sur Facebook
                                                  • Partager sur Twitter
                                                    15 août 2018 à 15:54:14

                                                    Faîtes-moi voir votre table SVP ?

                                                    Que donne le var_dump ?

                                                    -
                                                    Edité par Max Imus 15 août 2018 à 15:54:58

                                                    • Partager sur Facebook
                                                    • Partager sur Twitter
                                                      15 août 2018 à 15:57:10

                                                      table comments

                                                      -- phpMyAdmin SQL Dump
                                                      -- version 4.1.14
                                                      -- http://www.phpmyadmin.net
                                                      --
                                                      -- Client :  127.0.0.1
                                                      -- Généré le :  Mer 15 Août 2018 à 15:55
                                                      -- Version du serveur :  5.6.17
                                                      -- Version de PHP :  5.5.12

                                                      SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
                                                      SET time_zone = "+00:00";


                                                      /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
                                                      /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
                                                      /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
                                                      /*!40101 SET NAMES utf8 */;

                                                      --
                                                      -- Base de données :  `test`
                                                      --

                                                      -- --------------------------------------------------------

                                                      --
                                                      -- Structure de la table `comments`
                                                      --

                                                      CREATE TABLE IF NOT EXISTS `comments` (
                                                        `id` int(11) NOT NULL AUTO_INCREMENT,
                                                        `post_id` int(11) NOT NULL,
                                                        `author` varchar(25) NOT NULL,
                                                        `comment` text NOT NULL,
                                                        `comment_date` datetime NOT NULL,
                                                        PRIMARY KEY (`id`)
                                                      ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

                                                      --
                                                      -- Contenu de la table `comments`
                                                      --

                                                      INSERT INTO `comments` (`id`, `post_id`, `author`, `comment`, `comment_date`) VALUES
                                                      (1, 2, 'SAMKI', 'aaaaaaaa', '2018-08-08 19:59:18'),
                                                      (2, 2, 'MOMO', 'bbbbbbbb', '2018-08-08 19:59:27'),
                                                      (3, 2, 'PIERRE', 'ccccccccc', '2018-08-08 19:59:38'),
                                                      (4, 2, 'JULIEN', 'dddddddddd', '2018-08-08 19:59:56'),
                                                      (5, 2, 'LOIC', 'eeeeeeeeeee', '2018-08-08 20:00:07'),
                                                      (6, 1, 'MIKI', 'ffffffffff', '2018-08-08 20:00:37'),
                                                      (7, 1, 'JOLI', 'ggggggggg', '2018-08-08 20:01:00'),
                                                      (8, 1, 'RENE', 'hhhhhhhhh', '2018-08-08 20:01:20'),
                                                      (9, 1, 'REMI', 'iiiiiiiiiii', '2018-08-08 20:01:35'),
                                                      (10, 1, 'SEGUIN', 'jjjjjjjjjjj', '2018-08-08 20:01:55'),
                                                      (11, 2, 'ADRIEN', 'kkkkkkkkkk', '2018-08-14 16:53:23'),
                                                      (12, 2, 'KOKO', 'LLLLLLLLLL', '2018-08-15 15:00:42');

                                                      /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
                                                      /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
                                                      /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

                                                      • Partager sur Facebook
                                                      • Partager sur Twitter
                                                        15 août 2018 à 16:01:46

                                                        Que donne le var_dump ?
                                                        • Partager sur Facebook
                                                        • Partager sur Twitter
                                                          15 août 2018 à 16:05:33

                                                          j'ai un exemple de index.php

                                                          Commentaires

                                                          <form style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;" action="index.php?action=addComment&amp;id=2" method="post">
                                                          <label for="author">Auteur</label>
                                                          <input id="author" type="text" name="author" />
                                                          <label for="comment">Commentaire</label>
                                                          <textarea id="comment" name="comment" />
                                                          <input type="submit" value="Soumettre la requête" />
                                                          </form>
                                                          array (size=8)
                                                            'post_id' => 
                                                          <small>string</small>
                                                           '2' (length=1)
                                                            0 => 
                                                          <small>string</small>
                                                           '2' (length=1)
                                                            'author' => 
                                                          <small>string</small>
                                                           'KOKO' (length=4)
                                                            1 => 
                                                          <small>string</small>
                                                           'KOKO' (length=4)
                                                            'comment' => 
                                                          <small>string</small>
                                                           'LLLLLLLLLL' (length=10)
                                                            2 => 
                                                          <small>string</small>
                                                           'LLLLLLLLLL' (length=10)
                                                            'comment_date_fr' => 
                                                          <small>string</small>
                                                           '15/08/2018 à 15h00min42s' (length=25)
                                                            3 => 
                                                          <small>string</small>
                                                           '15/08/2018 à 15h00min42s' (length=25)
                                                          

                                                          KOKO le 15/08/2018 à 15h00min42s ( ! ) Notice: Undefined index: id in C:\wamp\www\Nouveau TP MVC PHP\view\frontend\postView.php on line 47 Call Stack #TimeMemoryFunctionLocation 10.0014253824{main}( )..\index.php:0 20.0059286856post( )..\index.php:11 30.0136311832require( 'C:\wamp\www\Nouveau TP MVC PHP\view\frontend\postView.php' )..\frontend.php:23 &postID=2&comment=LLLLLLLLLL">(modifier)

                                                          • Partager sur Facebook
                                                          • Partager sur Twitter
                                                            15 août 2018 à 16:08:40

                                                            JohanVallon a écrit:

                                                            Je ne vois pas l'id donc dans CommentManager.php essayez comme ceci sur la function getComments :

                                                            public function getComments($postId)
                                                                {
                                                                    $db = $this->dbConnect();
                                                                    $comments = $db->prepare('SELECT id, author, comment, DATE_FORMAT(comment_date, \'%d/%m/%Y à %Hh%imin%ss\') AS comment_date_fr FROM comments WHERE post_id = ? ORDER BY comment_date DESC');
                                                                    $comments->execute(array($postId));
                                                              
                                                                    return $comments;
                                                                }



                                                            -
                                                            Edité par JohanVallon il y a environ 1 heure



                                                            -
                                                            Edité par Max Imus 15 août 2018 à 16:23:55

                                                            • Partager sur Facebook
                                                            • Partager sur Twitter

                                                            TP adoptez une architechture MVC en PHP modifier u

                                                            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                                                            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                                                            • Editeur
                                                            • Markdown