PHP MySQL. Linking 2 Tables columns together - php

I've got an small issue, I'm trying to make my own forum, but I am stuck at something.
I have 3 tables, 1 users with a user_level (Authentication level).
forum_section which contains all the sections with a user_level again.
But how can I link forum_section.user_level to forum_topics.user_level.
So if I define: forum_section.user_level = 4 for forum_section.section_id = 1.
For example:
Someone wants to visit the forum_section id 1 then they must have a auth level of 4.
And when they go to the topic it then again checks if the user level is 4, but I do not want to manual set the topic level, topic level must always be the same as the section level.
I've googled for this, but I can't really find a good manual for it. I guess it has something to do with: "foreign keys"?
Okay, let me explain a bit more just to be sure.
I have 3 tables
Table User
username = Wesley
userlevel < for example 4(admin).
Table section
section_id = 3
section_name = News
section_level = 3 <require level to view.
table topics
topic_id = 123
topic_name = I like candy
topic_level = Needs to be the same as section_level so if I change section_level it automatically changes this too.

You should have to manage your database by using master and transaction tables.
Your table format should look like this:
table name: user
user_id user_name
1 abc
2 xyz
3 pqr
4 new
table2 name : forum_section
forum_id user_id forum_name
1 4 abc
2 4 jkl
3 2 cbd
4 3 lmn
Now, you can JOIN these two tables and make a query as you want, like this:
SELECT forum_id FROM forum_section as fs,user as u WHERE fs.user_id = u.user_id AND fs.user_id = 4
This may help you. Please write if you need more help!

You need to either use a SQL JOIN or a NESTED SUB QUERY
The simplest implementation would be to initially establish the users access level, then filter subsequent queries based on this- so in your PHP, pass the access level to subsequent requests for content- if the query returns anything, the user can view the content, if not- they can be redirected.

Related

Combine 2 columns as 1 with other columns as well

