add a friend php

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
Who is Participating?
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
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 != ''){"add-to-friends.php",allDone, 'userid=' + userID);
function allDone(result);
alert('Added to your Friends list');

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
Bruce SmithConnect With a Mentor Software Engineer IICommented:
Well you would set up a table called something like "tblFriends" with the following fields as a concatenated primary key:
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.
helpchrisplzAuthor Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Mark BradyPrincipal Data EngineerCommented:
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?
helpchrisplzAuthor Commented:
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.
Mark BradyPrincipal Data EngineerCommented:
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.
helpchrisplzAuthor Commented:
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.
Mark BradyPrincipal Data EngineerCommented:
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.
helpchrisplzAuthor Commented:
this is great thx i have been AFK for a bit and only getting this sorted now but thx for this help
has Best Solution nice
Mark BradyPrincipal Data EngineerCommented:
Thanks for the points and I'm glad you are happy.
helpchrisplzAuthor Commented:
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">

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

                        // Dialog                  
                              autoOpen: false,
                              width: 600,
                              buttons: {
                                    "Ok": function() {
                                    "Cancel": function() {
                        // Dialog Link
                              return false;

                        // Datepicker
                              inline: true
                        // Slider
                              range: true,
                              values: [17, 67]
                        // 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'); }
Mark BradyPrincipal Data EngineerCommented:
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 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"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.
helpchrisplzAuthor Commented:
thx for your comment yes it works now i put it before and no problems
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.