How to display comments stored from an array particularly by that email link clicked on


#21

What?

it seems to me that you wish to query a table for a matching email address, then loop through the rows to display the data. yes?

i think that you need to use fetch() here but you can execute the query with a variable.

$sql = "SELECT * FROM comments WHERE email = :email";
$result = $pdo->query($sql);
$result->execute(array(':email' => $email));

while($row = $result->fetch(PDO::FETCH_ASSOC)) {

       echo "<div class=\"comments\">" .
            "<div class=\"comment\">" . "<div class=\"ID\">" . "Post ID: " . $rows['ID'] . "</div>" .
            "<div class=\"date\">" . "Posted on: " . $rows['date'] . "</div>" . "<h3>New comment by: " . $rows['email'] . "</h3>" .
            "<div class=\"mood\">" . "Current mood: " . $rows['mood'] . "</div>" .
            "<div class=\"comment-text\">" . "<p>" . $rows['commentText'] . "</p></div></div></div>";
}

did you try a fetch() for the row?
i’m logging off for the night…


#22

Yes, but the problem is it is the link form. I don’t how this will work though the GET method and how it will work when clicking on the the link. :fearful:

This is the whole function that I am having for now:

// Output unique email addresses to HTML
function the_commenters() {
global $commenters;

  //TODO
  echo '<h2>People Who have Commented: </h2>';
  while($row = $commenters->fetch()){ 
    echo '<div class="commenters">'.'<ul>'.'<li>'.'<a href="index.php">'.$row['email'].'</a>'
    .'</li>'.'</ul>'.
    '</div>';
  }
    
  if($_SERVER["REQUEST_METHOD"] == "GET"){
    if(isset($_GET['email'])){
    
      $sql = "SELECT email FROM comments WHERE email = :email";
      //$sql = 'SELECT ' .$id. 'FROM comments;';
      $result = $pdo->query($sql);
       while($row = $result->fetch(PDO::FETCH_ASSOC)){ 
         //echo $row['ID'];
         echo  '<div class = "comments">'
        .'<div class="comment">'. '<div class="ID">'.'Post ID: '.$row['ID'].'</div>'.
        '<div class="date">'.'Posted on: .'.$row['date'].'</div>'. '<h3>New comment by: '.$row['email'].'</h3>'.
        '<div class="mood">'.'Current mood: '.$row['mood'].'</div>'.
        '<div class="comment-text">'. '<p>' .$row['commentText'].'</p>'.  '</div>'.
        '</div>'.
        '</div>';
      }
    }
  }

    //$pdo = null;
    echo "<a href='index.php'>"."Show All Posts"."</a>";

  }

#23

Yes, I fetched the rows to display whole of the comments present in the database


#24

@johnphpnewb This is how the page looks like.
I want only to display the comment by the email id I click on using GET method. Please have a look. I hope this helps.

03%20PM 09%20PM


#25

I really just wanted to show you how to use a variable in your query.
I have no time to build your page for you and i don’t really understand what you are doing.

logic: $query = "SELECT column FROM table WHERE column = :placeHolderForVariable";
thus: $sql = "SELECT commentText FROM comments WHERE email = :getEmail";

then you need to execute the query $sql with the variable:

$result = $pdo->query($sql);
$result->execute(array(':email' => $email));

But you appear to be fetching more than the comment ($row[‘commentText’]):
$row[‘ID’], $row[‘date’], $row[‘mood’], $row[‘commentText’]

if you want all data in the row, then the WHILE loop can fetch all of the data not just the commentText.
Alternatively, you could use pdo fetchAll() to fetch all rows, then use a foreach loop to display the fields.
if you only want the commentText, then the code that i’ve posted will fetch just the commentText from the table.

code inside of a function has a local scope, so you need to call the function somewhere in your code which needs to return the data back to the flow of the program.
a web page is stateless, so the database connection isn’t sitting there waiting for commands. PHP will terminate at the end of the script (stateless).

I see that you have posted the following code from index.php:
// Get comments from database
get_comments();
// Get commenters from database
get_commenters();

which appears to be calling functions but i don’t see a function named get_comments() or get_commenters(). you have a function named the_comments() in the template_functions.php you would have to replace get_comments(); with the_comments(); in order for the code to be executed.


#26

You can’t make a prepared statement with the query object.

$sql = "SELECT {columns} FROM comments WHERE email = ? ORDER BY {column}";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_GET['email']]);

// now fetch the results
$result = $stmt->fetchAll();
// check the results you get back
print_r($result);

#27

ok. And what do u mean by :placeHolderForVariable and :getEmail here? Is it going to replace any variables here I mean do I have to replace them with variables?


#28

You mean I try this code in the get method where I had my isset block?


#29

Follow the advice posted by astonecipher.


#30

Yes,

<?php

if($_SERVER["REQUEST_METHOD"] == "GET"){
    if(isset($_GET['email'])){
        $sql = "SELECT id, date, mood, commentText, email FROM comments WHERE email = ? ORDER BY date DESC";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$_GET['email']]);

        // now fetch the results
      //  $result = $stmt->fetchAll();
        // check the results you get back
        // print_r($result);
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
        	// sanitize the output
        	foreach($row as $key => $value) {
        		$row[$key] = htmlentities($row[$value]);
        	}
            echo  "<div class = 'comments'>
                  <div class='comment'><div class='ID'>Post ID: {$row['ID']}</div>
                  <div class='date'>Posted on: {$row['date']}</div>
                  <h3>New comment by: {$row['email']}</h3>
                  <div class='mood'>Current mood: {$row['mood']}</div>
                  <div class='comment-text'>{$row['commentText']}</p></div>
                  </div>
                  </div>";
        }
    }
}

#32

Ok I will try and let u know if it worked or not :slight_smile:
Thank You


#33

Ok. Thanks for your time


#34

The statement inside the while loop is giving me an error :frowning:
Its displaying this message:

Uncaught Error: Call to a member function fetch() on array


#35

Is that with the fetchAll commented out? I didn’t notice the fetch, and you can’t fetch what was already brought it…


#36

Yes, I tried with replacing the $result with the variable $stmt but same error


#37

Actually by $stmt in the while loop it doesn’t print anything at all. I tried to get rid of $result in while loop


#38

Try the code again, copy what is up there.

$result holds an array of returned objects if you didn’t comment it out. An array does not have a fetch method.

The while loop should be:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))

and above that all of this should be commented out,

// now fetch the results
//  $result = $stmt->fetchAll();
// check the results you get back
// print_r($result);

#39

Yes, I tried this way but there is no result getting displayed. I have all of the comments the way it was before


#40

Which is where this comes in,

// now fetch the results
$result = $stmt->fetchAll();
// check the results you get back
print_r($result);

Doing this you can see the results.


#41

30%20AM 18%20AM

It is getting the email in the url but all comments are showing up as before