2011-05-25 32 views
0

我想標識每個評論,以便任何成員對評論的評論,INSERT和SELECT知道在哪裏顯示評論,評論誰在評論和在什麼評論。這就像爲每個評論創建一個ID,以便SELECT在每個消息的位置顯示每條消息。 BU我不知道這是一個HTML,PHP或JavaScript問題來處理。 jsfiddle是here想知道這是一個HTML,PHP或JavaScript問題

PHP

我挑中的URL字符串

$id = $_GET['id']; // this id of the user being commented in the user table I guess I will need that id to identify which user is being commented "profile" that's the id of his profile 
if (isset($_SESSION['id'])) {  
$userid = $_SESSION['id']; 
$username = $_SESSION['name']; } 
    else { 

    // do this 

    } 
// the userid contains the id and name of the user commenting "the user logged in". 

if ($_POST['comment_field_1'] != ""){ 



    $comment_field_1 = $_POST['comment_field_1']; 
$comment_field_1 = stripslashes($comment_field_1); 
$comment_field_1 = strip_tags($comment_field_1); 
$comment_field_1 = mysql_real_escape_string($comment_field_1); 
$comment_field_1 = eregi_replace("'", "'", $comment_field_1); 

$sql = mysql_query("INSERT INTO comments (mem_id,commented_men_id, the_message,parent_id, message_date) 
VALUES('$userid','$idc','$comment_field_1', $parent_id now())") 
or die (mysql_error()); 

    // i guess the $parent_id should be initialize somewhere in the top with a value of 0 or 1 depending weather it is a response or a comment. 
} 

    // men_id is the field of the $userid, commented_men_id is the field for the commented on, $idc variable etc... 

if ($_POST['comment_field_2'] != ""){ // responses form field 



    $comment_field_2 = $_POST['comment_field_2']; 
$comment_field_2 = stripslashes($comment_field_2); 
$comment_field_2 = strip_tags($comment_field_2); 
$comment_field_2 = mysql_real_escape_string($comment_field_2); 
$comment_field_2 = eregi_replace("'", "'", $comment_field_2); 

$sql = mysql_query("INSERT INTO comments (mem_id,commented_men_id, the_message,sub_commented_id, message_date) 
VALUES('$userid','$idc','$comment_field_1', $sub_commented_id now())") 
or die (mysql_error()); 

    // here instead of $parent_id I use field created for responses to tag a comment as a response should be initialize somewhere in the top with a value of 0 or 1 depending weather it is a response or a comment. I am not sure weather I should leave the parent_id variable or come with this new field $sub_commented_id. What do you think? 
} 

    // men_id is the field of the $userid, commented_men_id is the field for the commented on, $idc variable etc... 


if (isset($_SESSION['id'])) { 

if ($_SESSION['id'] == $userid) { 
$the_message_form = ' 


comment (220 char max) 
<form align="left" id="comment" action="profile.php?id='.$idc.'" method="post" enctype="multipart/form-data" name="message_from"> 
<textarea type="text" id="text1" name="comment_field_1" rows="3" style="width:97%;"></textarea> 

<input class="commentinput" name="submit" type="button" value="submit comment" align="left" /> 

</form>'; 

    } 
} 

$sql1 = mysql_query("SELECT id, mem_id, commented_men_id, the_message,parent_id, message_date FROM comments WHERE commented_men_id='$idc' ORDER BY message_date DESC LIMIT 20")or die (mysql_error()); 
$i = 0; 
while($row = mysql_fetch_array($sql1)){ 
    $j = $i++; 
    $message_id = $row["id"]; 
    $uid = $row["mem_id"]; 
    $the_message= $row["the_message"]; 
    $message_date = $row["message_date"]; 
    $message_date = strftime("%b %d, %Y, %Y %I:%M:%S %p", strtotime($message_date)); 


    $sql_mem_data = mysql_query("SELECT id, name FROM users WHERE id='$uid' LIMIT 1"); 
    while($row2 = mysql_fetch_array($sql_mem_data)){ 
      $uid = $row2["id"]; 
      $ufirstname = $row2["name"]; 
      $ufirstname = mb_strimwidth($ufirstname, 0, 20, '&hellip;'); 


    } 

      $messageDisplayList .= ' 
       <table width="100%" align="center" cellpadding="4" bgcolor="#CCCCCC" style="margin-bottom:0.5em"> 
     <tr> 
      <td width="auto" height="auto" bgcolor="#EFEFEF" style="vertical-align:top"><a width="auto" height="auto" title="'.$row2["name"] .' "href="profile.php?id='.$uid .'&name='.$row2["name"].'">' .$user_pic. '</a><br /> 
      </td> 
      <td width="93%" bgcolor="#EFEFEF"><a href="profile.php?id='.$uid.'">' . $ufirstname . ' </a> &bull; <span style="font-size:10px; font-weight:bold; color:#A6A6A6;">' . $message_date . '</span><br /> 
    <p id="only"> ' . $the_message . '</p><span class="comment">&bull;<a style="padding-left:3.5px; color:#0099ff" href ="">Comment</a></span><div class="commentForm"><form align="left" id="bring" action="profile.php?id='. $uid .'" method="post" enctype="multipart/form-data" name="message_from"> 
<textarea name="comment_field_2" type="text" id="text2" rows="3" style="width:100%; height:30px;"></textarea> 
<input id="bringinput" name="submit" type="button" value="submit" align="left" /> 
</form></div> 
</td> 
    </tr> 
     </table> <br/>'; 
     } 
     $sql_sub_coment = mysql_query ("SELECT id, mem_id, commented_men_id, sub_commented_id, the_message, message_date FROM comments WHERE sub_commented_id='$blabid3' AND sub_commented_id != 0 ORDER BY blab_date DESC LIMIT 20"); 
    while($row3= mysql_fetch_array($sql_sub_coment)){ 
    $$message_id3 = $row3_sub["id"]; 
    $uid3 = $row3_sub["mem_id"] ; 
    $the_message3 = $row3_sub["the_message"]; 
    $message_date3 = $row3_sub["message_date"]; 
    $message_date3 = strftime("%b %d, %Y, %Y %I:%M:%S %p", strtotime($blab_date3)); 
    $sql_mem_data = mysql_query("SELECT id, name FROM user WHERE id='$uid3' LIMIT 1"); 
     while($row4 = mysql_fetch_array($sql_mem_data)){ 
      $uid4 = $row4["category_id"]; 
      $ufirstname2 = $row4["name"]; 
      $ufirstname2 = mb_strimwidth($ufirstname2, 0, 20, '&hellip;'); 

} 
      $$messageDisplayList2 .= '<table width="100%" align="center" cellpadding="4" bgcolor="#CCCCCC" style="margin-bottom:0.5em"> 
     <tr> 
      <td width="auto" height="auto" bgcolor="#FFFFFF"><a width="auto" height="auto" title="'.$row3["name"] .' "href="profile.php?category_id='.$uid4 .'">' .$blabber_pic2. '</a><br /> 
      </td> 
      <td width="93%" bgcolor="#EFEFEF"><a href="profile.php?id='.$uid4.'">' . $ufirstname2 . ' </a> &bull; <span style="font-size:10px; font-weight:bold; color:#A6A6A6;">' . $message_date3 . '</span><br /><p id="submessage">' . $the_message3 . '</p> 
      </td> 
     </tr> 
     </table>'; 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" /> 

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
$(".commentForm").hide(); 
    $(".comment").bind('click', function(){ 
     $(this).nextAll(".commentForm:first").slideToggle(); 
    return false; 
}); 
var html = $("#text1").html(); 
    $(".blabinput").click(function() { 
console.log("AJAX EVENT OCCURED") 
     $.post($('#comment').attr("action"), 
       $("#comment").serialize(), 

     function(html) { 
         // Find the first table after the form in the HTML from the server and append it to the form on the page. 

         $(html) .find('form + table').insertAfter('#comment').after('<br />'); 

     }); 
     }); 
var html2 = $("#text2").html(); 
    $(".bringinput").click(function() { 
console.log("AJAX EVENT OCCURED") 
     $.post($('#bring').attr("action"), 
       $("#bring").serialize(), 

     function(html2) { 
         // Find the first table after the form in the HTML from the server and append it to the form on the page. 

         $(html2) .find('form + table').insertAfter('#bring').after('<br />'); 

     }); 
     }); 
    }); 

的ID這是我到目前爲止所。父表單的第一種形式是使用id #message的jquery進行提交和發佈,然後第二種形式的id或響應形式的#bring id無法正常工作,它插入但將註釋發佈到p標籤中僅用id,這就是父註釋發佈的地方,而不是響應評論表單。它應該在下面發佈,但不知何故,我有HTML和jQuery的混亂。在那裏需要幫助。

評論文字滑動切換,但第二次點擊時,意味着有時它不會在第一次點擊時切換。但現在的主要問題是獲取下面顯示的響應和Ajaxing而不刷新瀏覽器。

+3

如果你也發佈你的PHP,它可能會有所幫助。我看到的第一個問題:你有兩個元素相同的ID:

... – 2011-05-25 18:53:41

+0

盧克你是在正確的方向我認爲我應該創建一個計數器來爲每次發表評論後生成一個不同的ID號。但是現在每個評論中都有一個不同的id,那麼我該如何使用它呢? – fello 2011-05-25 19:18:07

回答

2

您將需要修改保存您的註釋以添加「父」列的數據庫表。頂級評論將有一個父ID爲0,這意味着它不是對任何其他評論的迴應。

響應的父級列中將包含頂級註釋的ID。

修改表格後,您需要修改您的PHP代碼以識別對頂級註釋的響應,並修改您的HTML以適當地顯示它們。

+0

我將在 – fello 2011-05-25 19:19:04

+0

後面進行一些修改,我所做的是爲嵌套註釋而不是您的方法創建外鍵,但是我將爲註釋創建父列。對於響應,我有一個intmented爲11的commented_mem_id字段。 – fello 2011-05-25 22:19:46

+0

我已經編輯了上面的內容,現在使用php部分,然後html會晚點來。如果您不介意讓我們來看看php。 – fello 2011-05-25 22:46:28

相關問題