Yesterday we received another assignment. This is a group project that requires us to make a "basic" social networking site. If you'd like, you can look at the hand-out here: https://lh.rs/Z6X8Hx8xaUfS
I thought I'd make a thread about it to get some activity going in this section, and so you guys could test some stuff and give some feedback once I get started.
Results 1 to 27 of 27
Thread: My web programming class
- 15 Mar. 2013 03:10am #1
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
My web programming class
- 19 Mar. 2013 04:08am #2
If you need any help with anything, just message me on IM or post in this forum and link me on IM, and I'll contribute input. I mean, not about aesthetics or functional concepts of social networking, but any of the programming aspects you may have questions about.
- 20 Mar. 2013 08:31pm #3
Sounds fun. Is this a college course?
How much time do you have to complete the assignment? I'm interested in seeing how you guys approach the assignment.
- 20 Mar. 2013 08:36pm #4
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 20 Mar. 2013 08:44pm #5
- 21 Mar. 2013 02:14pm #6
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.37
6 degrees of separation!
- 21 Mar. 2013 03:11pm #7
>social networking site
>3 weeks
I feel bad for your classmates who think that they can make a Facebook competitor. Not even in these 3 weeks, but just ever.
- 21 Mar. 2013 08:20pm #8
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 24 Mar. 2013 05:13pm #9
I thought the assignment stated that it would be a watered down version of a social network. Why would they even think they could make a FB competitor in 3 weeks? o-o
If you stick to the assignment, you should have enough time to finish it just by putting like 5 hours a week per person. To be honest, the hardest part in my opinion is the graphics. The graphics always take me forever. D:
- 25 Mar. 2013 08:16pm #10
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 25 Mar. 2013 09:49pm #11
That's a terrible idea, and flies in the face of database normalisation principles :p You could do something like this:
So friends is a separate table with the following properties:
- User Id: fk to userId in Users table (the user who initiated the friends request)
- recipientId: fk to userId in Users table (the user who is the recipient of the friends request)
- dateAdded: date the friend request was added
- status: status of the friends request (i.e. 0 = pending, 1 = accepted, 2 = declined)
There's therefore a one:many relationship between the two tables, and it's fairly easy and manageable to retrieve a users friends
- 25 Mar. 2013 10:58pm #12
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
Code:function send_friend_req($sender,$target){ $link = mysqli_connect('localhost', 'root','','social'); $date = date("Y-m-d"); $query = mysqli_prepare($link, "INSERT INTO `friends` (`id`, `recipientId`, `dateAdded`, `status`) VALUES (?,?,?,'0')"); mysqli_stmt_bind_param($query,'iis', $sender, $target, $date); mysqli_stmt_execute($query); mysqli_stmt_close($query); }
- 25 Mar. 2013 11:17pm #13
Yea, although it's a fairly bad idea to establish a new connection in every function. Does that mean you're going to establish a connection for every CRUD operation? Database connections are exhaustive, and they can only handle so many simultaneous connections. You're better off using the one connection throughout your application.
Also, do you know anything about constraints? If you don't, it's not a huge deal, but if you do you may want to set them up with your schema. i.e. the foreign key constraints, userId and recipientId should be a composite key, etc. Again, not absolutely required (especially for a small web systems class), but if you were building the application properly you'd want to ensure the database is set up properly.
- 25 Mar. 2013 11:50pm #14
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 25 Mar. 2013 11:58pm #15
OK, that's not really an issue I guess. Have you had any exposure to JOINs? Your query will have to join the two tables to retrieve the necessary information.
- 26 Mar. 2013 12:21am #16
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 26 Mar. 2013 12:27am #17
So, so say you wanted to retrieve the friends of a user with id 1. Obviously if you were to use something like:
Code:SELECT recipient FROM `friends` WHERE `user_id` = 1 AND STATUS = 1;
You would get a list of user Ids of all of his accepted friends. However, say you wanted a list of their user IDs, and also their name. It may be tempting to use PHP to iterate through that list of user ids, and perform separate queries to retrieve those details from the users table. However, thats evidently quite inefficient (just 30 friends will require 30 extra queries). It's simpler to just use a join, which basically joins the two tables (with a foreign key relationship) and retrieves the required information. So something like this:
Code:SELECT friends.recipient, users.first_name, users.last_name FROM `friends` LEFT JOINT `users` ON friends.recipient = users.user_id WHERE friends.user_id = 1 AND friends.status = 1
That's just a single query, but will return their friends user id, first and last name.
- 26 Mar. 2013 12:53am #18
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 26 Mar. 2013 01:02am #19
You're absolutely right :p That was poor thinking on my part xD If you wanted a complete list of the user's friends, you could either alter the query (essentially merge two queries, one on the recipient, one on the sender), or redesign your database schema for that table.
edit: for instance, upon acceptance of a friends request, you may want to make another insert query on to that table, but this time swap the user id and recipient, in that way the above query would still stand. Though you could argue this may lead to data redundancy, and when you removed a friend you'd have to remove both entries. However, the choice is yours.
- 26 Mar. 2013 11:50pm #20
That's not correct. You don't need 30 queries to get the list of 30 friends. You just need 2 queries.
That's what the 'IN' operator is for.
get the list of user ids:
SELECT recipientId FROM friends where userId = ?;
get the list of users with the data from the previous call:
SELECT *
FROM users
WHERE userId IN (id1, id2, id3);
Merging data on the PHP side is a lot more efficient than having the DB do the work. JOINs aren't cheap, and they only work when you have database tables on the same server. Most big sites like Facebook, and Google have 92830492834 web servers, but a _lot_ less DB servers. Doing the work on the web site is a lot easier to scale.
- 26 Mar. 2013 11:52pm #21
Also, 3 weeks seems like a pretty tight deadline for all that functionality. That must be some pretty bare bones UI.
- 27 Mar. 2013 12:01am #22
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
- 27 Mar. 2013 12:03am #23
I hated doing work in groups. Someone has to keep on top of things for everyone, and make sure that they're doing what they're supposed to. It's so much easier to do things in 2s, or by yourself. I guess I've never been much of a team player.
- 27 Mar. 2013 12:11am #24
I was under the impression that provided the column in which you were joining on was UNIQUE, JOIN and IN would essentially resolve to the same execution plan in most database technology.
Also, in your query, it would surely suffice to write it as one query:
Code:SELECT * FROM users WHERE userId IN (SELECT recipientId FROM friends where userId = ?);
- 27 Mar. 2013 05:03pm #25
Yes, it can be written with a subquery. But in some cases, that can be slower. In this assignment though, either way will work. I doubt the teacher expects anyone to be an SQL expert.
When it really counts, you can always add 'EXPLAIN' to the beginning of the query and see what the DB says about performance. There are times when the DB has to create temporary tables, and an explain will show that, and that's what you want to avoid. The method to use (join, in, subquery) will depend on the query and what executes faster.
At work, we have DBs spread out across a lot of servers, and the data is usually not on the same server. So we rarely use JOINs.
- 27 Mar. 2013 11:15pm #26
Yea, that's fair enough. If OP really was concerned with scalability though, there's any number of optimisations you can make, one of the most important of which is the database technology itself. I suppose a positive for MySQL is it is open source, and can be modified to suit different requirements, however it was never really built to be used across hundreds/thousands of servers. Have you had to make any actual changes to it at work?
@stapled: out of curiosity, what language are you using to build the application?
- 28 Mar. 2013 12:14am #27
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96