我创建了一个喜欢和不喜欢的按钮但是当我点击它喜欢我的所有帖子时,这是我的代码:
$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循环中的like函数使用户喜欢所有帖子。
答案 0 :(得分:0)
您可以通过海报ID获取所有帖子。 然后循环浏览这些帖子。 然后在循环内部,在第一次while循环之后,再次使用poster id通过myMembers表获取用户数据。 在第二个while循环中,您将更新当前帖子ID的数据。
由于第一个while循环遍历所有帖子,因此您将更新第二个内部while循环中每个帖子ID的详细信息。
问题是我认为第一个外部while循环的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");
}