Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am creating an app similar to Instagram. I have a photos table with a likers columns, showing which user_ids have liked a photo. For example, if user_id 1 and 5 like photo_id 51, then photo_id 51's likers column will show 1 5. As seen, the user_ids are separated by spaces. Now, like Instagram, I want to be able to show which username's are liking a specific photo. For this, I need to be able to get all the user_ids from the likers column separately. Can anyone help me with how to do this, or suggest an alternative way?
You're approaching this the wrong way. You almost certainly don't want to store multiple pieces of data in one field. You want to have something like this:
Table: User
- id
- blah blah
Table: Photo
- id
- blah blah
Table: Like
- user_id
- photo_id
This will allow you write very flexible queries, including what you described:
SELECT u.id
FROM User AS u
JOIN Like AS li ON li.user_id = u.id
WHERE li.photo_id = $current_photo_id
This is a topic known as database normalization. Please read more about it if you want your application to succeed.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi guys I have Google to get answer for the above question but I got the answer for displaying the plain bar graph one but not stacked bar graph.
My problem is I have to display the stacked bar graph based on the number of count of different categories for example the number of login to the site, reset password, chats etc.And I have to use plugin for displaying of graph.
I would request you guys to please suggest me some thing on this.As I'm new to php.
Thanks in advance guys
I think I understand the question (a good one)
SELECT category, COUNT(category)
FROM users
GROUP BY category
ORDER BY category
This displays the total number of hits for each category. In javascript I use AJAX call to execute the php - mysql, then return the array of counts back to the javascript to display the stack graph.
I executed this code on my system, just substitute "where user_id = 36" for "where date = 31032015" (whatever date format), and change the table "users" to your table name.
$template2=mysql_query("SELECT category, COUNT(category) FROM users where user_id = 36
GROUP BY category ORDER BY category ");
while($template=mysql_fetch_array($template2)){
echo "<tr><td>{$template['COUNT(category)']} {$template['category']}</td></tr>";
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new in Laravel, I want to have like button on my website
My Post Table:
post_id | body | created_at
My like table:
like_id | user_id | post_id | timestamp
Questions:
1-How to count likes in Laravel?
2-Is my database structure true or not?
3-I am going to have reputation (Like stackoverflow), Should I save that republication in one field in user table or it has different structure?
You can count results with Laravel using count() method. For
example to count all likes in the table:
// Eloquent + Query Builder
$likes = Likes::all()->count();
// Query Builder
$likes = DB::table('likes')->count();
You might want to look at the aggregation methods Laravel offers.
The structure of the two tables you showed looks fine. One thing to consider is what should happen to respective likes when a user or a post gets deleted (ON DELETE ...)
You could put reputation field in the user table, but the cleaner way
to do it is to create separate table for reputation with a foreign
key to user ID.
I would add the total like count to the post table as well. If you don't add it you need to perform sum() queries all the time, which might put too much load on your database (depending on the numver of visits on your site, etc).
You can save the reputation in the user table. You probably want to add something like your like table though. Users for sure want to know why they received some more reputation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm hoping one of you more experienced programmers might be able to shed some light on this situation:
My site allows users to create a profile and enter in their favorite musicians, all of which reside in a pre-existing database of musicians.
Based on the artists they select, I want to display to them other users who have selected the same artists.
Can anyone offer suggestions on how this could be effectively accomplished?
I'm using SQL and PHP for the back end.
You would need to create a cross-reference table between users and musicians that defines the 'likes' relationship. At a minimum it would just need to contain the user id and musician id.
CREATE TABLE user_likes_musician (
user_id INT NOT NULL,
musician_id INT NOT NULL,
PRIMARY KEY(user_id, musician_id)
);
To find users who liked a particular musician you can just join the likes table with the users table:
SELECT * FROM users
JOIN user_likes_musician
ON (users.ID = user_likes_musician.user_id
AND user_likes_musician.user_id <> [current user id]
AND user_likes_musician.musician_id = [musicians id]);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a MYSQL Db that has a PROFILE-TABLE as well as a KEYWORD-TABLE which holds profiles and the other holds keywords associated with those categories.
Profile-Table
UserID
UserName
UserDept
UserPhoto
UserKeyword > indexes KeywordName (from Keyword-Table)
UserAssociations
Keyword-Table
KeyID
KeywordName
I need to make an association with the categories/keywords.
I want to add a hidden field (UserAssociations) onto my profile form which will display a hidden association where as when you click on a category via a link on the page, it will index first those that are associated. I have written this in PHP and use MYSQLI database.
I have never created associations before needing this. What would be the easiest way to achieve this functionality?
From what I gather, you wish to associate a user profile with keyword. What you need is another table to represent the relationship, something like this:
profile_keywords ( <UserID>, <KeyID> )
Hence, if UserID 4 has associated himself with keyword ID 3, you would have an entry in profile_keywords like this:
UserID, KeyID
---------------
3 4
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i am working on site where users login to read articles, every articles is based in a category. so i want to display on the 'user home page' the category and article the last have visited/read. This counts for multiple category, so it neets to display "category 'a' read 12 of 20 articles 'click here to continue reading' " If someone can help me or send me in the right direction, any help / info is very much appreciated. Thank you
There's no special utility that can do something like that for you, if that's what you're thinking. What you will need to do is create a table in MySQL that stores a list of articles read by your users. Maybe a table with just user_id and article_id, together making up the primary key. INSERT IGNORE when a user visits an article, and when you want to get how many articles have been read, you can SELECT COUNT(*) FROM table WHERE user_id = ?
SELECT COUNT(*) FROM table WHERE user_id = ? AND article_id IN (SELECT id FROM articles WHERE category_id = ?)
The query above would let you get the number of articles that the specified user has viewed within the specified category, assuming you have it set up like this.
You also mentioned that you want to be able to select the most recent article or category the user has read. To do that, you can add a timestamp field to the new table you created. Another way is to add an auto_increment field and grab the biggest one.
Just an idea. You can create an extra table , lets call it user_read and store which user read which article.
Table user_read
user_id
article_id
I assume you know which article belongs to each category, therefor you can count how many articles in each category the user has read.