Solved

friend request and show active friends php

Posted on 2011-03-21
4
649 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

830 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