我創建了一個喜歡和不喜歡按鈕,但是當我點擊喜歡它喜歡我所有的帖子,這裏是我的代碼:按鈕喜歡一個崗位居然喜歡的所有帖子
$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" /> '.$postlikes.' 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" /> '.$postlikes.' 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> '.$postlikes.' people like this.' . $commentList . ' </td></tr>
</table>';
}
}
}
類似的功能在第一個while循環使用戶喜歡所有的帖子。
不錯的bug,通過哪來的方式 – marcio
'$ postid'得到第二片段設置? –
$ postid被宣佈更遠,那個代碼我實際上忘記了,感謝評論 –