Nameless icon indicating copy to clipboard operation
Nameless copied to clipboard

PHP Output number of friends, "Add friend" & "Remove friend" button + output who are friends.

Open zTryhqrd opened this issue 10 years ago • 20 comments

  • What kind of issue is it (Question, Bug, etc.)? Question
  • In which version of NamelessMC did it occur (1.x.x)? 1.0.10

Hi @samerton, I have another question :stuck_out_tongue_closed_eyes: I am currently busy with my own profile page, but I need to echo the number of friends that a player has and also what friends he has. Example: https://i.gyazo.com/38796296e8efd317b896e5469b7fd59c.png

- But I do not know what files I need to include and what code I need to use to output the "Add Friend" button and the "Remove Friend" button and the number of friends and ofcourse what players are friends with him.

Can you please help me?

zTryhqrd avatar Jul 26 '16 09:07 zTryhqrd

Is your custom page within the pages directory?

If so, you can use the following functions:

  • $user->addfriend($user->data()->id, $other_user_id) to add friends
  • $user->removefriend($user->data()->id, $other_user_id) to remove friends,
  • $user->listFriends($user_id) to obtain an array containing IDs of the $user_id's friends
  • $user->isfriend($user1, $user2) to see if $user1 is friends with $user2

If your page isn't within the pages directory, you'll need to query the "nl1_friends" table to obtain the information from there.

samerton avatar Jul 26 '16 15:07 samerton

Hey dude, Thanks bro that you help me so much! Can you maybe give me an example code how to use all that stuff? Like the add and remove friend button and an output of friends with head images and friend counter etc. :D

Op 26 jul. 2016 om 17:16 heeft Sam [email protected] het volgende geschreven:

Is your custom page within the pages directory?

If so, you can use the following functions:

$user->addfriend($user->data()->id, $other_user_id) to add friends $user->removefriend($user->data()->id, $other_user_id) to remove friends, $user->listFriends($user_id) to obtain an array containing IDs of the $user_id's friends $user->isfriend($user1, $user2) to see if $user1 is friends with $user2 If your page isn't within the pages directory, you'll need to query the "nl1_friends" table to obtain the information from there.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

zTryhqrd avatar Jul 26 '16 15:07 zTryhqrd

And if I make a profile.php in pages/, I can use https://example.com/profile/test

But what $_GET['??'] do I have to use if I want to use the parameters after the /profile/

So I can use /profile/Samerton, what $_GET is that?

Because this only works: https://www.example.com/profile/?username=zTryhqrd

It doesnt work if I try: https://www.example.com/profile/zTryhqrd

Op 26 jul. 2016 om 17:16 heeft Sam [email protected] het volgende geschreven:

Is your custom page within the pages directory?

If so, you can use the following functions:

$user->addfriend($user->data()->id, $other_user_id) to add friends $user->removefriend($user->data()->id, $other_user_id) to remove friends, $user->listFriends($user_id) to obtain an array containing IDs of the $user_id's friends $user->isfriend($user1, $user2) to see if $user1 is friends with $user2 If your page isn't within the pages directory, you'll need to query the "nl1_friends" table to obtain the information from there.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

zTryhqrd avatar Jul 26 '16 15:07 zTryhqrd

It might be a bit more beneficial to see the code in action, lines 451-475 of pages/profile.php will check to see if the logged in user is a friend of the profile user, and will display a form (this is required to prevent spam clicking) containing an add or remove friend button. Lines 78-87 will actually add or remove the user as a friend.

Lines 388-411 will display a list of friends, and you can count the number of items in the array to display the number.

As for the URL username parameter, this is done by the index.php file within the root directory, if you take a look at lines 83-89 you can modify that code to your liking (the $profile variable will contain the username).

samerton avatar Jul 26 '16 17:07 samerton

Yea I know that it shows the 'profile' things from 83-89.. But this is only working: https://www.example.com/profile/?username=zTryhqrd

It doesnt work if I try: https://www.example.com/profile/zTryhqrd

zTryhqrd avatar Jul 26 '16 18:07 zTryhqrd

@samerton, please check your e-mail, I wrote it a little bit better there.

zTryhqrd avatar Jul 27 '16 07:07 zTryhqrd

Hey dude, I think you understood me wrong on GitHub, becuase you said that you can change the 'profile' thing to like http://example.com/user/ but that's not where I was looking for. The problem is, when I make a custom profile page, it is not reachable by https://www.example.com/profile/Name Because I am using $_GET["username"] so my custom profile is only reachable with the following link: https://example.com/profile/?username=zTryhqrd, so do you know how I can fix that? That I can use http://example.com/profile/Name for my $_GET["username"]

I hope you understand me :)

Regards, zTryhqrd

zTryhqrd avatar Jul 27 '16 10:07 zTryhqrd

@zTryhqrd Try using the $profile variable instead of $_GET['username']

samerton avatar Jul 27 '16 23:07 samerton

