Jump to content

Friend System


pro_se

Recommended Posts

Hi, I recently started an endeavor to create a friend system where it will allow people to add people as friends and I have run into quite a bit of trouble, please help.

 

..I am STUCK!

 

I want to display the friends and their pictures as well as links to their page with their friends on that page. But the design of my page only allows 3 thumbnails to be displayed.

 

I have created the code to display one friend but I cannot get it to display all the friends and limit it to only display 3.

 

Here is the code that I have managed to create:

<?php function getfriendsforuid($puid) {
$query = "SELECT * FROM `friends` WHERE `passive_id` = '$puid' AND `accpet` = 'y' LIMIT 3";
$result_query = mysql_query($query);
while($fnamelname = mysql_fetch_array($result_query)) {
    $friend = $fnamelname["acting_id"];
$query = "SELECT * FROM `pics` WHERE `uid` = '$friend'";
$result_query = mysql_query($query);
while($fnamelname = mysql_fetch_array($result_query)) {
    $pic = $fnamelname["path"];
}
}
$query2 = "SELECT * FROM `user` WHERE `id` = '$friend'";
$result_query2 = mysql_query($query2);
while($fnamelname2 = mysql_fetch_array($result_query2)) {
    $uname = $fnamelname2["username"];
}
$query3 = "SELECT * FROM `pics` WHERE `uid` = '$friend' AND `default` = 'y'";
$numpics = mysql_query($query3);
$picsfound = mysql_num_rows($numpics);
if ($picsfound >= 1) { 
echo "$pic";
echo "\" onclick=\"window.location = '/profile/$uname'\"";
} else {
echo "npic.jpg\" onclick=\"window.location = '/profile/$uname'\"";
}
} ?> 

 

That only displays one friend.. I was thinking maybe use a while statement to get all the friends but I am not sure of the procedure.

 

Any and all help is welcomed...

 

Oh, and here is the function-call:

<?php  echo "<div class=\"friendbar\" style=\"cursor:pointer;\"><img border=\"0\" src=\"/u/s_"; getfriendsforuid($puid); echo "></div>"; ?>

-- just to put it out there...

 

Thank you..

Link to comment
Share on other sites

Well I don't know about your tables in DB.

 

But if your passive_id is numeric field than next query shouldn't work because of apostrophes.

$query = "SELECT * FROM `friends` WHERE `passive_id` = '$puid' AND `accpet` = 'y' LIMIT 3";

 

Could you show us tables from db. I think you could make it easier with JOIN method or....

Link to comment
Share on other sites

yea, here is the DB Schema

-- phpMyAdmin SQL Dump

-- version 2.8.2.4

-- http://www.phpmyadmin.net

--

-- Host: mysql.cvnetworks.net

-- Generation Time: Apr 15, 2007 at 05:54 PM

-- Server version: 5.0.24

-- PHP Version: 4.4.4

--

-- Database: `notexting`

--

 

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

 

--

-- Table structure for table `comments`

--

 

CREATE TABLE `comments` (

  `commentid` int(12) NOT NULL auto_increment,

  `toid` int(11) NOT NULL,

  `fromid` int(11) NOT NULL,

  `comment` text NOT NULL,

  `datetime` datetime NOT NULL,

  PRIMARY KEY  (`commentid`)

) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

 

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

 

--

-- Table structure for table `friends`

--

 

CREATE TABLE `friends` (

  `friendshipid` int(12) NOT NULL auto_increment,

  `acting_id` int(11) NOT NULL,

  `passive_id` int(11) NOT NULL,

  `date` date NOT NULL,

  `accpet` enum('y','n') NOT NULL default 'n',

  PRIMARY KEY  (`friendshipid`)

) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

 

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

 

--

-- Table structure for table `pics`

--

 

CREATE TABLE `pics` (

  `pid` int(11) NOT NULL auto_increment,

  `uid` int(11) NOT NULL,

  `path` varchar(50) NOT NULL,

  `default` enum('y','n') NOT NULL default 'n',

  PRIMARY KEY  (`pid`)

) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

 

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

 

--

-- Table structure for table `profile`

--

 

