[英]Pull data from one table and check against a different table then show match
Here is the situation. 这是情况。 I have joins everywhere in other parts of the site and everything works fine, just this one hiccup that has been driving me crazy.
我在该网站其他部分的任何地方都加入了会议,并且一切正常,只是这一点让我发疯。 It is probably simple and I am just missing something.
这可能很简单,我只是想念一些东西。
This is a system where users can log in and follow each others blogs. 这是一个用户可以登录并互相关注博客的系统。
The Database Each Table has different amount of Columns and none of the Columns use the same name. 数据库每个表都有不同数量的列,并且没有一个列使用相同的名称。
Table1 = userdetails (uses ID as Primary ID) 表1 =用户详细信息(使用ID作为主要ID)
Table2 = blogs (uses BlogID as Primary ID) - it has BlogID, WriterID blogtitle, blogtext, blogdate, etc. as rows Table2 =博客(使用BlogID作为主要ID)-它具有BlogID,WriterID blogtitle,blogtext,blogdate等作为行
Table3 = blogFollowers (uses FollowingBlogID as primary ID) this is the tan;e that keeps all info on who is following which blogs. 表3 = blogFollowers(使用FollowingBlogID作为主ID),这是棕褐色,E,保持对谁是以下其中博客所有信息。 - it has FollowingBlogID, TheBlogID, ImFollowing, FollowOrNot - FollowOrNot has to be either 0 or 1 - 0 equals Following 1 equals Not Following)
-它具有followingBlogID,TheBlogID,ImFollowing,FollowOrNot-FollowOrNot必须为0或1-0等于关注1等于不关注)
EXPLANATION User is logged in. $LoggedInUser = id of person who is logged in when they are logged in. SESSION 说明用户已登录。$ LoggedInUser =他们登录时登录的人的ID。SESSION
A logged in user want to StartFollowing a blog. 登录的用户想要开始关注博客。 The user clicks on the StartFollowing blog link.
用户单击StartFollowing博客链接。 This sends info to be inserted into Table3.
这将发送要插入到Table3中的信息。 It creates a new row (assigns a unique id) and inserts TheBlogId (is the same as BlogID from table1) ImFollowing (inserts LoggedInUser) and FollowOrNot it inserts a 0.
它创建一个新行(分配一个唯一的ID)并插入TheBlogId(与table1中的BlogID相同)。ImFollowing(插入LoggedInUser),并且FollowOrNot插入0。
EXAMPLE 例
The Problem : So what I cannot figure out. 问题 :所以我不知道。
When logged in the user sees a list of all blogs-Blog Page (From Table2) on the page. 登录后,用户会在页面上看到所有博客的列表-博客页面(来自Table2)。 Next to each blog title there is a link that says StartFollowing (the logged in user is NOT following this blog) or StopFollowing (the logged in user IS following this blog).
每个博客标题旁边都有一个链接,该链接显示StartFollowing(登录的用户未关注此博客)或StopFollowing(登录的用户在此博客之后)。 How can I make this happen.
我该如何做到这一点。 No matter what I try I either just show the Logged in user's blogs that they already follow OR i see only the blogs that are being followed (table3).
无论我尝试什么,我要么只是向已登录用户的博客显示他们已经关注过,要么我只看到正在关注的博客(表3)。
So here is my take on it: 所以这是我的看法:
First , you said 首先 ,你说
A logged in user want to StartFollowing a blog.
登录的用户想要开始关注博客。 The user clicks on the StartFollowing blog link.
用户单击StartFollowing博客链接。 This sends info to be inserted into Table3.
这将发送要插入到Table3中的信息。 It creates a new row (assigns a unique id) and inserts TheBlogId (is the same as BlogID from table1 ) ImFollowing (inserts LoggedInUser) and FollowOrNot it inserts a 0.
它创建一个新行(分配一个唯一的ID)并插入TheBlogId(与table1中的 BlogID相同)。ImFollowing(插入LoggedInUser),并且FollowOrNot插入0。
Shouldnt it be table2 (the bold one). 它不应该是table2 (粗体的)。
Second : The table3 should have a composite primary key based on the blogId
and userId
. 第二 :table3应该具有基于
blogId
和userId
的复合主键。 Only then will it be logical as a user can follow many blogs. 只有这样,用户才能关注许多博客,这才是合乎逻辑的。
Third , to answer your question: 第三 ,回答您的问题:
No matter what I try I either just show the Logged in user's blogs that they already follow OR i see only the blogs that are being followed (table3).
无论我尝试什么,我要么只是向已登录用户的博客显示他们已经关注过,要么我只看到正在关注的博客(表3)。
This is the general pseudocode: 这是一般的伪代码:
foreach( $allRowsOfTable2 as $row) { //each row for those blogs being displayed on page.
blogId = $row['blogId'];
blogAuthorId = $row['blogAuthorId'];
if( blogAuthorId != currentLoggedInUserID ) { //obviously why do you need to follow yourself right? :P
$sql = "SELECT * from table3 WHERE blogId=`$blogId` AND userId='$currentLoggedInUserID';";
$result = do_a_database_operation_here;
if(count($result) >0 ) { //yes he is following the blog
//display the "unfollow" link
}
else {
//display the "follow" link
}
}
}
PS: You can do this in a better way if you can use joins in sql. PS:如果可以在sql中使用联接,则可以以更好的方式执行此操作。 If you provide the codes to your table structure a query can be shown.
如果将代码提供给表结构,则可以显示查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.