Associate data to users - php

I've got a database in which there is some data like:
Data
id|number|page|
1 |------|----|
2 |------|----|
3 |------|----|
Let's try to explain what i want to do: I want that, for example, user "A" can access only Data 1 and 2, and NOT 3.
So, technically, I want to store some information in the user table that tells me which Data he can actually download.

Creating a MANY to MANY relation in another table would be ideal.
association
user_id, data_id
data
id, number, page
user
id

Related

Record visible just for selected users

I want to create a form where the logged author can fill a textarea and then select 1 or more system users from a checkbox
So when every user logs in he Will see that record only if he has been selected in the checkbox before.
How can i do it?
Must i save values of the checkbox in a single record and then split to set the correct data set?
And then how can i show the whole records to the current user as i want him to see a list of records where he is the author or where he is among the users allowed to set that very same record
I know this can be a generic request but the matter is i dont understand how i could do it, so the answer i look for is not "give me the code" but"explain me the main steps i must follow", someone can give me some hints?
How about creating a table named "record" with the following structure:
id author_id allowed_user_id text
Now, when the author with id 1 selects two users with ids 2 and 3, you insert two rows into that table, one for each allowed user
id author_id allowed_user_id text
1 1 2 just a test
2 1 3 just a test
To show the current user the records he authored
SELECT * FROM record WHERE author_id = current_user_id
and records he's allowed
SELECT * FROM record WHERE allowed_user_id = current_user_id

How can I show results in a select input, based on values shown in a seperate table?

I have three tables, and I'm just looking for a way to make this work.
tbl_campaigns has the columns "id" and "campaign". This one is fairly straight forward, it's just campaign names with an ID number that is auto-incremented so they have unique IDs.
tbl_users has an "id" column so each user has a unique ID number, standard stuff.
tbl_permissions creates a new row whenever a new user is created. This means its "id" column has unique ID values that match to the ID of a user in 'tbl_users'. The columns have been named to match the ID value of a campaign each time a new one is created, for example, the column "campaign_1" is relevant to the campaign in 'tbl_campaigns' with the ID of 1. The idea is this table data is filled with either 1's or 0's.
If a row with the ID of 1 has the number 1 for the column "campaign_1", then the user with the ID of 1 is approved for the campaign with the ID of 1 in the campaign table. If it were 0 then they're not approved for it. The same logic applies for columns "campaign_2", "campaign_3" etc..
Anyways, the issue I'm having is displaying this information on a front-end, as I only want the user to be able to see the campaigns they are approved to run in a drop-down list. When the user is logged in it stores their User ID in a session, I'm not sure if there's a way around it with this method.
Is there any way to get around this? Please note I've done this in procedural PHP as I'm still in my early days, so if anyone has a solution along these lines it would be much appreciated. Sorry if it's a little confusing. I am aware it's a bit ham-fisted, but I just want it to work first.
I believe that your schema needs to be improved, as the table structure should not have to change every time that you add a new campaign.
keep tables tbl_campaigns and tbl_users as they are
create table tbl_permissions with 4 fields (id, user_id, campaign_id and permission)
To check if a user has permission use a query like this:
SELECT permission FROM tbl_permissions WHERE user_id = ? AND campaign_id = ?
So, every time you create a campaign add a corresponding record to the tbl_permissions table. No need to add a new column.
I think the best practice to do this is as follows:
- Create HTML to show to the user(if you don't have it, let me know so i can work on one you can use)
- Create JS archive that will be in charge of calling PHP file and show the result in your HTML(if you don't know how to make it let me know so i can help you)
- Create PHP file, this is going to be in charge of consulting your data base and give the result disired for your select (if you don't know how to make it, let me know)
It is pretty easy to make this work, let me know if you need more help.

Track Profile Views by Other Profiles

I have a database of users, each with a unique id, and I would like to keep track of which users profile each user views:
For instance if User 56 views the profile of User 104, what's the best/recommended way of storing that in MySQL?
I don't want to count the views, I want to know who views who.
Is a toxi solution best for this? It seems to me that if I'm adding a new row into a table for each new view, that table is going to get pretty big, pretty quickly?
Can someone point me in the right direction?
if you would like to store just user id of visiting profile you can store by coma separated id in one row you just need to append user id in existing string by coma
for example if profile user id is 56 then other profile_visitor_id may be like 23,42,56,23
so table will be like
id, user_id, profile_visitor_id
i think this is good way to manage big data in small way.

How to structure a huge mysql database with many profiles

The question is how to structure a huge mysql database with for example 10.000 profiles.
Example1
1 Database
3 Database fields (field_profile, field_images, field_posts etc.)
field_profile 10.000 profiles with an id and the rest of the information.
field_images 150.000 images related to the id in database_profile.
field_images 350.000 posts related to the id in database_profile.
Slow searching but when i want to change something really easy.
Example2
1 Database
30.000 Database fields (field_profile_profile1, field_images_profile1, field_posts_profile1, field_profile_profile2, field_images_profile2, field_posts_profile2 etc..
field_profile_profile1 1 profile with information.
field_images_profile1 50
field_posts_profile1 3500
Fast searching but when i want to change something really difficult?!
Which example is the best or is there a better option?
I would go for normalization, according that your number of records are not even close to huge:
[table_profile]
profile_id
[table_image]
image_id
profile_id
[table_post]
post_id
profile_id
You should simply come up with table that if Field profile and field image is same for different post then
table profile
field_id field_profile field_image
table post
field_id field_post
Second option if field_profile , field_image is different every time then come up with single table .
table field
field profile field_image field_post
Try to store repeating data in different table and use id's whenever needed , it will decrease your data storage and increase speed of database access .

Private Messaging System with 3 different users tables

I have three different users tables, and I would like to know what is the best way to create a private messaging system for them to communicate.
I tried to create it with a simple db scheme:
id (int)
from (int)
to (int)
subject (varchar)
message (text)
timestamp (timestamp)
read (bool)
deleted_to (bool)
deleted_from (bool)
But complications instantly arised because of the three users tables, where user IDs in table A can have ID = 1 and another user in table B can have ID = 2
Any ideas on how to create a better DB scheme? Thanks
use "model" column with varchar type to save which user model the user comes from.
this way you can have several entries with "2" for example. model + user_id then has to be unique.
e.g.:
User1 3
User1 5
User2 3
...
A hackish solution would be to create a hash for every user and store it in one table to uniquely identify any users. Then, using the hashes, figure out which message was sent to which user.
It most certainly is not a great idea, because it generates unnecessary overhead sending several queries to the database. If possible, create a table to hold all users and using one-to-one relationships create tables for entity specific fields. Your authentication methods would be stored in the user table. Post authentication your system would know, which tables to join together for the required data as described in that single user table.
You could add two fields in your table to store the "table" related to the from and to.
Dunno what kind of db you are using but this problem could be solved easily with table inheritance.

Categories