views:

34

answers:

1

Hi guys! I'm in the process of coding my very first blog. With the help of various tutorials, and other forums I have managed to gather a semi-working code.

Right now I have a code that takes and displays the comment, but the problem is I wish to display Gravatars beside each comment. I was just wondering how exactly I would go about implementing the code that they provided on their website.

Here is my current comment form:

            <?php

}
$commenttimestamp = strtotime("now");

$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
    $timestamp = date("l F d Y", $row['timestamp']);
    printf("<hr />");
    print("<p>" . stripslashes($row['comment']) . "</p>");
    printf("<p>Comment by <a href=\"%s\">%s</a> @ %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
    printf("<hr />");
}
?>



    <form method="post" action="process.php">

    <p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />

    <input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">

    <strong><label for="name">Name:</label></strong> <input type="text" name="name" id="name" size="25" /><br />

    <strong><label for="email">E-mail:</label></strong> <input type="text" name="email" id="email" size="25" /><br />

    <strong><label for="url">URL:</label></strong> <input type="text" name="url" id="url" size="25" value="http://" /><br />

    <strong><label for="comment">Comment:</label></strong><br />
    <textarea cols="25" rows="5" name="comment" id="comment"></textarea></p>

    <p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>

    </form>

If you wish for me to post the php that processes each comment here as well just ask below.

My code now:

 <?php

    function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
        $url = 'http://www.gravatar.com/avatar/';
        $url .= md5( strtolower( trim( $email ) ) );
        $url .= "?s=$s&d=$d&r=$r";
        if ( $img ) {
            $url = '<img src="' . $url . '"';
            foreach ( $atts as $key => $val )
                $url .= ' ' . $key . '="' . $val . '"';
            $url .= ' />';
        }
        return $url;
    }

}
$commenttimestamp = strtotime("now");

$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
    $timestamp = date("l F d Y", $row['timestamp']);
    printf("<hr />");
    print("<p>" . stripslashes($row['comment']) . "</p>");
    printf("<p>Comment by <a href=\"%s\">%s</a> @ %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
    echo $imagetag = "<img src='" . get_gravatar($email) . "' />";
    printf("<hr />");
}
?>
+2  A: 

You want an image tag whose src comes from the gravatar function.

Something like:

$imagetag = "<img src='" . get_gravatar($email_address) . ' />";

(You'll need to echo this variable where you want it to display.)

The only required parameter of the get_gravatar function is the email address, so just pass this to get_gravatar and you get the url of the gravatar image.

Skilldrick
@Skilldrick I've got it displaying an avatar now. But it isn't displaying my personal Gravatar.I've added the new code above.
ThatMacLad
Are you passing your email address? I really think you should take some time to learn some basic PHP - it'll really help. Have a look at http://www.w3schools.com/php/default.asp
Skilldrick