Solved

friend request and show active friends php

Posted on 2011-03-21
4
640 Views
Last Modified: 2012-05-11
i made a working friend request system but it breaks if two users add the same person. :S

its showing users that have been activated for the current memberID in the output of my query
i just put id's of users in both ways in the table when some one wants to add a friend.
but if 2 users add the same person that persons id will show in two rows and my query cant handle that so am trying to fix it so it doesn't have that problem .

the code i am shoing is the bit that checks if the user (when on there profile) has any friends activated and if so show them

 
<?php
	$friendPlayerName = "Player Name: <br>";
$Email = "Players Email: <br>";
$title = "Your Accepted Friends: <br>";

	include("conf.php");
       
    $connection = mysql_connect($databaseURL,$databaseUName,$databasePWord);
        // or die ("Error while connecting to localhost");
    $db = mysql_select_db($databaseName,$connection);
        //or die ("Error while connecting to database");
	
$sqlf = "SELECT friends.friendPage FROM friends 
WHERE friends.activate = '1' AND friends.friendID = '".$_SESSION['MemberID']."'";


$resultf = mysql_query($sqlf)or die(mysql_error());

while($row = mysql_fetch_array($resultf, MYSQL_ASSOC))
{
	$friendPage = $row['friendPage'];//now i can compare this with current log in user later on

}


//where current member id = friendPage and friendId = $friendPage

$sqlfTwo = "SELECT friends.friendID,friends.playername,friends.UserEmail,images.imgSmall FROM friends JOIN images ON images.memberId = friends.friendID 
WHERE friends.activate = '1' AND friendID = '$friendPage'";
$resultfTwo = mysql_query($sqlfTwo)or die(mysql_error());

while($row = mysql_fetch_array($resultfTwo, MYSQL_ASSOC))
{
	

echo '<img src='. $row['imgSmall'].'><br>';
echo '<a href="users.php?id='. $row['friendID']. '">' . $row['playername'] . '</a><br>';
echo $row['UserEmail'].'<br><br>'; 

}

mysql_close($connection);

Open in new window

CREATE TABLE IF NOT EXISTS `friends` (
  `id` int(11) NOT NULL auto_increment,
  `friendID` varchar(50) NOT NULL,
  `UserName` varchar(55) NOT NULL,
  `friendPage` varchar(55) NOT NULL,
  `playername` varchar(55) NOT NULL,
  `activate` varchar(55) NOT NULL,
  `UserEmail` varchar(55) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `friends`
--

INSERT INTO `friends` (`id`, `friendID`, `UserName`, `friendPage`, `playername`, `activate`, `UserEmail`) VALUES
(1, '2', '', '1', 'chris', '1', 'chris@chris.com'),
(2, '1', '', '2', 'admin', '1', 'admin@admin.com'),
(3, '4', '', '1', 'ben', '1', 'ben@ben.com'),
(4, '1', '', '4', 'admin', '1', 'admin@admin.com');

Open in new window


id ,      
friendID = id of session or id of friend (i put this in both ways),
UserName = not using      
friendPage = id of session or id of friend (i put this in both ways),       
0
Comment
Question by:helpchrisplz
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
palanee83 earned 500 total points
ID: 35186612
My understanding is that,  one person is allowed to add more than one friend. So You need to have separate table to  store the friend request as this is one-to-many relationship.  Design your table as mentioned below to solve your problem.

User Table Schema
===============

CREATE TABLE IF NOT EXISTS `users` (
  `UserID` int(11) NOT NULL auto_increment,
  `UserName` varchar(55) NOT NULL,
  `friendPage` varchar(55) NOT NULL,
  `playername` varchar(55) NOT NULL,
  `activate` varchar(55) NOT NULL,
  `UserEmail` varchar(55) NOT NULL,
  PRIMARY KEY  (`UserID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Friend Table Schema
=================

CREATE TABLE IF NOT EXISTS `friends` (
  `requestID` int(11) NOT NULL auto_increment,
  `UserID` int(11) NOT NULL,
  `friendID` varchar(50) NOT NULL,
  PRIMARY KEY  (`requestID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;





0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 35191175
ok i have them tables up now

do i just put the users in both ways when one adds?

INSERT INTO friends(UserID, friendID) VALUES ('$UserID', '$friendID');

INSERT INTO friends(friendID, UserID) VALUES ('$friendID', '$UserID');


0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 35228046
hmmm my problem was more complicated than i expected
0
 
LVL 1

Author Closing Comment

by:helpchrisplz
ID: 35228048
lots of other stuff that was not working but yes
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now