Php比较html按钮名称与数据库中的名称
问题描述:
我正在处理的网站允许用户发布多个评论到网站,每个评论都有一个唯一的id,它在数据库中是auto_incremented的。然后我将这些ID设置为评论对象列表。在我的html中,然后检索这些ID并为每个评论对象创建一组喜欢和不喜欢的按钮(带有foreach循环)。Php比较html按钮名称与数据库中的名称
我遇到的问题是,当用户点击一个像按钮时,与它的id(动态名称)名称相同的按钮的名称应该与该ID在数据库中。但点击按钮只会改变屏幕上显示的评论之一。我目前使用会话来存储数组,并将其值发送到DA类,但没有取得任何成功。
我不知道如何正确发送和比较名称与数据库中的ID。
的HTML:
<?php foreach ($comments->getComments() as $comment) : ?>
<!--The like button:-->
<form class="userActions" action="../Controller/index.php" method="post">
<input type="submit" name="<?php echo "comment_#" . $comment->getID() . "_Likes"; ?>" class="btnLike btnLikeStyle like" value="Like">
<input type="hidden" name="action" value="comment_like">
</form>
<!--Show number of likes on the page:-->
<div>
<!--display the likes number from the database-->
<label class="lblLikes"><?php echo htmlspecialchars($comment->getLikes()); ?></label>
</div>
<!--Dislike button:-->
<form class="userActions" action="../Controller/index.php" method="post">
<input type="submit" name="<?php echo "comment_#" . $comment->getID() . "_Likes"; ?>" class="btnDislike btnDislikeStyle dislike" value="Dislike">
<input type="hidden" name="action" value="comment_dislike">
</form>
<?php endforeach; ?>
控制器:
case 'comment_like':
$commentID = $comment->getID();
$comments = comments::addLike($theCommentID);
include('../View/home.php');
break;
case 'comment_dislike':
$commentID = $comment->getID();
$comments = comments::addDislike($theCommentID);
include('../View/home.php');
break;
评论类
function addLike($commentID) {
CommentDA::addCommentLike($commentID);
}
function addDislike($commentID) {
CommentDA::addCommentDislike($commentID);
}
的DA类:
public static function addCommentLike($commentID) {
$db = self::getConnection();
$query = 'UPDATE comments
SET commentLikes = CommentLikes + 1
WHERE CommentID = :commentIDPlaceholder';
$statement = $db->prepare($query);
$statement->bindValue(':commentIDPlaceholder', $commentID);
try {
$statement->execute();
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
$statement->closeCursor();
}
public static function addCommentLike($commentID) {
$db = self::getConnection();
$query = 'UPDATE comments
SET commentLikes = CommentLikes - 1
WHERE CommentID = :commentIDPlaceholder';
$statement = $db->prepare($query);
$statement->bindValue(':commentIDPlaceholder', $commentID);
try {
$statement->execute();
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
$statement->closeCursor();
}
答
有阅读你的代码的问题不好意思IM,IM没有足够的经验..但只是我的2美分,至使“喜欢”按钮逻辑..
- 保存用户ip,在数据库中创建一个为该用户保存的变量的数组。
- 创建一个隐藏或单选按钮表单,例如name =“like_btn” ,您可以通过邮政发送给php。
- 如果$ like_btn === 1的值,则显示'喜欢'的图片,否则显示另一张图片。 要计算喜欢你需要计数有多少用户有这个变量设置为1在你的分贝。
按钮的例子..
<label for="like_button">
<div
<?php
if($like_status == "liked")
{
echo'class="liked_class"';
}else
{
echo 'class="empty_class"';
};
?>
><input type="radio" id="liked" name="like_status" value="like_button" form="form01"
<?php
if ($like_status == "liked")echo "checked";
?>
> Like button content
</div></label>
可以将不透明度设置为0或移动的单选按钮的possition远将其隐藏。 (Hide radio button while keeping its functionality)