I have this table
myid his_id
7 1
1 7
1 6
1 3
But its giving me 4 records instead of 3, i tried using mysqli "join" , "group by" but didnt work.
Im trying to develop a chat system with pure php and jquery, in d table above, user 7 chatted with user 1 and user 1 chatted with user 7 so i want to see it as just the same chat and the rest of the 2 ( user 1 chatting with user 6 and user 1 chatting with user 3)
I want it to give me just 3 records instead of 4 records, pls any help will be appreciated, thank you
Sorry im not connected to my system, im using a smart phone, forgive my bad formatted query below
$sql = mysqli_query($connectn, "select a.id, b.his_id, a.myid, a.status, a.date_replied FROM chats a JOIN chats b ON b.myid=b.myid WHERE b.his_id='$regestered_id' or b.myid='$regestered_id' GROUP BY b.his_id ORDER BY a.id DESC") ;
If you want distinct combinations of myid and his_id, you must not print id, date_replied, status, etc, but just two columns- his_id, myid.
Because if there are multiple rows with two columns with same values, it is yet another to decide which row must be selected in order to print id, status, etc.
So, just to print which two users are chatting, use myid and his_id. You can later use them for any other information.
Now, the code you wanted is-
$sql = mysqli_query($connectn, "select distinct
least(myid, his_id), greatest(myid, his_id)
from chats");
And yes, like #iblamefish mentioned, don't forget to protect your code against SQL injection.

Implementing Discussion Group System

I am going through the process of adding discussion groups to my site, currently users can post on topics then other users can reply, pretty simple.
Here is how I have set up the group tables
Disc_group
ID(int)
Name(varchar)
Description(varchar)
Created(date)
Creator(int) = user_id
invite_rule(enum 0,1) 0 = open invite, 1 = invite must be approved
Disc Users
ID(int)
User_id(int)
Group_id(int)
datejoined(date)
accepted(enum 0,1) 1 = accepted
admin(enum 0,1) 1 = admin
My question is I am trying to think of the most efficient way to implement this with the current topics, posts and tags tables.
I thought I could just add "group_id" into topics, so topics with group_id of 3 for example, only display if you are viewing the group page id=3.
I don't want any one to write code for me, I'm wanting to know what file structure would be recommended, such as
Non grouped posts are displayed in "topics.php?id=1" topics in groups displayed in "gtopics.php?id=1" for example, hopefully my issue isn't too confusing.
invite_rule(enum 0,1) 0 = open invite, 1 = invite must be approved
accepted(enum 0,1) 1 = accepted
admin(enum 0,1) 1 = admin
We can already see the above forming into a set of permissions, I highly recommend implementing a bit masking system that way you can store the user permissions in the database and perform a logical AND when you generate your authorization token.
Disc Users
ID(int)
User_id(int)
Group_id(int)
datejoined(date)
accepted(enum 0,1) 1 = accepted
admin(enum 0,1) 1 = admin
Is going to start hurting when it comes to normalization, I would propose the following structures to replace this:
DISC_GROUPS
GroupName
GroupId (not needed if group names need to be unique)
DISC_USER_GROUPS
UserId
GroupId
DISC_USERS
USER_ID
NAME
PERMISSIONS
DATE_JOINED
//other information about users
//This table may be unnecessary, unless you want to add some audit information perhaps
DISC_INFORMATION (THIS IS YOUR OLD DISC_USERS table)
ID
USERID
Notice how we don't duplicate information and we rely on the USER_ID relationship if we need more details.

MySQL - Private Posts Display

So I have 3 tables: users, posts, private. In this example, lizzy created a private post 'Dating' and only wants her girlfriends to see it 'authorized_user_ids' in the private table, and herself 'user_id' in posts table.
users
user_id user_name
1 tony
2 steph
3 lizzy
4 adam
5 lara
6 alexa
posts
post_id user_id post_name private (0 is public, 1 is private to authorized users)
1 1 Coding 0
2 3 Dating 1
3 3 Show Me 0
private
private_id post_id authorized_user_ids
1 2 2,5,6
Only the private poster user_id and the authorized_user_ids should see the 'Dating' post by lizzy, but everyone should be able to see the 'Show Me' post by lizzy. Logged out users and users not authorized should not see anything. Is that the most efficient way to go about it, or should it be:
private
private_id post_id authorized_user_id
1 2 2
2 2 5
3 2 6
That's my first question. My second is what would be the best way to tackle it in php (display to authorized_user_ids) when pulling the data from a mysql db? Do we do a second query based on the 'private' flag in the posts table? If it is 1, we query in the while loop to get the 'authorized_user_ids' from the private table, and if it matches the $logged_in_id of the logged in user, then we display 'Dating'? Or should there be a join somewhere in the main query with the 'authorized _user_ids' using the 'post_id' in the private table and the posts table? Any help on how to most efficiently do this is appreciated.
First Q: I would definitely go with the second version. Storing authorized_user_id as a CSV will cause both DB read and write operations to become much less efficient as you will end up splitting the CSV values all the time in your application code and generating bad dynamic SQL statements in order to process updates. You should never have to write PHP code to help with finding the intersection of different sets of data like that, use SQL.
Second Q: Use joins, no while loops for example
SELECT DISTINCT p.post_id, p.post_name FROM users as u
JOIN posts as p on p.user_id=u.user_id
LEFT JOIN private pv on pv.post_id=p.post_id
WHERE (p.private=0) OR (authorized_user_id={$logged_in_id} and p.private=1)
The above (or very similar) will list all posts $logged_in_id is 2 but only public ones when $logged_in_id is 4 etc.

How to manage user access and user permissions

I am working on an application with PHP + MySql. In my application I have a table for users, a table for relationships (friends, following, subscribed) and a table for posts. The main actions for users are:
A user has friends
A user can make post entries
A user can see the friends entries
And finally a user can block entries viewing for specific friends
If user A is friends with user B, then user A can see all entries from user B. But user B can restrict access to only a few friends for example. Now the query is: how can I manage these permissions? I was thinking of a table that stores each user that is blocked for viewing an specific entry, but this would't be a good idea once a single user can have several friends. So, how can I solve this? Can someone show me how to start? Maybe the right terms for searching on Google or a link for something similar.
You are on the right track. You are going to want to use linked tables. You would start with a table users. Each user has an id. Then create a table users_friends. This table would consist of two ids, user_id and friend_id. The last table would be users_restricted which would also consist of two ids, user_id and restricted_id.
For example
users
user_id name
1 user1
2 user2
3 user3
users_friends
friend1_id friend2_id
1 2
2 3
This says user 1 and 2 are friends and users 2 and 3 are friends. (This assumes that if user 1 is friends with user 2 then user 2 is also friends with user 1)
users_restricted
user_id restricted_id
1 2
Now even though user 1 and user 2 are friends, user 2 is in the restricted list meaning don't allow user 2 to view user 1's entries.
You can see that tables are linked via ids and all the ids come from the users table. This can be expanded to relate to entries as well. Just refer to entries by their id.
To have users blocked for specific entries you would have the following tables.
entries
entry_id user_id ... other columns holding entry information
1 1
2 1
3 2
4 2
Now user 1 has made 2 entries (entry 1 and entry 2) and user 2 has made 2 entries (entry 3 and entry 4). Another table would hold the restrictions.
entries_restricted
entry_id restricted_user_id
1 2
This would say user 2 cannot view entry 1.
To get the entries visible to user 2 your statement would look something like this.
SELECT e.*, er.entry_id FROM entries e JOIN entries_restricted er ON e.entry_id=er.entry_id WHERE er.restricted_user_id != 2;
The statement selects all the entry information excluding entries restricted to user 2.
You can start using following tables.
The first table is users table (as Jason.Wolfsmith suggested)
users
u_user_id u_name
1 user1
2 user2
3 user3
The second table can be like this.
friends_permissions
f_user_id f_friend_id permission entries
1 2 1 entry1
2 3 0
1 3 1 entry3
This table will contain permission and name of entries that should be allow for view. 1 - restrict some entries; 0 - allow all.
In the column permission data type might be set as SET('1','0') and data type in entries NULL.
Thus, user1 don't allow to view entry1 to user2. (entry1 and entry3 are from entries table).

Grouping Users on a virtual roster

I'm trying to come up with the most simple / userful / efficient method to group 3 users together using mysql.
To set the stage:
X number of users in a list (all with int account_id's)
mini groupings need to be created on a per user basis (user 1 wants to group with 220 for instance).
Max 3 people per grouping (user 1 + user 220 + user 9123 = group full)
Need to easily find if a user is in a group or not without looking in a bunch of columns
I'm stumped about how best to create a schema for this (so I can easily query my table to see if user is in a group, or if they can be added, or check for group space availability).
Anyone have any idea? My initial thought is schema like this (but it really seems too rigid):
Schema
GROUP_ID USER1 USER2 USER3 LASTUPDATE
1 1 220 null 5/25/2011 20:00:00
2 300 2 4 5/25/2011 20:00:00
How would you do it to make something this simple very flexible and efficient. I really feel stupid for asking.
Personally I would approach this by using 3 tables.
Users Table
user_id user_name ..... last_update
Groups Table
group_id group_name ......
Users to groups Table
user_to_group_id user_id group_id
This forms a many to many relationship by linking through the "Users to groups" table, obviously you can have more that 3 users linked to one group so you will have to make your PHP logic check for this when adding a new user to a group.
You can simply use SQL joins to retrieve all the data required and filter the results in your PHP code.
I hope this helps
Kind regards
Garry

Categories