CREATE TABLE `profile` (

  `id` int(11) NOT NULL,

  `aboutme` text NOT NULL,

  `im` varchar(50) default NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

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

 

--

-- Table structure for table `tags`

--

 

CREATE TABLE `tags` (

  `tagid` int(11) NOT NULL auto_increment,

  `uid` int(11) NOT NULL,

  `tag` varchar(20) NOT NULL,

  PRIMARY KEY  (`tagid`)

) ENGINE=MyISAM AUTO_INCREMENT=95 DEFAULT CHARSET=latin1 AUTO_INCREMENT=95 ;

 

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

 

--

-- Table structure for table `user`

--

 

CREATE TABLE `user` (

  `id` int(11) NOT NULL auto_increment,

  `fname` varchar(20) NOT NULL,

  `lname` varchar(20) NOT NULL,

  `username` varchar(20) NOT NULL,

  `password` varchar(32) NOT NULL,

  `email` varchar(50) NOT NULL,

  `bday` date NOT NULL,

  `gender` enum('m','f','o') NOT NULL default 'm',

  `orientation` enum('s','b','g','o') NOT NULL default 's',

  `location` varchar(50) NOT NULL default 'Fullerton, CA',

  `signup_date` date NOT NULL,

  `ip` varchar(16) NOT NULL,

  `logintimes` int(10) NOT NULL default '0',

  `lastlogin` date NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=69 DEFAULT CHARSET=latin1 AUTO_INCREMENT=69 ;

 

 

Link to comment
Share on other sites

Well I don't know about your tables in DB.

 

But if your passive_id is numeric field than next query shouldn't work because of apostrophes.

$query = "SELECT * FROM `friends` WHERE `passive_id` = '$puid' AND `accpet` = 'y' LIMIT 3";

 

Could you show us tables from db. I think you could make it easier with JOIN method or....

Well, the whole thing works, I just need help making the stupid thing display all the rows where the friends id matches whats in the database...

Link to comment
Share on other sites

The problem with your code is db link. When you create link on db and execute first query everything is okay. But after you (in first while loop) execute another query, than you lost data in first query. I hope you understand. Basicaly you would need more than one link on database to execute all of your queries.

 

Please send via email again structure of your data but this time export data too so I can test it and maybe make one query for everything you need.

 

If I don't answer soon = sleeping already.

Link to comment
Share on other sites

Okay, I couldn't read your tables because you made them so "different"....

 

But, here is what I made:

 

getfriendsforuid(1);

function getfriendsforuid($puid) {
$cnn1 = mysql_connect('localhost', 'root', 'root');
$cnn2 = mysql_connect('localhost', 'root', 'root');	

$db1 = mysql_select_db('t', $cnn1);
$db2 = mysql_select_db('t', $cnn2);	

$sql1 = "SELECT tbl_users. * , tbl_pictures.path 
		FROM tbl_users
		LEFT JOIN tbl_pictures ON tbl_pictures.id = tbl_users.picture_id
		WHERE tbl_users.id = $puid";

$res1 = mysql_query($sql1, $cnn1);

while($row1 = mysql_fetch_assoc($res1)) {
	echo "<br>User ${row1['fname']} has image: ${row1['path']}<br>His/Her friends are:";
	$id = $row1['id'];
	$sql2 = "SELECT tbl_users. * , tbl_pictures.path
			FROM tbl_users
			LEFT JOIN tbl_friends ON tbl_users.id = tbl_friends.friend_id
			INNER JOIN tbl_pictures ON tbl_users.picture_id = tbl_pictures.id
			WHERE tbl_friends.user_id = $id";
		$res2 = mysql_query($sql2, $cnn2);
		$i = 1;
		while($row2 = mysql_fetch_assoc($res2)) {
			echo "<br>$i. ${row2['fname']} with image ${row2['path']}";
			$i++;		
		}
}
}

 

Here is new DB exported. So, use it to test:

-- phpMyAdmin SQL Dump
-- version 2.7.0-pl1
-- http://www.phpmyadmin.net
-- 
-- Host (domena): localhost
-- Vrijeme podizanja: 16. Tra 2007. u 05:41
-- Verzija servera: 5.0.11
-- verzija PHP-a: 5.1.0
-- 
-- Baza podataka: `t`
-- 

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

-- 
-- Struktura tablice `tbl_friends`
-- 

DROP TABLE IF EXISTS `tbl_friends`;
CREATE TABLE `tbl_friends` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `friend_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

-- 
-- Izvoz <i>(dump)</i> podataka tablice `tbl_friends`
-- 

INSERT INTO `tbl_friends` VALUES (1, 1, 2);
INSERT INTO `tbl_friends` VALUES (2, 1, 4);

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

-- 
-- Struktura tablice `tbl_pictures`
-- 

DROP TABLE IF EXISTS `tbl_pictures`;
CREATE TABLE `tbl_pictures` (
  `id` int(11) NOT NULL auto_increment,
  `path` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

-- 
-- Izvoz <i>(dump)</i> podataka tablice `tbl_pictures`
-- 

INSERT INTO `tbl_pictures` VALUES (1, 'Image1.jpg');
INSERT INTO `tbl_pictures` VALUES (2, 'Image2.jpg');
INSERT INTO `tbl_pictures` VALUES (3, 'Image3.jpg');
INSERT INTO `tbl_pictures` VALUES (4, 'Image4.jpg');

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

-- 
-- Struktura tablice `tbl_users`
-- 

DROP TABLE IF EXISTS `tbl_users`;
CREATE TABLE `tbl_users` (
  `id` int(11) NOT NULL auto_increment,
  `fname` varchar(30) NOT NULL,
  `lname` varchar(30) NOT NULL,
  `picture_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

-- 
-- Izvoz <i>(dump)</i> podataka tablice `tbl_users`
-- 

INSERT INTO `tbl_users` VALUES (1, 'Person1', '', 4);
INSERT INTO `tbl_users` VALUES (2, 'Person2', '', 4);
INSERT INTO `tbl_users` VALUES (3, 'Person3', '', 1);
INSERT INTO `tbl_users` VALUES (4, 'Person4', '', 2);

 

I tested it and my result for user with ID=1 was:

User Person1 has image: Image4.jpg

His/Her friends are:

1. Person2 with image Image4.jpg

2. Person4 with image Image2.jpg

 

I hope you can use this to solve your issue. ;)

Link to comment
Share on other sites

Well, ok.. That Partially works... My database does not have predefined pictures... The pictures are all user uploaded and the path (or filename) is stored into a row on the database. What you gave me works fine when you are looking up images with a specific ID. I need a way to get the path with just the friends user ID. I dont know how to do all that LEFT JOIN and INNER JOIN stuff so I could not modify it to work for me.. Can you explain to me how that works or can you modify the code you sent me to get the path by looking in the 'pics' table where the ID = PUID and get the path out..

Link to comment
Share on other sites

okay, I got the stupid thing to have the if/ else thing work.. now.. all I am getting is the same three friends.. all of them are the same.. how can i display all the friends different?

 

function getfriendsforuid($puid) {

$query = "SELECT * FROM `friends` WHERE `passive_id` = '$puid' AND `accpet` = 'y' LIMIT 3";

$result_query = mysql_query($query);
$numfriends = mysql_num_rows($result_query);
$repeat = 0;
while ($repeat < $numfriends) {
while($fname1lname = mysql_fetch_assoc($result_query)) {
    $friend = $fname1lname["acting_id"];
$query = "SELECT * FROM `pics` WHERE `uid` = '$friend' AND `default` = 'y'";
$result_query = mysql_query($query);
while($fnam2elname = mysql_fetch_assoc($result_query)) {
    $pic = $fnam2elname["path"];
$query2 = "SELECT * FROM `user` WHERE `id` = '$friend'";
$result_query2 = mysql_query($query2);
while($fnamelname2 = mysql_fetch_assoc($result_query2)) {
   $uname = $fnamelname2["username"];
$query3 = "SELECT * FROM `pics` WHERE `uid` = '$friend' AND `default` = 'y'";
$numpics = mysql_query($query3);
$picsfound = mysql_num_rows($numpics);
if ($picsfound >= 1) { 
echo "<div class=\"friendbar\"><a href=\"/profile/$fnamelname2[username]\"><img border=\"0\" src=\"/u/s_$pic\"></a></div>";

}
}
}
}
if ($picsfound == 0) {echo "<div class=\"friendbar\"><a href=\"/profile/$uname\"><img border=\"0\" src=\"/u/s_np.jpg\"></a></div>";}
$repeat++; 
}
}

 

and a link to my site is: http://notexting.com/profile/cvarma

Link to comment
Share on other sites

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.