Creating a Comment Anchor

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-05-25T02:50:22Z Indexed on 2010/05/25 3:01 UTC
Read the original article Hit count: 459

Filed under:
|

Hello,

The function below allows a user to insert a comment into a MySQL table called "comment." Then, the file "comments2.php" displays all comments for a given submission.

Right after a user submits a comment, I would like the top of the user's browser to be anchored by the comment the user just submitted.

How can I do this?

Thanks in advance,

John

The function:

function show_commentbox($submissionid, $submission, $url, $submittor, $submissiondate, $countcomments, $dispurl)
{
echo '<form  action="http://www...com/.../comments/comments2.php" method="post"> 
    <input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">
    <input type="hidden" value="'.$submissionid.'" name="submissionid">  
    <input type="hidden" value="'.stripslashes($submission).'" name="submission">
    <input type="hidden" value="'.$url.'" name="url">
    <input type="hidden" value="'.$submittor.'" name="submittor">
    <input type="hidden" value="'.$submissiondate.'" name="submissiondate">
    <input type="hidden" value="'.$countcomments.'" name="countcomments">
    <input type="hidden" value="'.$dispurl.'" name="dispurl">

    <label class="addacomment" for="title">Add a comment:</label>

    <textarea class="checkMax" name="comment" type="comment" id="comment" maxlength="1000"></textarea>  

    <div class="commentsubbutton"><input name="submit" type="submit" value="Submit"></div> 
</form>
'; 
}

The file comments2.php contains:

$query = sprintf("INSERT INTO comment VALUES (NULL, %d, %d, '%s', NULL)", $uid, $subid, $comment);

mysql_query($query) or die(mysql_error());


$submissionid = mysql_real_escape_string($_POST['submissionid']);
$submissionid = mysql_real_escape_string($_GET['submissionid']);

$sqlStr = "SELECT comment.comment, comment.datecommented, login.username
FROM comment
LEFT JOIN login ON comment.loginid=login.loginid
WHERE submissionid=$submissionid
ORDER BY comment.datecommented ASC 
LIMIT 100";         

$tzFrom1 = new DateTimeZone('America/New_York'); 
$tzTo1 = new DateTimeZone('America/Phoenix'); 


$result = mysql_query($sqlStr);

$arr = array(); 
echo "<table class=\"commentecho\">";
$count = 1;
while ($row = mysql_fetch_array($result)) { 
    $dt1 = new DateTime($row["datecommented"], $tzFrom1);  
    $dt1->setTimezone($tzTo1);
    echo '<tr>';
    echo '<td rowspan="3" class="commentnamecount">'.$count++.'.</td>';
    echo '<td class="commentname2"><a href="http://www...com/.../members/index.php?profile='.$row["username"].'">'.$row["username"].'</a></td>';
    echo '<td rowspan="3" class="commentname1">'.stripslashes($row["comment"]).'</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td class="commentname2">'.$dt1->format('F j, Y').'</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td class="commentname2a">'.$dt1->format('g:i a').'</td>';
    echo '</tr>';
    }
echo "</table>";

The fields in the MySQL table "comment":

commentid loginid submissionid comment datecommented

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql