Displaying friends on profile page

Thread Solved

Join Date: Oct 2009
Posts: 20
Reputation: enzo420 is an unknown quantity at this point 
Solved Threads: 0
enzo420 enzo420 is offline Offline
Newbie Poster

Displaying friends on profile page

 
0
  #1
Nov 2nd, 2009
Hi all, i've recently made a friends script that adds the id of user who requested the friend (user) and the requestee (req), with a confirmed column, 0 if pending, 1 if accepted.

i was looking for a way to search through the friends table for all friends of a certain id, i would need to search user column and req column where confirmed =1, then put all the values into an array and list them on the profile page. but im stuck,

  1. $connect = mysql_connect("localhost", "root", "") or die (mysql_error());
  2. $connect;
  3. mysql_select_db ("phplogin");
  4. $userfriend = mysql_query ("SELECT req FROM friends WHERE user='$profileid' AND confirmed=1");
  5. $userfriendarray = mysql_fetch_array($userfriend);
  6. $userfriendreq = $userfriendarray['req'];
  7.  
  8.  
  9. $userreq = mysql_query ("SELECT user FROM friends WHERE req='$profileid' AND confirmed=1");
  10. if ($userreq != 0)
  11. {
  12. $userreqarray = mysql_fetch_array($userreq);
  13. $userreq2 = $userreqarray['user'];
  14. }
  15. if ($userreqarray != 0 && $userfriendarray!= 0) // merges arrays if they both contain data
  16. {
  17. $friends = array_merge($userreqarray, $userfriendarray);
  18. $friend1 = array_rand($friends);
  19. }
  20. elseif ($userreqarray != 0)
  21. {
  22. var_dump ($userreqarray);
  23. foreach($userreqarray as $userreq2);
  24. {
  25. echo "$userreq2";
  26. }
  27. }
  28. elseif($userfriendarray != 0)
  29. {
  30. foreach($userfriendarray as $req5 => $reqno) // there is one entry in the table yet this returns 2 values? i dont know why?
  31. {
  32. echo "$reqno";
  33. }
  34. }
  35. echo '<br></center></a></div>';
  36.  
  37.  
  38.  
  39. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #2
Nov 2nd, 2009
  1. $rs = mysql_query("SELECT * FROM friends WHERE (reg = '$id' AND confirmed = '1') OR (user ='$id' AND confirmed = '1')");
  2.  
  3. while($data = mysql_fetch_array($rs)){
  4. if($data['user'] != $id){
  5. $friendarray[] = $data['user'];
  6. }else{
  7. $friendarray[] = $data['req'];
  8. }
  9. }
This should place all the friends 'ids' the user stipulated as 'id' into an array called $friendarray.

