简体   繁体   English

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

[英]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

  1. User1 is logged in and creates BlogA. User1登录并创建BlogA。
  2. User2 logs in and sees it at the top of the Blogs page. User2登录并在“博客”页面的顶部看到它。
  3. User2 wants to StartFollowing this blog. User2想要开始关注此博客。
  4. User2 clicks the link. User2单击链接。 It inserts the info from above into table 3. 它将信息从上方插入表3中。
  5. User2 then refreshes the page. 然后,User2刷新页面。 Now instead of saying StartFollowing it says StopFollowing. 现在,不用说StartFollowing而是说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. 登录后,用户会在页面上看到所有博客的列表-博客页面(来自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应该具有基于blogIduserId的复合主键。 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.

相关问题 从一个表中选择数据,检查并从第二个表中提取值 - select data from one table, check against and pull values from second table 如何将数据从一个表拉到另一个表 - How to pull data from one table into another 通过 id 从一个表中获取数据并显示在不同的不同页面中 - fetch data by id from one table and show in different defferent pages 拉表表数据preg_match - Pull Table Table Data preg_match MySQL从一个表中选择列以与另一个表匹配以拉ID - Mysql select column from one table to match up with another table to pull id 如何在Laravel中显示来自不同表的数据? - How to show data from different table in laravel? PHP / MySQL 如何从一个表中获取值并检查另一个表 - PHP / MySQL How to get values from one table and check against another table 输入数据从“ A”表中获取数据,然后与“ B”表中的数据进行核对,然后在数据不在“ B”表中时显示数据 - input data bring data from 'A' table then check with 'B' table with the data then show the data when the data not in 'B' table 如何从不同的表中提取数据并使用php将其显示在同一页面的不同html表中? - how to pull data from different table and display it in different html table on a same page using php? 如果没有从一个表中选择,请检查任一表上是否存在数据 - Check if data exists on either table if not select from one table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM