Jump to content

Friends like with MySpace


Seraskier

Recommended Posts

I think the best way to achieve this is to create a table in your database named users like:

[code]
CREATE TABLE users (
`userId` int not null auto_increment,
`name` varchar(255) not null default '',
`address` varchar(255) not null default '',
...,
PRIMARY KEY (userId)
);
[/code]

To Link all the users (as friends) to eachother:

[code]
CREATE TABLE users_users (
`userId` int not null,
`friendId` int not null,
PRIMARY KEY(userId, friendId)
);
[/code]

To find all your friends:

[code]
SELECT friends.*
FROM users_users
INNER JOIN users as `friends` ON friends.userId = users_users.friendId
WHERE users_users.userId = '$MyId'
[/code]
Table
- friends table

column names:
- user_id
- friend_id

Now when you do your sql, seach the friends table for where user_id equals the id of the current user. Then get the profile picture of the friend_id with another sql, where user_id equals friend_id from the previous search.

Example:

[code]<?php
$sql = mysql_query("SELECT * FROM friends_table WHERE user_id='$_SESSION[user_id]'")or die(mysql_error());
while($row = mysql_fetch_array($sql)){
    $sqls = mysql_query("SELECT * FROM users_table WHERE user_id='$row[friend_id]'")or die(mysql_error());
    $rows = mysql_fetch_array($sqls);
    echo $rows[users_display_name];
}
?>[/code]
With this example, you will need to have a session that stores the current users id number (the unique one from the database given at sign up).
You will need 2 tables one that has your user, and the one that has friend connections.
This is ONLY for displaying of friends.

To add/modify/delete friends there are a few more things you will need to do.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.