If you want the actual usernames in the array, just use an INNER JOIN to get this data from the users table. I gotta go for a minute. Will be back later with this.
Last edited by ardav; Nov 2nd, 2009 at 7:17 pm.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #3
Nov 2nd, 2009
OK

  1. $rs = mysql_query("SELECT friends.*,users2.username,reqs.username AS requsername
  2. from friends
  3. LEFT JOIN users users2 ON friends.user = users2.id
  4. LEFT JOIN users reqs ON friends.req = reqs.id WHERE confirmed = 1 AND (req = 1 OR `user` = 1)");

This should give you the following fields:

id
user
req
confirmed
username
requsername



  1. while($data = mysql_fetch_array($rs)){
  2. if($data['user'] != $id){
  3. $friendarray[] = $data['username'];
  4.  
  5. }else{
  6. $friendarray[] = $data['requsername'];
  7. }
  8. }


OR:

  1. while($data = mysql_fetch_array($rs)){
  2. if($data['user'] != $id){
  3. $friendarray[] = array($data['user'],$data['username']);
  4.  
  5. }else{
  6. $friendarray[] = array($data['req'], $data['requsername']);
  7. }
  8. }

You now have an array of arrays that link the id of the friend to the friend username. Useful if you want to create hyperlinks of the friend username, e.g. to go to the friend's public profile page, or to send a PM. You can also sort the arrays alphabetically, if required.
Last edited by ardav; Nov 2nd, 2009 at 8:03 pm.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: enzo420 is an unknown quantity at this point 
Solved Threads: 0
enzo420 enzo420 is offline Offline
Newbie Poster
 
0
  #4
Nov 3rd, 2009
[QUOTE=ardav;1034735]OK

  1. $rs = mysql_query("SELECT friends.*,users2.username,reqs.username AS requsername
  2. from friends
  3. LEFT JOIN users users2 ON friends.user = users2.id
  4. LEFT JOIN users reqs ON friends.req = reqs.id WHERE confirmed = 1 AND (req = 1 OR `user` = 1)");

is user2 the users table? also is reqs supposed to be plural or do i copy the column name exactly?

cant get it to work
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #5
Nov 3rd, 2009
[QUOTE=enzo420;1035568]
Originally Posted by ardav View Post
OK

  1. $rs = mysql_query("SELECT friends.*,users2.username,reqs.username AS requsername
  2. from friends
  3. LEFT JOIN users users2 ON friends.user = users2.id
  4. LEFT JOIN users reqs ON friends.req = reqs.id WHERE confirmed = 1 AND (req = 1 OR `user` = 1)");

is user2 the users table? also is reqs supposed to be plural or do i copy the column name exactly?

cant get it to work

users2 is an alias for your 'users' table
reqs is another alias for your 'users' table

I have taken that your 'users' table has the following fields (amongst others):

id (an autoincrement integer primary key)
username (varchar field holding user's handle)


Your 'friends' table, I have taken to have the following fields:

id (just an autoincrement integer primary key)
user (foreign key on id in the 'users' table)
req (foreign key on id in the 'users' table)
confirmed (0 = pending, 1 = confirmed)


The purpose of the sql is to link the 'friends.user' field (which is an user id) to the 'users.id' field and also link 'friends.req' field (which is also an user id) to the 'users.id' field. Thus we create two 'new' fields called 'username' and 'requsername'

For example:

users
  1. id username
  2. 1 ardav
  3. 2 filo34
  4. 3 canzo
  5. 4 peeps456
  6. 5 costas
  7. 6 triage
  8. 7 carchaser
  9. 8 rugbyboy20

friends
  1. id user req confirmed
  2. 1 1 8 1
  3. 2 1 6 1
  4. 3 2 1 1
  5. 4 3 1 0
  6. 5 2 3 1
  7. 6 4 8 1

if user '1' (ardav) is the focus, the recordset should output

  1. id user req confirmed username requsername
  2. 1 1 8 1 ardav rugbyboy20
  3. 2 1 6 1 ardav triage
  4. 3 2 1 1 filo34 ardav

Your array (if taking the last example I posted), should be:

  1. array(
  2. array(8, 'rugbyboy20'),
  3. array(6, 'triage'),
  4. array(2, 'filo34')
  5. )

I may have named some of your db fields or tables incorrectly, otherwise it should work.

for each array you could make links like this:

  1. echo "<a href=\"profile.php?id={$fieldarray[$i][0]}\">{$fieldarray[$i][1]}</a>";

where $i is the counter in the loop.
Last edited by ardav; Nov 3rd, 2009 at 3:10 pm.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: enzo420 is an unknown quantity at this point 
Solved Threads: 0
enzo420 enzo420 is offline Offline
Newbie Poster
 
0
  #6
Nov 5th, 2009
everything is bang on apart from i dont have foreign keys set up?

how do i do that in phpmyadmin? ive googled it a couple of times but nothing useful comes up?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #7
Nov 5th, 2009
You don't actually need to explicitly define foreign keys. Purists may scoff at this as you may end up with invalid data in the friends table. However, if you take care not to delete users, the friends records should remain valid.

However, defining indices should speed up querying, as long as you don't define indices on fields that do not appear in WHERE clauses.


ALTERNATIVELY
If you want to set constraints (actually define the foreign keys), see this blog page: http://www.mytechmusings.com/2008/04...-in-mysql.html

It's one of the best examples (including video tutorials) of how to set up FKs in phpmyadmin.

If you are using InnoDB tables, this should be straightforward. If using MyISAM tables, you'll probably need to convert them to InnoDB.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: enzo420 is an unknown quantity at this point 
Solved Threads: 0
enzo420 enzo420 is offline Offline
Newbie Poster
 
0
  #8
Nov 9th, 2009
when i said bangon i meant the information you put up about tables was correct... i still cant get it to work, the query returns false
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #9
Nov 9th, 2009
OK, if you want to send me an exported *.sql file of your DB, I can import it and give it a whirl, see what we get. The above code works fine with a duplicate DB I produced (from the info you gave). Just attach the file to your next post.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: enzo420 is an unknown quantity at this point 
Solved Threads: 0
enzo420 enzo420 is offline Offline
Newbie Poster
 
0
  #10
Nov 9th, 2009
-- Table structure for table `friends`
--

CREATE TABLE IF NOT EXISTS `friends` (
`user` varchar(30) NOT NULL,
`req` varchar(30) NOT NULL,
`confirmed` tinyint(1) NOT NULL DEFAULT '0',
KEY `req` (`req`),
KEY `req_2` (`req`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

INSERT INTO `friends` (`user`, `req`, `confirmed`) VALUES
('7', '10', 1),
('8', '7', 1),
('7', '5', 1),
('8', '9', 1);

thats it there man cheers
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 926 | Replies: 22
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC