2011-12-01 74 views
0

我創建了一個喜歡和不喜歡按鈕,但是當我點擊喜歡它喜歡我所有的帖子,這裏是我的代碼:按鈕喜歡一個崗位居然喜歡的所有帖子

$sql_posts = mysql_query("SELECT * FROM post WHERE mem_id='$id' ORDER BY post_date DESC LIMIT 30"); 

while($row = mysql_fetch_array($sql_posts)){ 

$postid = $row["id"]; 
$poster_id = $row["mem_id"]; 
$post_body = $row["post_body"]; 
$notokinarray = array("fucker", "ass", "shit", "fuck", "fucking", "damn", "asshole", "cunt", "damnit"); 
$okinarray = array("******", "***", "****", "****", "*******", "****", "*******", "****", "******"); 
$post_body = str_replace($notokinarray, $okinarray, $post_body); 
$post_body = ($activeLinkObject -> makeActiveLink($post_body)); 
$post_date = $row["post_date"]; 
$convertedTime = ($myObject -> convert_datetime($post_date)); 
$whenPost = ($myObject -> makeAgo($convertedTime)); 
$device = $row["device"]; 
$postlikes = $row["likes"]; 
$postlikers = $row["likers"]; 
$postdislikes = $row["dislikes"]; 
$postdislikers = $row["dislikers"]; 

像功能(這是那裏的問題似乎是)

$visitor = $_SESSION['id']; 
     if (isset($_POST['likeIt'])) { 
      if ($postlikers != "") { 

       $postlikers = "$postlikers,$visitor"; } else { $postlikers = "$visitor"; 
       $UpdateArrayLikers = mysql_query("UPDATE post SET likers='$postlikers' WHERE id='$postid'") or die (mysql_error()); 
       $UpdateArrayLikers = mysql_query("UPDATE post SET likes=likes + 1 WHERE id='$postid'") or die (mysql_error()); 
       header("location: profile.php?id=$id"); 
       } 
     } 

    // Inner sql query 
$sql_mem_data = mysql_query("SELECT id, username, firstname, lastname FROM myMembers WHERE id='$poster_id' LIMIT 1"); 
while($row = mysql_fetch_array($sql_mem_data)){ 
     $uid = $row["id"]; 
     $name = $row["username"]; 
     $fname = $row["firstname"]; 
     $lname = $row["lastname"]; 
     $commentForm = ' 
      <form id="form1" name="form1" method="post" action="profile.php?id='.$id.'" style="padding-top:-10;"> 
       <textarea onfocus="value=\'\'" value="Write a comment..." name="comments" id="comments" rows="1"></textarea> 
       <input type="submit" name="comment" id="comment" value="comment" /> 
      </form>'; 



     if (isset($_POST['dislikeIt'])) { 
      $thisPostDislikers = explode(",", $postdislikers); 
      if (!in_array($visitor, $thisPostDislikers)) { 
       if ($postdislikers != "") { $postdislikers = "$postdislikers,$visitor"; } else { $postdislikers = "$visitor"; } 
       $UpdateArrayDislikers = mysql_query("UPDATE post SET dislikers='$postdislikers' WHERE id='$postid'") or die (mysql_error()); 
       $UpdateArrayDislikers = mysql_query("UPDATE post SET dislikes=dislikes + 1 WHERE id='$postid'") or die (mysql_error()); 
       header("location: profile.php?id=$id"); 
      } 
     } 
     if ($id) { 
      $thisPostLikers = explode(",", $postlikers); 
      if (in_array($visitor, $thisPostLikers)){ 
       $likesPost = true; 
      } 
     } 
     if ($id) { 
      $thisPostDislikers = explode(",", $postdislikers); 
      if (in_array($visitor, $thisPostDislikers)){ 
       $dislikesPost = true; 
      } 
     } 

     if ($fname != "") {$name = "$fname $lname"; } // (I added usernames late in my system, this line is not needed for you) 
     /////// Mechanism to Display Pic. See if they have uploaded a pic or not ////////////////////////// 
     $ucheck_pic = "members/$uid/image01.jpg"; 
     $udefault_pic = "members/0/image01.png"; 
     if (file_exists($ucheck_pic)) { 
      $post_pic = '<div style="overflow:hidden; height:40px;"><img src="' . $ucheck_pic . '" width="40px" border="0" /></div>'; 
     } else { 
      $post_pic = '<div style="overflow:hidden; height:40px;"><img src="' . $udefault_pic . '" width="40px" border="0" /></div>'; 
     } 
     if ((!$likesPost) && (!$dislikesPost)) { 
     $postDisplayList .= ' 
      <table style="background-color:#FFF; border:#999 1px solid; border-top:none;" cellpadding="5" width="100%"> 
       <tr> 
        <td width="10%" valign="top"><a href="profile.php?id=' . $poster_id . '">' . $post_pic . '</a> 
        </td> 
        <td width="90%" valign="top" style="line-height:1.5em;"> 
         <span class="liteGreyColor textsize11"> ' . $whenPost . ' <a href="profile.php?id=' . $poster_id . '"><strong>' . $name . '</strong></a> 
         via <em>' . $device . '</em></span><br /> 
         <span> ' . $post_body . '</span> 
         <span> 
          <form id="likeForm" name="likeForm" method="post" action="" style="padding-bottom:-20;"> 
           <input type="submit" name="likeIt" value="Like" /> 
           <input type="submit" name="dislikeIt" value="Dislike" />&nbsp;'.$postlikes.'&nbsp;people like this. 
          </form> 
         </span> 
        </td> 
       </tr> 
       <tr> 
        <td>' . $commentList . ' </td> 
        <td>' . $commentForm . ' </td> 
       </tr> 
      </table>'; 
    } else if ((!$likesPost) && ($dislikesPost)) { 
     $postDisplayList .= ' 
      <table style="background-color:#FFF; border:#999 1px solid; border-top:none;" cellpadding="5" width="100%"> 
       <tr> 
        <td width="10%" valign="top"><a href="profile.php?id=' . $poster_id . '">' . $post_pic . '</a> 
        </td> 
        <td width="90%" valign="top" style="line-height:1.5em;"> 
         <span class="liteGreyColor textsize11"> ' . $whenPost . ' <a href="profile.php?id=' . $poster_id . '"><strong>' . $name . '</strong></a> 
         via <em>' . $device . '</em></span><br /> 
         <span> ' . $post_body . '</span> 
        </td> 
       </tr> 
       <tr> 

        <td>' . $commentList . ' </td> 
        <td>' . $commentForm . ' </td> 
       </tr> 
      </table>'; 
    } else if ((!$dislikesPost) && ($likesPost)) { 
     $postDisplayList .= ' 
      <table style="background-color:#FFF; border:#999 1px solid; border-top:none;" cellpadding="5" width="100%"> 
       <tr> 
        <td width="10%" valign="top"><a href="profile.php?id=' . $poster_id . '">' . $post_pic . '</a> 
        </td> 
        <td width="90%" valign="top" style="line-height:1.5em;"> 
         <span class="liteGreyColor textsize11"> ' . $whenPost . ' <a href="profile.php?id=' . $poster_id . '"><strong>' . $name . '</strong></a> 
         via <em>' . $device . '</em></span><br /> 
         <span> ' . $post_body . '</span> 
         <span>       
          <form id="dislikeForm" name="dislikeForm" method="post" action=""> 
           <input type="submit" name="dislikeIt" value="Dislike" />&nbsp;'.$postlikes.'&nbsp;people like this. 
          </form> 
         </span> 
        </td> 
       </tr> 

       <tr><td>' . $commentList . ' </td></tr> 
       <tr><td>' . $commentForm . ' </td></tr> 
      </table>'; 
    } else { 
     $postDisplayList .= ' 
      <table style="background-color:#FFF; border:#999 1px solid; border-top:none;" cellpadding="5" width="100%"> 
       <tr> 
        <td width="10%" valign="top"><a href="profile.php?id=' . $poster_id . '">' . $post_pic . '</a> 
        </td> 
        <td width="90%" valign="top" style="line-height:1.5em;"> 
         <span class="liteGreyColor textsize11"> ' . $whenPost . ' <a href="profile.php?id=' . $poster_id . '"><strong>' . $name . '</strong></a> 
         via <em>' . $device . '</em></span><br /> 
         <span> ' . $post_body . '</span> 
        </td> 
       </tr> 
       <tr><td>&nbsp;'.$postlikes.'&nbsp;people like this.' . $commentList . ' </td></tr> 
      </table>'; 
    } 
} 

} 

