Page 1 of 1

friend system

Posted: Wed Dec 29, 2010 1:09 pm
by VZdemon
i'm trying to create a friend system for my website, and when ever you click a users name where ever it might appear, it will take you to my user.php page. now in that page i'm currently trying to display all of the person's friend's ids that are stored like this:1,2,3,4 in a text value in my users data base called friends. now i'm trying to get the value to the page, turn it to an array, and diplay it in seperate links using the array() and the implode() function. but i'm unsuccessfully. if any1 could pliz help me, here is my code:

user.php

Code: Select all

<html>
      <head>
      <link rel='stylesheet' rel='text\css' href='forum/fstyle.css'>
      </head>
      <body link="#990000" vlink="#CC0000">
      <table BACKGROUND="images/pnl.png">
      <center><h1> Profile </h1></center><div id='holder'>
           <div id='profile'>
              <?php
              include "connect.php";
      
              $id = $_GET['user_id'];
              $selid = $id - 5;
              $sid = $id + 2;
              $get = "SELECT * FROM users WHERE id='$selid'";
              $query = mysql_query($get)or die(mysql_error());
              $array = mysql_fetch_array($query)or die(mysql_error());

              $friends = $array['friends'];
              $name = $array['name'];
              $image = $array['picid'];
              $posts = $array['posts'];
              $date = $array['date'];
              $title = $array['title'];
              $rank = $array['rank'];
              $size = 500;
              $hide = md5(rand(9999999,0));
              $fary = array($friends);
              $frnds = implode('<br/>',$fary);

              echo "<br/><img class='floatright' src='$image' alt='' width='$size' height='$size'>";
              echo "<big>Name: $name</big><br/>";
              echo "<big>Posts: $posts</big><br/>";
              echo "<big>Joined at: $date</big><br/>";
              echo "<big>Title: $title</big><br/>";
              echo "<big>Rank: $rank <a href='add_freind.php?user_id=$sid'>add as a friend</a></big><br/>";
              echo "<big>Friends:</big> <p><a href='user.php?user_id=$sid&$hide'>".$frnds."</a><br/>";

              ?>
          </div></div>
      </table>
      </body>
</html>
output
Image

Re: friend system

Posted: Thu Dec 30, 2010 2:14 am
by Baseball435
well this is my idea of a system that you could make. What i did for an inventory in my game is i had a seperate table where it had a owner, itemname, itemprice, itemdesc, and id. So what i would do is when someone bought an item it would insert information into the inventory database. So for the owner it would be the users name, then itemname would be the item name and the rest is obvious.

Then when i wanted to display it i would make a while loop so that it displays everything.

For your friend system you could try doing something like that. Then you wouldnt need the arrays. Just an idea, but it might not help you. Idk haha.

Re: friend system

Posted: Thu Dec 30, 2010 5:59 am
by Jackolantern
If you are storing all the ids like "11, 42, 91, 105..." in one field in the database, that is inefficient database design. You will make your life much, much easier by making a new table for the friends. So, you would have a main table called something like "users". The Users table would list everyone who is registered with your website, and each would have a unique ID (which MySQL can make for you by creating an ID field, making it the primary key and setting it to Auto_Increment). Then you create the 2nd table called "friends". In Friends, you could probably just have 3 fields: relationshipID, owner, and friendID. For example, say I have a User ID of 54, and you have a User ID of 12. I friend you, so that makes a row with an Owner value of 54, a FriendID value of 12, and a relationshipID assigned by MySQL just like the Users table (that field would be the Friends table's primary key and would Auto_Increment). The beauty of this design is that I can have an unlimited amount of friends, since more rows can be added with 54 in the owner field, you can be friended by an unlimited amount of people, since more rows can be added with 12 as the friendID, and our User IDs can also appear in different arrangements (i.e. others can friend me, and you can friend users, too).

This is the basis of relational database management systems such as MySQL. By keeping only one piece of data in each field, and only one "record" in each row, you make your database much more searchable through queries, you will make it perform faster, it will be more tolerant to incidental corruption, you will not have to keep increasing the length of your "friends field" (how could Tila Tequila get her million friends with your database design?), you will have access to better record keeping, and you will simplify the PHP code to make use of the data.

Re: friend system

Posted: Thu Dec 30, 2010 3:19 pm
by VZdemon
i though of that before the text value array thingy, but if i get like 100 users (witch i doubt) then the friendships table would be HUGE!!! and i use a free hosting space, so with all the images and uploads i would reach my limit.

Re: friend system

Posted: Thu Dec 30, 2010 10:28 pm
by Jackolantern
Rows add almost nothing to a database table, particularly if you only have 3 fields in that table. You would probably have to get about 5,000 entries or more to even hit 2mb.

As it stands, having all the friend IDs in a text field will make you miss out on a ton of functionality, because you will not be able to query for specific friends.