Creating groups like facebook

Ask about a PHP problem here.
Post Reply
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

Creating groups like facebook

Post by marytan »

Hi, can anyone help me with this one? I'm trying to make a feature in my where in users can create groups. What is the best database structure and can someone help me by writing necessary queries? I'm not good when it comes to getting information and using functions :(
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Creating groups like facebook

Post by Temor »

Have you tried anything yourself?
Take the first step and try this yourself and I'll help from there.
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

Re: Creating groups like facebook

Post by marytan »

I already made a script for creating a group. A form that will submit the group's name, ID and the creator but that's about it.

I have a table called "groups" where I am storing group_id, group_name, and created_by. Another table for group_members: id, group_id, member, and subscribed(to check whether the user is a member).

I can't fully understand the way how foreign keys work but I'm eager to learn. I usually learn after someone does it for me that's why I like betterphp's tutorials. :)

I hope you can help me create this one! Thanks! ;)
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Creating groups like facebook

Post by Temor »

Foreign keys can be a bit of a pain if you're not used to them. It's really fun playing with it though :)

I'm not really sure what subscribed is supposed to be doing. Do you care to elaborate on what it's function is?

I'm in a bit of a hurry, so I will rush through this. If you need help coding this out I'll be able to do that later tonight.

What you need to do is:
For each member of a group, you need to create an entry in group_members.
Each entry has these values associated with it:
The group ID.
The user ID or username.

To add a member to a group, you need to run a query like this:
[syntax=text]INSERT INTO `group_members` (`group_id`,`user_id`) VALUES('ID Of the group to join','ID of the user joining the group')[/syntax]

And to fetch all members of a specific group, you do a SELECT statement like this:
[syntax=text]SELECT FROM `group_members` WHERE `group_id` = Group ID.[/syntax]


Sorry for the poor explanation. I'm in a rush as previously stated.
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

Re: Creating groups like facebook

Post by marytan »

I don't mind as long as you're ok with me asking for huge favor because I find this problem a too complex for me. Other forums are just being rude to me and kept ranting. :( Thanks for being friendly! :D

The `subscribed` row will be used to check if the user is a member. Just like activating accounts when you register. If the subscribed = 1, let the user use the functionality in the group, else, display group description.

For the meantime, I'll keep watching sql tutorials. If you can code it for me I'd be grateful forever! :lol: Thanks!!!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Creating groups like facebook

Post by Temor »

Alright, I see.
This forum is not like others. We will not simply provide finished code, but rather guide you through the process. That's the only way you'll learn, and this is a forum for learning PHP, not for full-fledged PHP-professionals. I'm still learning myself, and helping others is a great way for me to solve problems I usually do not encounter.
Win / Win situation.

If you post the code you have currently I will take a look at it and write a "guide" on how to proceed :)
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

Re: Creating groups like facebook

Post by marytan »

[syntax=php]<?php
$group = @$_POST['group_name'];
if ($group != "") {

$group_name = $_POST['group_name'];
$created_by = $user_data['username'];

$sqlCommand = "INSERT INTO `group` VALUES('', '{$group_name}','{$created_by}')";
$query = mysql_query($sqlCommand) or die (mysql_error());
header ('Location:attend.php');}
?>
<form action="" method="POST">
Group:<input type="text" name="group_name"><br>
<input type="submit" class="btn" name="create_group" value="Create" />
</form>
<?php
$created_by = $user_data['username'];
$getposts = mysql_query("SELECT * FROM `group` WHERE `created_by` = '{$created_by}' ORDER BY `group_id` DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
$id = $row['group_id'];
$group = $row['group_name'];
$admin = $row['created_by'];

echo "
<div class='posted_by'><a href=''>$group</a> by <a href='$admin'>$admin</a></div>
";
} ?>[/syntax]

Here's the form that I made so far. A user can create a group and it returns the name of the group in the page. That's about it for now. What do you think should I do next? :?:
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Creating groups like facebook

Post by Temor »

Half way through writing this I realized that I'm pretty much just explaining Jacek's " User Profiles " tutorial to you. If you watch it I bet you'll get an idea of how to do this even better. http://www.youtube.com/watch?v=SSpSczxobK8

I would create a group page next.
I would do it like this:

Create a function which fetches all the data associated with the group.
[syntax=php]
<?php
function fetch_group_info($gid){
$gid = (int)$gid;
$sql = "SELECT `group_name`, `created_by` FROM `groups` WHERE `group_id` = {$gid}";

// Run query and return array.
}
?>[/syntax]
And another function which checks if the user is already subscribed or not.

[syntax=php]
<?php
function is_subscribed($gid, $member){
$gid = (int)$gid;
$member = mysql_real_escape_string($member); // I'm assuming member is a varchar string ( username ) and not the id.

// Run query and see if subscribed is equal to 1. If it is, return true, otherwise, return false.
$result = mysql_query("
SELECT `subscribed`
FROM
`group_members`
WHERE
`group_id` = '{$gid}'
AND
`member` = '{$member}'
");
$subscribed = mysql_result($result, 0);

return ($subscribed == 1 ? true : false);
}
?>[/syntax]
Create a new document and call it group_info.php ( name it whatever you want ).
This is where you're gonna display all the group info, so we call the first function.
See User Profile Tutorials on how to code this page. Just swap any user info with group info. Easy as that.

Next up is the is_subscribed function. Because you wanted to display a join group button if the user was not already a member.
This one is pretty simple.
[syntax=php]<?php
if(is_subscribed($gid,$member) == true){
// Show regular page
}else{
echo "<a href='join_group.php?gid=". {$gid} ."'>Join Group</a><br />";
}
?>[/syntax]


You can use what you learn from the User Profiles tutorial to code everything you need.
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

Re: Creating groups like facebook

Post by marytan »

I'm currently doing it and I'm almost finished. can you help me design the group_member table? I can't think of a good way how I should create it with sql...
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Creating groups like facebook

Post by Temor »

Well, you need these things:
User ID
Group ID

That's it.

If you have a row with these values:

User ID Group ID
1 1
That means the user with ID 1 is in the group with ID 1.

If you want the ID's of all users in group 1, you do a statement like this:
[syntax=text]SELECT `UserID` FROM `group_members` WHERE `GroupID` = 1[/syntax]
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

Re: Creating groups like facebook

Post by marytan »

almost done. :)

How about the join function? What's the best way to implement this?

I'm trying to decide which function is better so I might create 2 options just for the purpose that I may use it some time in the future. First is a button where a user will press and send a request to the user who created the group. second is when the group is created, there will be a random string generated and other users who want to join the group must have the key string and just have to submit it to join the group. What do you think??? :?:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Creating groups like facebook

Post by jacek »

marytan wrote: What do you think??? :?:

The invite thing would be good for small groups but for large ones the password thing would be better. I would let people choose their own key though sine random strings are not very memorable.
Image
marytan
Posts: 9
Joined: Sun Sep 09, 2012 11:25 am

Re: Creating groups like facebook

Post by marytan »

I think I should store the small random code, maybe just 5 characters when the group is created. Then the members can just pass it around.

I still don't know how I should create the join function :(

Also, what are some good parameter checks for checking if the user is a member?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Creating groups like facebook

Post by jacek »

marytan wrote:I still don't know how I should create the join function :(

Think about members first, you will probably have a table called something like group_members that will have the group ID and the user ID. The fact that the user and group is in that table means the user is ion the group, then to join a group you just insert into that table.
Image
Post Reply