从一个表中提取数据并对照另一张表进行检查,然后显示匹配项

发布于 2024-12-04 01:03:25 字数 1182 浏览 5 评论 0原文

情况是这样的。我已经加入了网站其他部分的所有地方,一切正常,只是这个问题让我发疯。这可能很简单,我只是错过了一些东西。

这是一个用户可以登录并关注彼此博客的系统。

数据库 每个表都有不同数量的列,并且没有一个列使用相同的名称。

Table1 = userdetails(使用 ID 作为主要 ID)

Table2 = blogs(使用 BlogID 作为主要 ID) - 它具有 BlogID、WriterID blogtitle、blogtext、blogdate 等,作为行

Table3 = blogFollowers(使用 FollowBlogID 作为主要 ID),这是保存有关谁正在关注哪些博客的所有信息的 tan;e。 - 它有FollowingBlogID、TheBlogID、ImFollowing、FollowOrNot - FollowOrNot 必须为 0 或 1 - 0 等于关注 1 等于不关注)

解释 用户已登录。 $LoggedInUser = 登录时登录者的 ID。 SESSION

已登录的用户想要开始关注博客。用户单击 StartFollowing 博客链接。这会发送要插入到 Table3 中的信息。它创建一个新行(分配唯一的 id)并插入 TheBlogId(与表 1 中的 BlogID 相同)ImFollowing(插入 LoggedInUser)和 FollowOrNot 它插入 0。

示例

  1. User1 已登录并创建 BlogA 。
  2. User2 登录并在博客页面顶部看到它。
  3. User2 想要开始关注此博客。
  4. 用户 2 单击该链接。它将上面的信息插入到表 3 中。
  5. 然后 User2 刷新页面。现在而不是说 StartFollowing 它说停止关注。

问题:所以我无法弄清楚。

登录后,用户会在页面上看到所有博客的列表 - 博客页面(来自表 2)。每个博客标题旁边都有一个链接,显示 StartFollowing(登录用户未关注此博客)或 StopFollowing(登录用户正在关注此博客)。我怎样才能做到这一点。无论我尝试什么,我要么只显示他们已经关注的已登录用户的博客,要么只看到正在关注的博客(表3)。

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)

Table2 = blogs (uses BlogID as Primary ID)
- it has BlogID, WriterID blogtitle, blogtext, blogdate, etc. as rows

Table3 = blogFollowers (uses FollowingBlogID as primary ID) this is the tan;e that keeps all info on who is following which blogs.
- it has FollowingBlogID, TheBlogID, ImFollowing, FollowOrNot
- FollowOrNot has to be either 0 or 1 - 0 equals Following 1 equals Not Following)

EXPLANATION
User is logged in. $LoggedInUser = id of person who is logged in when they are logged in. SESSION

A logged in user want to StartFollowing a blog. The user clicks on the StartFollowing blog link. This sends info to be inserted into 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.

EXAMPLE

  1. User1 is logged in and creates BlogA.
  2. User2 logs in and sees it at the top of the Blogs page.
  3. User2 wants to StartFollowing this blog.
  4. User2 clicks the link. It inserts the info from above into table 3.
  5. User2 then refreshes the page. Now instead of saying StartFollowing
    it says StopFollowing.

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. 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). 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-12-11 01:03:25

所以这是我的看法:

首先,你说

登录用户想要开始关注博客。用户点击
开始关注博客链接。这会发送要插入到 Table3 中的信息。
它创建一个新行(分配一个唯一的 id)并插入 TheBlogId(是
table1 中的 BlogID 相同)ImFollowing(插入 LoggedInUser)和
FollowOrNot 会插入 0。

它不应该是 table2 (粗体的)。

第二:table3 应该有一个基于 blogIduserId 的复合主键。只有这样,用户才能关注许多博客,这才是合乎逻辑的。

第三,回答你的问题:

无论我尝试什么,我要么只显示 Logged
在他们已经关注的用户博客中或者我只看到以下博客
正在被关注(表3)。

这是一般的伪代码:

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:如果您可以在 sql 中使用联接,您可以以更好的方式做到这一点。如果您向表结构提供代码,则可以显示查询。

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. This sends info to be inserted into 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.

Shouldnt it be table2 (the bold one).

Second: The table3 should have a composite primary key based on the blogId and 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).

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. If you provide the codes to your table structure a query can be shown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文