Jump to content

Recommended Posts

Hey there everyone, I'm having a problem with my code. What I had wanted to do was have my users to create groups and then choose which people in the groups to see their post. I had some radio buttons with the options of the groups and that was supposed to have only the group they chose see their post.

I removed the buttons for now because I couldn't get it to work. Please help, thank you.

One to One Relationship:

Essentially, one table that is split up. Each row is associated to exactly one row of the other table.

 

One to Many Relationship:

Table 1: Employees

Description: A list of all employees registered with the website.

Relation to Table 2: Many employees to one company

 

Table 2: Companies

Description: A list of all of the companies

Relation to Table 1: One company to many employees

 

  id  |    name    | companies_id

------------------------------------

  1  |      Max    |      1

  2  |    John    |      1

  3  |    Sarah    |      2

  4  |    David    |      1

 

  id  |    name

---------------------

  1  |  Company Inc.

  2  |  Incorp Ltd.

 

Therefore, Max, John, and David, all work for Company Inc.

Sarah works for Incorp. Ltd.

 

Many to Many Relationship:

 

Table 1: Employee

Description: A list of all employees registered with the website.

Relation to Table 2: Many employees to many companies

 

Table 2: Companies

Description: A list of all of the companies

Relation to Table 1: Many companies to many employees

 

Table 3: Employee_has_Companies

Description: Every row is a one to one relationship between an employee and a company.

 

 

 

Employee

  id  |    name

---------------------

  1  |      Max   

  2  |    John   

  3  |    Sarah   

  4  |    David

 

Companies

  id  |    name

---------------------

  1  |  Company Inc.

  2  |  Incorp Ltd.

 

Employee_has_Companies

 

Employee_id | Comanpies_id

--------------------------

    1      |      1

    2      |      1

    3      |      2

    4      |      1

    4      |      2

 

Same as before, except that David works for both Company Inc. and Incorp Ltd.

Post the relevant code that you have, and some data about the relevant database tables.

 

Table Relationships. It's an advanced concept, but you'll benefit learning it early.

I had separate pages for it.

 

edit_groups.php

<?php include_once "scripts/checkuserlog.php";
$id = $logOptions_id;


if (isset($_POST['parse_var']) && $_POST['parse_var'] == "add_to_group"){
$toGroup = $_POST['toGroup'];
$addFriend = $_POST['addFriend'];
$toGroup = preg_replace('#[^0-9]#i', '', $toGroup);
$addFriend = preg_replace('#[^0-9]#i', '', $addFriend);
$id = preg_replace('#[^0-9]#i', '', $id);
if (($toGroup != "")&&($addFriend !== "")&&($id != "")){
$sql = mysql_query("INSERT INTO user_groups (group_oid, group_mid, group_id) VALUES('$id','$addFriend','$toGroup')");
header ("Location: edit_groups.php");
}
}


if (isset($_POST['parse_var']) && $_POST['parse_var'] == "remove_from_group"){
$removeFriend = $_POST['removeFriend'];
$removeFriend = preg_replace('#[^0-9]#i', '', $removeFriend);
$id = preg_replace('#[^0-9]#i', '', $id);
if (($removeFriend !== "")&&($id !== "")){
$sql = mysql_query("DELETE FROM user_groups WHERE group_oid-'$id' AND group_mid='$removeFriend' LIMIT 1");
header ("Location: edit_groups.php");
}
}

$sql = mysql_query("SELECT friend_array FROM myMembers WHERE id='$id'");
while($row = mysql_fetch_array($sql)){
$friendArray = $row['friend_array'];
}

$in_group = array();
$sql2 = mysql_query("SELECT group_mid FROM user_groups WHERE group_oid='$id'");
while($row2 = mysql_fetch_array($sql2)){
$in_group[] = $row2['group_mid'];
}
$in_group2 = implode(", ", $in_group);

$addList = "";
if ($friendArray != ""){
$addList .= '<select name="addFriend">';
$addList .= '<option value=""></option>';
if ($in_group2 != ""){
$sql = mysql_query("SELECT id, username FROM myMembers WHERE id IN($friendArray) AND id NOT IN($in_group2)");
} else {
$sql = mysql_query("SELECT id, username FROM myMembers WHERE id IN($friendArray)");
}
while($row = mysql_fetch_array($sql)){
$addList .= '<option value="'.$row["id"].'">'.$row["username"].'</option>';
}
$addList .= '</select>';
}

$remList = "";
if (($friendArray != "") && ($in_group2 !== "")) {
$remList .= '<select name="removeFriend">';
$remList .= '<option value=""></option>';
$remsql = mysql_query("SELECT id, username FROM myMembers WHERE id IN($friendArray) AND id IN($in_group2)");
while($remrow = mysql_fetch_array($remsql)){
$remList .= '<option value="'.$remrow["id"].'">'.$remrow["username"].'</option>';
}
$remList .= '</select>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Your Groups</title>
<link href="style/main.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
#groupsWrap {
width: 900px;
margin-right:auto;
margin-left:auto;
background-color: #f2f2f2;
}
#groupsTitle {
height: 50px;
width: 880px;
padding-top: 20px;
padding-left: 20px;
}
#groupsContent {
float:left;
width: 700px;
margin-left: 20px;
}
#groupsAd {
float:right;
height: 600px;
width: 160px;
}
.groupsClear {
clear:both;
}
.glists {
float:left;
width: 200px;
margin-right: 30px;
}
-->
</style>
</head>

