Note that there are some explanatory texts on larger screens.

plurals
  1. POComment prints more than once
    primarykey
    data
    text
    <p>I've made a CMS following the tutorial by phpacademy on Youtube, and now I'm extending it by adding comments.</p> <p>I got it to print the data from the database with the comments.</p> <p>However, it prints it more than once.</p> <p>Code:</p> <p><strong>Article.php</strong> (Where I print the articles)</p> <pre><code>&lt;?php include_once('includes/connection.php'); include_once('includes/article.php'); include_once('includes/comments.php'); $article = new Article; $comment = new Comment; if(isset($_GET['id'])){ $id = $_GET['id']; $data = $article-&gt;fetch_data($id); $comments = $comment-&gt;fetch_data($id); ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;CMS&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="assets/style.css"&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="container"&gt; &lt;a href="index.php" id="logo"&gt;CMS&lt;/a&gt; &lt;h4&gt;&lt;?php echo $data['article_title']; ?&gt; - &lt;small&gt;posted &lt;?php echo date('l jS', $data['article_timestamp']); ?&gt;&lt;/small&gt;&lt;/h4&gt; &lt;div id="content"&gt; &lt;p&gt;&lt;?php echo $data['article_content'] ?&gt;&lt;/p&gt; &lt;/div&gt; &lt;a href="index.php"&gt;&amp;larr; Back&lt;/a&gt;&lt;br&gt; &lt;div id="comments"&gt; &lt;?php foreach($comments as $comment){ ?&gt; &lt;div class="comment"&gt; &lt;span class="poster"&gt;Posted &lt;?php echo date('l jS', $comments['comment_timestamp'])." by ".$comments['poster_name']; ?&gt;&lt;/span&gt; &lt;?php echo $comments['comment_content']; ?&gt; &lt;/div&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/head&gt; &lt;?php }else{ header('Location: index.php'); exit(); } ?&gt; </code></pre> <p><strong>includes/Comments.php</strong>:</p> <pre><code>&lt;?php class Comment{ public function fetch_all(){ global $pdo; $query = $pdo-&gt;prepare("SELECT * FROM comments"); $query-&gt;execute(); return $query-&gt;fetchAll(); } public function fetch_data($post_id){ global $pdo; $query = $pdo-&gt;prepare("SELECT * FROM comments WHERE post_id = ?"); $query-&gt;bindValue(1, $post_id); $query-&gt;execute(); return $query-&gt;fetch(); } } ?&gt; </code></pre> <p>If you need more code, or me to explain my problem more in detail, just comment.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload