2017-09-01 75 views
0

我正在處理的網站允許用戶發佈多個評論到網站,每個評論都有一個唯一的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(); 
} 

回答

0

有閱讀你的代碼的問題不好意思IM,IM沒有足夠的經驗..但只是我的2美分,至使「喜歡」按鈕邏輯..

  1. 保存用戶ip,在數據庫中創建一個爲該用戶保存的變量的數組。
  2. 創建一個隱藏或單選按鈕表單,例如name =「like_btn」 ,您可以通過郵政發送給php。
  3. 如果$ 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