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.
Related
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
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.
I am trying to understand the database design for an e-commerce site. I am having trouble understanding on what to do in the following situation. Say the user creates an account for the first time, and makes an order. I can write php code which will add the user_id (primary-key), first name & last name. But what if I want to add the user_id in the orders table (user_id in the orders tables is a foreign key). How do I get the value, as the user_id in the customers table is auto incrementing?
Are you using two tables one for storing user details and other for placing orders ?(which is better) If so my recommendation would be to use username (not first name or last name) as primary key and store the username as SESSION variable for each login and for each order add the username in order table along with orders details
There are couple of ways to do that, you can access the newly generated user_id by LAST_INSERT_ID() and use it in subsequent transaction, or you can store it in session and use it later when inserting in Orders table.
I have a website where people can make posts and follow other users. I have a sidebar that has a value that keeps track of the number of posts that have been posted since your last visit.
I'm stuck thinking of how I should handle this. Should I create an entirely new table in the database called notifications that would hold the user's id and the number of posts since last visit, should I just add a column in the existing user table for this value, or should I use an entirely different method?
Thanks.
First of all: Think, which object this is a property of. In your case, the count will differ from user to user, so we might assume, it is a user property.
We could hang it on the last login, but this would give us a wrong count, if the user is logged in for a long period (The user doesn't want to know the count since his last login, but since his last activity!).
So the easiest way could be to add a field to the users table, that holds the last post ID - We just SELECT MAX(id) FROM posts and update users.lastSeenPost with the result on every user action. We can then display MAX(post.id)-users.lastSeenPost as the new post count.
Every post has a date recording when it was made.
Every user will have a date keeping track of when he/she logged in the last time.
By the following SQL statement you could ask the database to return the number of posts since the user logged in last:
SELECT COUNT(*) FROM `posts` WHERE `posts.post_date` > `user.lastlogin_date`
I suggest that you will create a cookie ($_COOKIE['lastPostId']) in each customer webbrowser with the LAST ID of your posts, and, when the user return, you will read $_COOKIE['lastPostId'] and query your database as SELECT * FROM posts WHERE id>lastPostId
I want to create a MySQL database for a project in which users can come and make comments on other profile. Every profile has a unique id to identify it, now when a user comes and makes comment on other profile I'll need to store the user id of the person who made the comment and the person on whose profile the comment was made, along with that I'll need to store the comment in the database.
As many users can make comments on a single profile, I'll need to store all the comments and the users who made them on a single profile. For this how many tables should I create and how many columns should they have? For this I'm thinking about creating a table for named user_comments and that has column user_id, commenter_id (all the commenter who commented their id separated by comma), comments (then all the user comments separated by comma).
Will this work?
For this I'm thinking about creating a table for named user_comments
and that has column user_id ,commenter_id(all the commenter who
commented their id seprated by comma) ,comments(then All the user
comments seprated by comma)
God no! You are almost there:
Table comments
id INT AUTO_INCREMENT
recipient_id
sender_id
message TEXT
[ sent DATETIME ]
[ other meta data ]
Store one message in message. Create one row per message. Never store several records in the same field separated by anything.
I'd have a profile_comment table:
id, text, profile_user_id, commenter_user_id, created_at
And a user table:
id, name, email
You can see here that the first table has two foreign keys to the user table - one points to the owner of the profile, and the other points to the owner of the comment. You can sort them in order of created_at to list them as you would on a blog, either in forward or reverse order.
Now, when you are rendering a profile page, you can get the profile id from your query string:
$profileId = isset( $_GET['profile_id'] ) ? $_GET['profile_id'] : null;
From there, you can add it into a SQL query:
SELECT * FROM profile_comment
WHERE profile_user_id = :profile_id
ORDER BY created_at
The colon mark here is a placeholder you can use with a parameterised query, which helps protect against SQL injection. However, you can build the statement as a string if you are careful to untaint any user input you insert into it.