Okay, it's working but I have the friends problem now, because if you take a look at https://shadowhcf.com/profile/zTryhqrd , it'll show a "Friends" list (https://i.gyazo.com/f1d07d43912699e502bafadba02b684e.png), but this is actually just HTML, I have no idea how to implement the friend stuff to my custom profile script, here is my custom profile script: http://hastebin.com/nemayipiwa.php (the code)

Because I want to add a friend list with the heads and a little friendcounter and a "Add Friend" & "Remove Friend" button aswell ;/

Do you know what kinda scripts I need to include or what I have to do.. I have no idea how to implement the friends to my php script.

zTryhqrd avatar Jul 28 '16 07:07 zTryhqrd

Hello?

zTryhqrd avatar Jul 28 '16 14:07 zTryhqrd

Seems like you need a id2username. And username2id function for the dynamic content.

On Jul 28, 2016 7:25 AM, "zTryнqrd ツ" [email protected] wrote:

Hello?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/NamelessMC/Nameless/issues/356#issuecomment-235910132, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGSZU70pM_6HqhLnFIrwZmCiGe1Kyr0ks5qaLu2gaJpZM4JU83F .

WASasquatch avatar Jul 28 '16 18:07 WASasquatch

@WASasquatch Don't understand your comment. @samerton =

Okay, it's working but I have the friends problem now, because if you take a look at https://shadowhcf.com/profile/zTryhqrd , it'll show a "Friends" list (https://i.gyazo.com/f1d07d43912699e502bafadba02b684e.png), but this is actually just HTML, I have no idea how to implement the friend stuff to my custom profile script, here is my custom profile script: http://hastebin.com/nemayipiwa.php (the code)

Because I want to add a friend list with the heads and a little friendcounter and a "Add Friend" & "Remove Friend" button aswell ;/

Do you know what kinda scripts I need to include or what I have to do.. I have no idea how to implement the friends to my php script.

zTryhqrd avatar Jul 28 '16 18:07 zTryhqrd

To list users, simply use the $user->listFriends($user_id) function to obtain a list of friends for the user (with id $user_id).

Example usage is as seen in this gist.

(pretty much taken straight from the default profile.php).

Hope that helps.

samerton avatar Jul 29 '16 17:07 samerton

Thanks dude, that's helped alot, last question: what files do I need to include at the top of my custom php page to make all the friends stuff working?

zTryhqrd avatar Jul 29 '16 19:07 zTryhqrd

I'm assuming you would need to create your own action page, or integrate it in your custom page.

<?php

require_once('core/integration/uuid.php');

if($user->isLoggedIn()){

    require('core/includes/htmlpurifier/HTMLPurifier.standalone.php');

    if(!isset($_POST['action'])){
        if(isset($_POST['AddFriend'])) {
            if(Token::check(Input::get('token'))){
                $user->addfriend($user->data()->id, $profile_user[0]->id);
                header('location: '.$_SERVER['HTTP_REFERER']);
            }
        }
        if(isset($_POST['RemoveFriend'])){
            if(Token::check(Input::get('token'))){
                $user->removefriend($user->data()->id, $profile_user[0]->id);
                header('location: '.$_SERVER['HTTP_REFERER']);
            }
        }
    } else {
        header('location: '.$_SERVER['HTTP_REFERER']);
    }

?>

The variable $profile_user[0]->id could be made to be a form input which is the target users ID, or gathered from the page if available. I don't use the system like you so just a vague guess.

WASasquatch avatar Jul 30 '16 03:07 WASasquatch

To use the functions included with Nameless you need to have your custom PHP file within the pages directory, and it will load everything automatically.

samerton avatar Jul 31 '16 07:07 samerton

My whole friend system is weird right now, I'll send you a hastebin and you can check it. It kinda begins at line 589..

http://hastebin.com/rulapigode.php

But <?php if(!$exists || $friends == false) echo '0'; else echo count($friends); ?> returns at '0' because it doesn't know any user to check it from, because I added 1 friend, and it still counts as 0. So I think that the system don't recognize a user or it doesnt know from what user to check.

Please check it for me from my hastebin.

zTryhqrd avatar Jul 31 '16 08:07 zTryhqrd

Sorry for the late reply.

I've taken a look, and you simply need to add the following anywhere before your friends section :

<?php // Get friends $friends = $user->listFriends($profile_user[0]->id); ?>

You'll also need to replace <?php if(!$exists || $friends == false) echo '0'; else echo count($friends); ?> with <?php if($friends == false) echo '0'; else echo count($friends); ?>.

Hope that helps.

samerton avatar Aug 11 '16 17:08 samerton

Doesn't work.. ;/ Still returns at '0'

zTryhqrd avatar Aug 12 '16 10:08 zTryhqrd

Apologies for not being able to discover what the issue was, please feel free to re-open this if you're still looking for help on this.

samerton avatar Jun 29 '17 13:06 samerton