944,172 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1623
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2009
0

Displaying friends on profile page

Expand Post »
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,

PHP Syntax (Toggle Plain Text)
  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. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enzo420 is offline Offline
20 posts
since Oct 2009
Nov 2nd, 2009
-1
Re: Displaying friends on profile page
PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 955
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Nov 2nd, 2009
-1
Re: Displaying friends on profile page
OK

PHP Syntax (Toggle Plain Text)
  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



PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 955
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Nov 3rd, 2009
0
Re: Displaying friends on profile page
[QUOTE=ardav;1034735]OK

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enzo420 is offline Offline
20 posts
since Oct 2009
Nov 3rd, 2009
-1
Re: Displaying friends on profile page
[QUOTE=enzo420;1035568]
Click to Expand / Collapse  Quote originally posted by ardav ...
OK

PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 955
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Nov 5th, 2009
0
Re: Displaying friends on profile page
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enzo420 is offline Offline
20 posts
since Oct 2009
Nov 5th, 2009
-1
Re: Displaying friends on profile page
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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 955
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Nov 9th, 2009
0
Re: Displaying friends on profile page
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enzo420 is offline Offline
20 posts
since Oct 2009
Nov 9th, 2009
-1
Re: Displaying friends on profile page
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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 955
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Nov 9th, 2009
0
Re: Displaying friends on profile page
-- 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enzo420 is offline Offline
20 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: display confirmation after sending the mail?
Next Thread in PHP Forum Timeline: nested while loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC