Record visible just for selected users - php

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

Related

search in 2 tables if field has value every hour

i have 2 tables one name unete_info and unete_confirmation. unete info has all basic information for a registry and unete confirmation has a field name joind_id which is generated when user successfully registered.
join_id is located in both tables as well name last_name and email, email is the unique value to validate both tables. i need to check whithin the tables every hour to see if the user the register successfully if not get a notificacion i have a user pending.
meaning my query needs to check within both tables and if user has join id empty in both tables after an hour send a notfication. but so far i need first how to get the user that dont have join_id, so far i came up with this.
SELECT unete_info.name,unete_info.last_name,unete_info.age,unete_info.number,unete_info.language,unete_info.submitted,unete_confirmation.join_id
FROM unete_info
INNER JOIN unete_confirmation ON unete_info.email=unete_confirmation.email
im missing how to check every hour and how to get users that join_id is null(empty)

How to get the RECENT records first?

MySQL code: jsfiddle.net/HBps8/
I have a table called "Likers", in that table, there is "ID, User FB ID, user FB name, and user Access Token".
At the moment, there are 2487 records in the "Likers" table.
(ID is the number of user, if the user submitted his access token when the table was EMPTIED; his ID will be 1, if another user submits his access token after the first user; his id will be 2 and so on and on)
The problem I am facing is that when the HTML form is submitted to get the IDs; it gets the OLDEST ids first (ID #1, #2, #3, #4, etc...)
What I want is that when the form is submitted, the RECENT ID's should be first (ID #2487, #2486, #2485, #2484, etc...)
Is it possible to do something like that in MySQL database? I am very new to the databases codes and I am trying my best to learn :)
Thanks in advance.
In your MySQL query you can use ORDER BY
For example
SELECT * FROM `Likers` ORDER BY `ID` DESC
you can sort the result by using ORDER BY, e.g. ORDER BY ID DESC. But it is not recommended to determine the latest entries by the ID, you should add a additional field like creationDate and sort by this one.

add a field to each record that shows when and how many people visited it?

I have a mysql table and in that is individual records. What I'd like to do is be able to have a field for each record that shows how many people have visited each record.
For example:
Jame's Record - Visited 10 times
Joe's Record - Visited 3 times
Is this possible at all?
Solution 1:
Add a field in user row named view_count and on every view update this field with +1 using
UPDATE table SET field = field + 1 WHERE [...]
Solution 2:
Create a lable view_log
Fields id,table,row_id
And on every view function insert a new row into this table with table and row id.
And when u wana get count just run
Select count(I'd) from view_log where table = 'users' and row_I'd = 'x'
Then u will get counts :)
If you want to monitor update,insert, delete then u can use mysql trigger to do it.
It is possible, You can simply do this using session.Make a view field in table that record the no of times record has been viewed.
Make session for new user's ,Whenever new user is on that user's record page increment that view field by 1 and if same user is visiting that record destroy the session.

SQL boolean relationship

I have a table dislikes which contains two columns, idone and idtwo.
These are unique ids' from users, for example:
| idone | idtwo |
-----------------
| 5 | 4 |
This means that user with id=5 does not like user with id=4. What I have in PHP is an array containing the ids' of all the users that the current user has selected as not liking them.
So say dislikes={1,2,3}, this means that the current user does not like user 1,2, or 3. There is an unknown number of users in the database.
So if user 1 chooses to dislike user 2 and user 3 (this is done via HTML dropdown), I pass dislike={2,3} to a PHP page which processes this data.
I want the PHP page to then add entries (1,2) and (1,3). Here is the first problem, how can I make sure only to add unique entries?
Also say that user 1 changes the fact that he dislikes user 2. Then I pass dislike={3} to the php page and must somehow remove all entries (1,!3), i.e. all entries in which user 1 dislikes anyone except user 3. How can I achieve this? Or is there a better way?
Since you're using MySQL the easiest thing is probably to use REPLACE INTO instead of INSERT with a primary key or unique index on the pair of columns (idone, idtwo).
Alternatively, on update, you can run a transaction that does any one of:
Remove existing rows for this user, add all rows, commit
Select existing rows, remove the rows from your local set that you would duplicate, add only new rows, commit

How to make a select with 3 tables?

I have 2 tables: tbImages, tbUsers
tbImages has lots of paths of images with unique ids
tbUsers has lots of users with unique ids...
I will code a page that make a delivery of images...This page will show all images ordering by id... The admin will select 7 images to send it to some user by email... I will have to register when and how many times some user has received that image (a 3rd table maybe)...
Here is the problem... I'll have to ORDER images by date of register.. Images already sent have to appear at end of that delivery page...
What is the better way to do this? tbImages has lots of columns one for each user? A 3rd table (tbDelivered) one row each user with column 'images received'? ?How to make this SELECT to show already sent images at end?
A 3rd table relating your imageID to a userID (relational table). Then you can use JOIN statements in your query. For example:
relational_table
imageID
userID
sendCount
Query could be:
SELECT sendCount
FROM relational_table
WHERE imageID = $imageID
AND userID = $userID
That would give you back the number of times that image has been sent to that user.
You will have to create a 3rd table that will contain:
The image ID
The user ID
The quantity
And you will make a select statement that will look like this:
SELECT tbImages.imageID, tbDelivered.quantity
FROM tbImages, tbDelivered
WHERE tbImages.imageID = tbDelivered.imageID;
To show the ones that have not already been delivered you will have to add at the end of it:
AND quantity = 0;
I hope this helps

Categories