2015-02-07 144 views
0

我正在嘗試在我的網站上使用評論。評論將用於我的帖子。對帖子的評論(PHP)

事情是,我目前正在使用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頁面。我希望所有的代碼都在一個頁面中。我會怎麼做?

+0

再次多了一個PHP編碼器使用爺爺風格編碼這在很大程度上忽視安全!!!!不要使用'mysql_query',使用PDO或mysqli。 – 2015-02-07 05:56:38

回答

1

把UR home.php代碼中的其他條件,如果用戶增加新的評論的首要條件將工作,否則第二個

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

我想要做的是在home.php文件中獲取所有這些代碼,然後在用戶點擊時顯示評論框(例如「評論」)。我想我需要一個像$ comment這樣的變量,並將該變量替換爲iframe。我試圖這樣做,因爲一切都在回聲命令中。有任何想法嗎? – 2015-02-07 05:58:54

+0

使用ajax ..我認爲這將是更好:)甚至沒有刷新頁面..還..在home.php只是添加您的評論代碼..它應該工作很好因爲你添加了isset函數。 – 2015-02-07 06:02:00

+0

感謝您的幫助。我一定會去看看AJAX :) – 2015-02-07 06:06:24