| | |
Displaying friends on profile page
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 20
Reputation:
Solved Threads: 0
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,
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)
$connect = mysql_connect("localhost", "root", "") or die (mysql_error()); $connect; mysql_select_db ("phplogin"); $userfriend = mysql_query ("SELECT req FROM friends WHERE user='$profileid' AND confirmed=1"); $userfriendarray = mysql_fetch_array($userfriend); $userfriendreq = $userfriendarray['req']; $userreq = mysql_query ("SELECT user FROM friends WHERE req='$profileid' AND confirmed=1"); if ($userreq != 0) { $userreqarray = mysql_fetch_array($userreq); $userreq2 = $userreqarray['user']; } if ($userreqarray != 0 && $userfriendarray!= 0) // merges arrays if they both contain data { $friends = array_merge($userreqarray, $userfriendarray); $friend1 = array_rand($friends); } elseif ($userreqarray != 0) { var_dump ($userreqarray); foreach($userreqarray as $userreq2); { echo "$userreq2"; } } elseif($userfriendarray != 0) { foreach($userfriendarray as $req5 => $reqno) // there is one entry in the table yet this returns 2 values? i dont know why? { echo "$reqno"; } } echo '<br></center></a></div>'; ?>
-1
#2 Nov 2nd, 2009
PHP Syntax (Toggle Plain Text)
$rs = mysql_query("SELECT * FROM friends WHERE (reg = '$id' AND confirmed = '1') OR (user ='$id' AND confirmed = '1')"); while($data = mysql_fetch_array($rs)){ if($data['user'] != $id){ $friendarray[] = $data['user']; }else{ $friendarray[] = $data['req']; } }
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.
Happy Humbugging Christmas
-1
#3 Nov 2nd, 2009
OK
This should give you the following fields:
id
user
req
confirmed
username
requsername
OR:
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.
PHP Syntax (Toggle Plain Text)
$rs = mysql_query("SELECT friends.*,users2.username,reqs.username AS requsername from friends LEFT JOIN users users2 ON friends.user = users2.id 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)
while($data = mysql_fetch_array($rs)){ if($data['user'] != $id){ $friendarray[] = $data['username']; }else{ $friendarray[] = $data['requsername']; } }
OR:
PHP Syntax (Toggle Plain Text)
while($data = mysql_fetch_array($rs)){ if($data['user'] != $id){ $friendarray[] = array($data['user'],$data['username']); }else{ $friendarray[] = array($data['req'], $data['requsername']); } }
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.
Happy Humbugging Christmas
•
•
Join Date: Oct 2009
Posts: 20
Reputation:
Solved Threads: 0
0
#4 Nov 3rd, 2009
[QUOTE=ardav;1034735]OK
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
PHP Syntax (Toggle Plain Text)
$rs = mysql_query("SELECT friends.*,users2.username,reqs.username AS requsername from friends LEFT JOIN users users2 ON friends.user = users2.id 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
-1
#5 Nov 3rd, 2009
[QUOTE=enzo420;1035568]
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
friends
if user '1' (ardav) is the focus, the recordset should output
Your array (if taking the last example I posted), should be:
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:
where $i is the counter in the loop.
•
•
•
•
OK
PHP Syntax (Toggle Plain Text)
$rs = mysql_query("SELECT friends.*,users2.username,reqs.username AS requsername from friends LEFT JOIN users users2 ON friends.user = users2.id 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)
id username 1 ardav 2 filo34 3 canzo 4 peeps456 5 costas 6 triage 7 carchaser 8 rugbyboy20
friends
PHP Syntax (Toggle Plain Text)
id user req confirmed 1 1 8 1 2 1 6 1 3 2 1 1 4 3 1 0 5 2 3 1 6 4 8 1
if user '1' (ardav) is the focus, the recordset should output
PHP Syntax (Toggle Plain Text)
id user req confirmed username requsername 1 1 8 1 ardav rugbyboy20 2 1 6 1 ardav triage 3 2 1 1 filo34 ardav
Your array (if taking the last example I posted), should be:
PHP Syntax (Toggle Plain Text)
array( array(8, 'rugbyboy20'), array(6, 'triage'), array(2, 'filo34') )
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)
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.
Happy Humbugging Christmas
-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.
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.
Happy Humbugging Christmas
•
•
Join Date: Oct 2009
Posts: 20
Reputation:
Solved Threads: 0
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
--
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
![]() |
Similar Threads
- PHP Friends request system! (PHP)
- Using PHP to upload an image into an sql database, and then displaying on a profile p (PHP)
- Profile Viewing Page suggestions/ideas (DaniWeb Community Feedback)
- Suggestion for new field in the public profile page (DaniWeb Community Feedback)
- Selecting info from multiple mysql tables and display (PHP)
- Do you know what I need for this to Validate (PHP)
- FireFox Video not displaying properly test page works site code does not (Graphics and Multimedia)
- displaying data onto a web page (ASP.NET)
Other Threads in the PHP Forum
- Previous Thread: PHP & MySQL Archive
- Next Thread: nested while loops
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email encode error fcc file files folder form forms function functions google howtowriteathesis href htaccess html image include insert integration ip java javascript joomla limit link login loop mail menu methods mlm mod_rewrite multiple multipletables mysql oop open parse paypal pdf php problem provider query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table template tutorial update upload url validation validator variable video web xml youtube