<body>
<?php include_once "header_template.php"; ?>
<div id="groupsWrap">
<div id="groupsTitle"><h1>Group Manager</h1></div><!-- end groupsTitle -->
<div id="groupsContent">
<p> </p>
<p>This top section is for adding someone from your friends to an assigned group <br />
Each person may only be in 1 group.
</p>
<?php if($friendArray != ""){ ?>
<form method="post" action="edit_groups.php">
Add <?php echo $addList; ?>
To
<select name="toGroup">
<option value=""></option>
<option value="Friends">Friends</option>
<option value="Musicians">Musicians</option>
<option value="Dancers">Dancers</option>
<option value="Painters">Painters</option>
<option value="Singers">Singers</option>
</select>
<input name="parse_var" type="hidden" value="add_to_group" />
<input name="submit" type="submit" id="add" value="Add To Group" />
</form>
<?php } ?>
<br /><br />
<p>This bottom section is for removing someone from a group.<br />
This does not delete that person as a friend, it only removes them from that group.
</p>
<?php if(($friendArray != "")&&($in_group2 !== "")){ ?>
<form method="post" action="edit_groups.php">
Remove from group
<?php echo $remList; ?>
<input name="parse_var" type="hidden" value="remove_from_group" />
<input type="submit" name="add" id="add" value="Remove From Group" />
</form>
<?php } ?>
<br />
<div class="glists"><b>Friends</b><br />
<?php
$sql_friends = mysql_query("
SELECT username FROM myMembers WHERE id IN (SELECT group_mid FROM user_groups WHERE group_oid ='$id' AND group_id= 1)
");
while($fr_row = mysql_fetch_array($sql_friends)){
echo $fr_row['username'];
echo '<br />';
}?>
</div><!-- end friends glist -->
<div class="glists">
<b>Musicians</b><br />
<?php
$sql_musicians = mysql_query("
SELECT username FROM myMembers WHERE id IN (SELECT group_mid FROM user_groups WHERE group_oid ='$id' AND group_id= 2)
");
while($ms_row = mysql_fetch_array($sql_musicians)){
echo $ms_row['username'];
echo '<br />';
}?>
</div><!-- end musician glist -->
<div class="glists">
<b>Dancers</b><br />
<?php
$sql_dancers = mysql_query("
SELECT username FROM myMembers WHERE id IN (SELECT group_mid FROM user_groups WHERE group_oid ='$id' AND group_id= 3)
");
while($dan_row = mysql_fetch_array($sql_dancers)){
echo $dan_row['username'];
echo '<br />';
}?>
</div><!-- end dancer glist -->
<div class="glists">
<b>Painters</b><br />
<?php
$sql_painters = mysql_query("
SELECT username FROM myMembers WHERE id IN (SELECT group_mid FROM user_groups WHERE group_oid ='$id' AND group_id= 4)
");
while($pn_row = mysql_fetch_array($sql_painters)){
echo $pn_row['username'];
echo '<br />';
}?>
</div><!-- end painter glist -->
<div class="glists">
<b>Singers</b><br />
<?php
$sql_singers = mysql_query("
SELECT username FROM myMembers WHERE id IN (SELECT group_mid FROM user_groups WHERE group_oid ='$id' AND group_id= 5)
");
while($sng_row = mysql_fetch_array($sql_singers)){
echo $sng_row['username'];
echo '<br />';
}?>
</div><!-- end singers glist -->
</div><!-- end groupsContent -->
<div class="groupsClear"></div><!-- end groupsClear -->
</div><!-- end groupsWrap -->
</body>
</html>

 

That example you gave is like what I have in my database.

I'm in a rush to go, but I'll try to point you in the right direction. I haven't read your code yet.

 

 

You need a database for the users, groups, posts, and user_sees_posts, user_has_groups. Something like that.

 

For every user, there can be multiple groups. For every group, there can be many users. Many to many.

For every group, there's many posts. One to many.

For every post, there are many users that can see it. For every user, there are many posts visible. Many to many.

 

User:

id, name, email, password (ect.)

 

groups:

id, name, creator (ect.)

 

user_has_groups:

id, user_id, groups_id

The id in this table will be used for updating or deleting rows.

 

user_sees_posts:

id, user_id, posts_id

 

If user with id 1 is part of group 5 and can see posts 32 and 45, both associated with group 5, you would have:

 

user:

id=1, name=blahblahblah

 

groups:

id=5, name...

 

user_has_groups:

id=1, user_id=1, groups_id=5

 

user_sees_posts

id=1, user_id=1, posts_id=32

id=2, user_id=1, posts_id=45

I'm in a rush to go, but I'll try to point you in the right direction. I haven't read your code yet.

 

 

You need a database for the users, groups, posts, and user_sees_posts, user_has_groups. Something like that.

 

For every user, there can be multiple groups. For every group, there can be many users. Many to many.

For every group, there's many posts. One to many.

For every post, there are many users that can see it. For every user, there are many posts visible. Many to many.

 

User:

id, name, email, password (ect.)

 

groups:

id, name, creator (ect.)

 

user_has_groups:

id, user_id, groups_id

The id in this table will be used for updating or deleting rows.

 

user_sees_posts:

id, user_id, posts_id

 

If user with id 1 is part of group 5 and can see posts 32 and 45, both associated with group 5, you would have:

 

user:

id=1, name=blahblahblah

 

groups:

id=5, name...

 

user_has_groups:

id=1, user_id=1, groups_id=5

 

user_sees_posts

id=1, user_id=1, posts_id=32

id=2, user_id=1, posts_id=45

 

Thanks! That really helps, I'll test it out.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.