Solved

add a friend php

Posted on 2011-03-08
13
1,395 Views
Last Modified: 2012-05-11
ok so i already have a system set up to allow users to register and then they can search for other users. thats done

so am thinking about how to allow users to add friends and display links to there friends on there profile page like facebook

unsure about the tables and the code on the button please help
0
Comment
Question by:helpchrisplz
  • 6
  • 6
13 Comments
 
LVL 11

Assisted Solution

by:patsmitty
patsmitty earned 100 total points
Comment Utility
Well you would set up a table called something like "tblFriends" with the following fields as a concatenated primary key:
user_id
friend_id
Both of these fields are foreign keys in the user table.

Each time a user "friends" somebody, you would enter the user's ids into the table both ways. Meaning that if Joe Blow who has user_id = 17 "friends" John Doe who's user_id = 120, then you would run the following INSERT statements:
 
INSERT INTO tblFriends(user_id, friend_id) VALUES ('17', '120');

INSERT INTO tblFriends(user_id, friend_id) VALUES ('120', '17');

Open in new window

This will make both of them show as friends.

Hope this is a start.
0
 
LVL 20

Accepted Solution

by:
Mark Brady earned 400 total points
Comment Utility
On every users page display a simple button "Add to Friends" or whatever you want on the button. Display it on each persons profile page. If I am looged in and am looking at your page for eg: I could click that button and it would add that users id to my "friends" table. I assume that on each profile page you have a way to get their user id? If so, you could do something simle to add them. Here's the ajax way.

<input type="button" onclick="addToFriends()" />

there's the button. Here's the script. Keep in mind for this example I am assuming you could store the users id in a hidden div. If you have the users id in a php variable this is how it would go.

<div id="userid" style="display:none"><?php echo $userID; ?></div>

Now the javascript and ajax to add to friends.

<script language="javascript">
function addToFriends()
{
var userID = document.getElementById('userid').innerHTML;
if(userID != ''){
ajax.post("add-to-friends.php",allDone, 'userid=' + userID);
}
}
function allDone(result);
{
alert('Added to your Friends list');
}
</script>

and the code for the add-to-friends.php file

<?php // add-to-friends.php
include("connection-script.php"); // or however you connect to mysql
// you will need the current logged in users id here but I'll assume it is in $username

$friend_id = $_POST['userid'];
if($friend_id != "") mysql_query("INSERT INTO friends (`friend id`) VALUES ('$friend_id')") or die(mysql_error());
echo "done";
?>


You will need to include the miniajax.js library I am using so I have attached it for you.
So, you need to know the current users username. You also need to know the other users id so when you load their profile the id is put into the <div>.

Javascript gets the id and posts it to the php script and you get an alert to say all is good.

Just a quick example of how you could do it. miniajax.js
0
 
LVL 1

Author Comment

by:helpchrisplz
Comment Utility
if i added a activation field the table so users can click a button to confirm a friendship. how could i do a query that pulls full rows of the friends table were activated = 0

then i can display this information to the user who has received the friend request
0
 
LVL 20

Expert Comment

by:Mark Brady
Comment Utility
how could i do a query that pulls full rows of the friends table were activated = 0:  ??? Pardon me? You want to pull full rows of what exactly? I'm sorry, I don't undestand what you said or what you want to do. Do you want to let the user know that someone has added them as a friend?
0
 
LVL 1

Author Comment

by:helpchrisplz
Comment Utility
you know on facebook where you can add some one but you have to wait till they confirm you. so i was thinking of how to do that.

so was thinking if there was a field in the friends table that was 0 or 1. 0 for if the friend request is not activated and 1 for if it is activated. then i can show not activated requests on a requests.php page and show the activated friend requests on the users profiles.
0
 
LVL 20

Expert Comment

by:Mark Brady
Comment Utility
Ok well you have answered your own question. I would do exactly that, add a 0 to a column named "status" when you do the first insert. You could setup an email to the other user to let them know someone has added them as a friend, then when they click a link in the email, or go to their own page they can click "accept" or "deny". If they click except it will change the 0 to a 1 and if they click "deny" it will delete that entry from the request table. Pretty simple thing to do.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:helpchrisplz
Comment Utility
i would like friend requests to be shown on the users profile.
as in this: it will show users that they are already friends with on the left and then on the right it will show users that have not been confirmed yet.

so the bit am struggling with is this:

once the friend request is in the table what would be the query to get friend requests that have not been activated yet. i can explain it but cant think of the query php.

and am thinking if a user declines a friend request i can delete the row from the table.
0
 
