10.5. Retrieving All Comments for a Given Entry

You can now that store comments in the database; your next task is to write a method that retrieves them.

This method, called retrieveComments(), accepts one argument: the ID of the blog that you need to retrieve comments for ($blog_id).

This new method works similarly to your retrieveEntries() function, except that you store the entries as an array in a property of the Comments class itself, for use with other methods.

Begin by declaring a new $comments property in the class; you use this property to store the loaded comments in memory.

Next, you need to write a SQL query that pulls all the entries from the comments table that match the given blog ID. Be sure to retrieve every column from the table with your query.

The next step is to prepare your SQL statement and execute it using the value of $blog_id that was passed as an argument to your method.

Now loop through the returned entries and store each row in the $comments property as an array element.

Finally, check to see whether any comments exist for the given blog entry. If not, place some default text in the array to let users know that no comments have been posted yet.

You can create this method by adding the code in bold to comments.inc.php:

<?php

include_once 'db.inc.php';

class Comments
{
    // Our database connection
    public $db;

    // An array for containing the entries
    public $comments;

    // Upon class instantiation, open a database connection
    public function __construct()
    {
        // Open a database connection and store it
        $this->db = new PDO(DB_INFO, DB_USER, DB_PASS);
    }

    // Display a form for users to enter new comments with
    public function showCommentForm($blog_id)
    {
        return <<<FORM
<form action="/simple_blog/inc/update.inc.php"
    method="post" id="comment-form">
    <fieldset>
        <legend>Post a Comment</legend>
        <label>Name
            <input type="text" name="name" maxlength="75" />
        </label>

<label>Email
            <input type="text" name="email" maxlength="150" />
        </label>
        <label>Comment
            <textarea rows="10" cols="45" name="comment"></textarea>
        </label>
        <input type="hidden" name="blog_id" value="$blog_id" />
        <input type="submit" name="submit" value="Post Comment" />
        <input type="submit" name="submit" value="Cancel" />
    </fieldset>
</form>
FORM;
    }

    // Save comments to the database
    public function saveComment($p)
    {
        // Sanitize the data and store in variables
        $blog_id = htmlentities(strip_tags($p['blog_id']),ENT_QUOTES);
        $name = htmlentities(strip_tags($p['name']),ENT_QUOTES);
        $email = htmlentities(strip_tags($p['email']),ENT_QUOTES);
        $comment = htmlentities(strip_tags($p['comment']),ENT_QUOTES);

        // Generate and prepare the SQL command
        $sql = "INSERT INTO comments (blog_id, name, email, comment)
                VALUES (?, ?, ?, ?)";
        if($stmt = $this->db->prepare($sql))
        {
            // Execute the command, free used memory, and return true
            $stmt->execute(array($blog_id, $name, $email, $comment));
            $stmt->closeCursor();
            return TRUE;
        }
        else
        {
            // If something went wrong, return false
            return FALSE;
        }
    }

// Load all comments for a blog entry into memory
    public function retrieveComments($blog_id)
    {
        // Get all the comments for the entry
        $sql = "SELECT id, name, email, comment, date
                FROM comments
                WHERE blog_id=?
                ORDER BY date DESC";
        $stmt = $this->db->prepare($sql);
        $stmt->execute(array($blog_id));

        // Loop through returned rows
        while($comment = $stmt->fetch())
        {
            // Store in memory for later use
            $this->comments[] = $comment;
        }

        // Set up a default response if no comments exist
        if(empty($this->comments))
        {
            $this->comments[] = array(
                'id' => NULL,
                'name' => NULL,
                'email' => NULL,
                'comment' => "There are no comments on this entry.",
                'date' => NULL
            );
        }
    }
}

?>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.181.57