類似的功能在第一個while循環使用戶喜歡所有的帖子。

+1

不錯的bug,通過哪來的方式 – marcio

+0

'$ postid'得到第二片段設置? –

+0

$ postid被宣佈更遠,那個代碼我實際上忘記了,感謝評論 –

回答

0

您可以通過您的海報ID獲取所有帖子。 然後循環瀏覽這些帖子。 然後在循環中,在第一次while循環之後,再次通過myMembers表使用海報ID獲取用戶數據。 並在第二個while循環中更新當前帖子ID的數據。

由於第一次while循環遍歷所有帖子,因此您會在第二次inner while循環中更新每個帖子id的詳細信息。

所以問題是SQL查詢第一次外循環,我認爲。或者,你錯過了什麼是你需要包括

   $UpdateArrayLikers = mysql_query("UPDATE post SET likers='$postlikers' WHERE id='$postid'") or die (mysql_error()); 
       $UpdateArrayLikers = mysql_query("UPDATE post SET likes=likes + 1 WHERE id='$postid'") or die (mysql_error()); 
       header("location: profile.php?id=$id"); 

if ($postlikers != "") { $postlikers = "$postlikers,$visitor"; } else { $postlikers = "$visitor"; } 

條件。

編輯:如果只有$ postlikers不是空的,你應該更新張貼表的相似者和喜歡。像這樣的東西。

if ($postlikers != "") { 

$postlikers = "$postlikers,$visitor"; } else { $postlikers = "$visitor"; 
$UpdateArrayLikers = mysql_query("UPDATE post SET likers='$postlikers' WHERE id='$postid'") or die (mysql_error()); 
$UpdateArrayLikers = mysql_query("UPDATE post SET likes=likes + 1 WHERE id='$postid'") or die (mysql_error()); 
        header("location: profile.php?id=$id"); 

} 
+0

所以我應該移動if函數來添加喜歡第一個while循環 –

+0

看到編輯。我認爲如果不需要檢查「myMembers」表是否包含具有id的行,我認爲可以移動這個「if」條件阻塞第二個內部「while」循環的一側(實際上它的行爲就像「if」條件)更新「發佈」表之前$ poster_id。 –

+0

我將用新代碼更新帖子,看看它是否有效 –