LVL 20

Expert Comment

by:Mark Brady
Comment Utility
Yes you can delete the row from the table. Here is how to get the fiends waiting to be approaved. Assuming you have a basic table like I said above. Change this query to suit your own table names.

<?php // this is the users page

$fid = $_POST['friendid'];
if($_POST['action'] == "approve"){
mysql_query("UPDATE `friends` SET `status` = 1 WHERE `id` = '$fid'")or die(mysql_error());
}
if($_POST['action'] == "decline"){
mysql_query("DELETE FROM `friends` WHERE `id` = '$fid'")or die(mysql_error());
}


$approve = "<form action='this_page.php' method='POST'>
<input type='hidden' name='action' value='approve' />
<input type='hidden' name='friendid' value='$friendID' />
<input type='submit' value='approve' /></form>";

$decline = "<form action='this_page.php' method='POST'>
<input type='hidden' name='action' value='decline' />
<input type='submit' value='decline' /></form>";

$sql = "SELECT name,id FROM `friends` WHERE status = 0";
$result = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$friendID = "{$row['id']}";
$name = "{$row['name']}";
$show_friends_waiting .= "<td>$friendID</td><td>$name</td><td><form action='this_page.php' method='POST'>
<input type='hidden' name='action' value='approve' />
<input type='hidden' name='friendid' value='$friendID' />
<input type='submit' value='approve' /></form></td>
<td><form action='this_page.php' method='POST'>
<input type='hidden' name='action' value='decline' />
<input type='hidden' name='friendid' value='$friendID' />
<input type='submit' value='decline' /></form></td></tr>";
}
$show_friends_waiting .= "</table>";



echo $show_friends_waiting;

// you can put that anywhere on your page.
0
 
LVL 1

Author Closing Comment

by:helpchrisplz
Comment Utility
this is great thx i have been AFK for a bit and only getting this sorted now but thx for this help
elvin66
has Best Solution nice
0
 
LVL 20

Expert Comment

by:Mark Brady
Comment Utility
Thanks for the points and I'm glad you are happy.
0
 
LVL 1

Author Comment

by:helpchrisplz
Comment Utility
elvin66 i am linking to that js script but it is breaking my other jquery scripts and stopping my jquery from showing properly can you help?

<link type="text/css" href="css/ui-darkness/jquery-ui-1.8.10.custom.css" rel="stylesheet" />      
            <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
            <script type="text/javascript" src="js/jquery-ui-1.8.10.custom.min.js"></script>


  <script type="text/javascript" src="js/miniajax.js"></script>

       
<script type="text/javascript">
                  $(function(){

                        // Accordion
                        $("#accordion").accordion({ header: "h3" });
      
                        // Tabs
                        $('#tabs').tabs();
      

                        // Dialog                  
                        $('#dialog').dialog({
                              autoOpen: false,
                              width: 600,
                              buttons: {
                                    "Ok": function() {
                                          $(this).dialog("close");
                                    },
                                    "Cancel": function() {
                                          $(this).dialog("close");
                                    }
                              }
                        });
                        
                        // Dialog Link
                        $('#dialog_link').click(function(){
                              $('#dialog').dialog('open');
                              return false;
                        });

                        // Datepicker
                        $('#datepicker').datepicker({
                              inline: true
                        });
                        
                        // Slider
                        $('#slider').slider({
                              range: true,
                              values: [17, 67]
                        });
                        
                        // Progressbar
                        $("#progressbar").progressbar({
                              value: 20
                        });
                        
                        //hover states on the static widgets
                        $('#dialog_link, ul#icons li').hover(
                              function() { $(this).addClass('ui-state-hover'); },
                              function() { $(this).removeClass('ui-state-hover'); }
                        );
                        
                  });
            </script>
0
 
LVL 20

Expert Comment

by:Mark Brady
Comment Utility
Yes the miniajax.js file is probably clashing with your min.js and/or custom.min.js files. Try adding the miniajx.js file BEFORE the other ones and see if that makes a difference. If not, you might need to just delete the miniajax.js file alltogether and use jquery to do the posting of the data to my php file and not my ajax.post call. Jquery does the same thing but I have not used it so can't help you out but I'm sure you can replace ajax.post("phpfilename.php" blah....blah... with a jquery call. the important thing is to get the user-id to my script so you can do that with jquery instead. Those mini javascript and ajax libraries can clash sometimes.
0
 
LVL 1

Author Comment

by:helpchrisplz
Comment Utility
thx for your comment yes it works now i put it before and no problems
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now