| | |
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: 18
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 29 Days Ago
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; 29 Days Ago at 7:17 pm.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
-1
#3 29 Days Ago
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; 29 Days Ago at 8:03 pm.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#4 29 Days Ago
[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 28 Days Ago
[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; 28 Days Ago at 3:10 pm.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
-1
#7 26 Days Ago
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.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
-1
#9 22 Days Ago
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.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#10 22 Days Ago
-- 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
- 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)
- PHP Friends request system! (PHP)
- 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 |
apache api array basic beginner binary broken cache cakephp checkbox class cms code computing confirm cron curl customizableitems database date delete display dynamic echo email error external file files filter folder form forms forum function functions gc_maxlifetime google headmethod host howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction memmory memory menu mlm multiple mysql navigation oop parsing paypal pdf php phpmysql problem query question radio random recursion remote script search select server sessions sms snippet source space sql syntax system table thesishelp trouble tutorial update upload url validator variable video web youtube





