我有以下三個表 - users_thoughts
,users
和post_favourites
。我在社交網站上有一項功能,允許用戶收藏一篇文章(文章存儲在user_thoughts
中,當某篇文章被收錄時,即favourite_post.php
被調用時,它會將收藏夾存儲在post_favourites
表中)。如果聲明沒有被執行,當它預計工作
如果登錄的用戶有不是贊成帖子,它會顯示Glyphicon heart-empty
。但是,如果用戶HAS贊成帖子,它將顯示Glyphicon-heart
。
讓我們假設我有我的表如下行:
users
表:
id: 1
first_name: conor
id: 2
first_name: Alice
id: 3
first_name: Anderson
user_thoughts
表:
id: 100
message: This is a post by Alice.
added_by: Alice
id: 101
message: This is a post by Anderson
added_by: Anderson
post_favourites
表:
id: 1
user_id: 1 (This is the id of the user who has favourited the post, see users table)
thought_id: 101
假設我登錄爲Conor
。正如你所看到的,Conor
已被收錄Anderson
的帖子,所以Glyphicon-heart
應該出現,因爲登錄用戶已經贊成安德森的帖子。但Glyphicon-heart-empty
正在出現,即使我的數據庫說conor已經支持安德森的職位。
這裏是我的查詢:
注意:$username
是登錄的用戶創建會話變量。
$count = mysqli_query ($connect, "SELECT * FROM user_thoughts");
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
$thought_id = $row['id'];
}
// Get all user_ids attachted to a thought ($thought_id)
$get_user_id = mysqli_query ($connect, "SELECT * FROM post_favourites WHERE thought_id = '$thought_id'");
$id_fetch = mysqli_fetch_assoc ($get_user_id);
$all_user_id = $id_fetch ['user_id'];
$post_id = $id_fetch ['thought_id'];
// get id of users from users table
$get__id = mysqli_query ($connect, "SELECT id FROM users WHERE username = '$username'");
$id_fetch2 = mysqli_fetch_assoc ($get__id);
$logged_in_user = $id_fetch2 ['id'];
if ($post_id == $thought_id){
// If the post has already been favourited by the username, then display this icon with funtionality.
if ($all_user_id == $logged_in_user){
echo "$get_num_of_favs
<a href='/inc/unfavourite_post.php?id=";?><?php echo $thought_id;?><?php echo "'>
<span class='glyphicon glyphicon-heart' aria-hidden='true' style='padding-right: 5px;'></span>
</a>";
}
// if the post hasn't been favourited by the username, display this icon.
else {
echo "$get_num_of_favs
<a href='/inc/favourite_post.php?id=";?><?php echo $thought_id;?><?php echo "'>
<span class='glyphicon glyphicon-heart-empty' aria-hidden='true' style='padding-right: 5px;'></span>
</a>";
}
}
我需要它,就已經收藏最多用戶的帖子是誰在與heart
圖標即,如果在執行語句中dislayed記錄。但目前,安德森的This is a post by Anderson
- 這是Conor喜歡的職位,顯示heart-empty
- 意味着else語句正在執行,我不知道爲什麼?
喜添。似乎與這種變化了一些進展。安德森的職位目前正與'heart'圖標顯示,但是當我去到愛麗絲的'profile_page.php',看到她的帖子,被旁邊顯示她的職位都沒有圖標。我希望看到'心臟,empty'旁邊都是Alice的帖子,因爲登錄的用戶'Conor'尚未收藏任何Alice的帖子。 – Freddy
既沒有你聽說過加入? – Shadow