帖子评论(PHP)

时间:2015-02-07 05:40:43

标签: javascript php iframe

我目前正在尝试在我的网站上使用评论。评论将用于我的帖子。

问题是,我目前正在使用iframe来展示它们。

我希望代码与我的帖子在同一页面上。

我会通过发布我的代码向您展示我的意思:

comment_frame.php:

<?php
if (isset($_POST['postComment' . $getid . ''])) {
    $post_body = $_POST['post_body'];
    $posted_to = "Chris";
    $insertPost = mysql_query("INSERT INTO post_comments VALUES ('','$post_body','$user','$posted_to','0','$getid')");
    echo "<p style='color: green'>Comment Posted!</p><br /><br />";
}

// Get relevent comments
$get_comments = mysql_query("SELECT * FROM post_comments WHERE post_id='$getid' ORDER BY id DESC");
$count = mysql_num_rows($get_comments);
if ($count != 0) {
while ($comment = mysql_fetch_assoc($get_comments)) {

    $comment_body = $comment['post_body'];
    $posted_to = $comment['posted_to'];
    $posted_by = $comment['posted_by'];
    $removed = $comment['post_removed'];

    echo "<b><a href='$posted_by' target='_blank'>$posted_by</a> said: <br /></b>".$comment_body."<hr /><br />";
}
}
else 
{
    // Do nothing!
}
?>

home.php:

<?php
// If the user is logged in
$getposts = mysql_query("SELECT * FROM posts WHERE user_posted_to='$user' ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
                        $id = $row['id'];
                        $body = $row['body'];
                        $date_added = $row['date_added'];
                        $added_by = $row['added_by'];
                        $user_posted_to = $row['user_posted_to'];

                        $get_user_info = mysql_query("SELECT * FROM users WHERE username='$added_by'");
                        $get_info = mysql_fetch_assoc($get_user_info);
                        $profilepic_info = $get_info['profile_pic'];
                        if ($profilepic_info == "") {
                            $profilepic_info = "./img/default_pic.png";
                        }
                        else
                        {
                            $profilepic_info = "./userdata/profile_pics/".$profilepic_info;
                        }
                        ?>
                        <script language="javascript">
                            function toggle<?php echo $id; ?>() {
                                var ele = document.getElementById("toggleComment<?php echo $id; ?>");
                                var text = document.getElementById("displayText<?php echo $id; ?>");
                                if (ele.style.display == "block") {
                                    ele.style.display = "none";
                                }
                                else
                                {
                                    ele.style.display = "block";
                                }
                            }
                        </script>
                        <?php
                        echo "
                        <br />
                        <div class='newsFeedPost'>
                        <div class='newsFeedPostOptions'>
                        <a href='javascript:;' onClick='javascript:toggle$id()'>Show Comments</a>
                        <div style='float: left;'>
                        <a href='$added_by'><img src='$profilepic_info' height='60' /></a>
                        </div>
                        <div class='posted_by'><a href='$added_by'>$added_by</a> wrote:</div>
                        <br /><br />
                        <div style='max-width: 600px; height: auto;'>
                        $body<br /><br /><br /><p />
                        </div>
                            Like &ndash; Re-Shout! &ndash; Comment
                        <br /><iframe src='./comment_frame.php?id=$id' frameborder='0' style='max-height: 200px; width: 100%; min-height: 10px;'></iframe>
                        </div>
                        </div>
                        ";
                        }
}
?>

如您所见,它使用iframe显示了comment_frame.php页面。我希望所有代码都在一个页面中。我该怎么做?

1 个答案:

答案 0 :(得分:1)

如果用户添加新评论,则将你的home.php代码置于else条件下,否则第二个条件将起作用

  if (isset($_POST['postComment' . $getid . ''])) {
     your code here
    }else {
       // home.php code herer
    }