rnmembers.php

Using Example 20-10, rnmembers.php, your users will be able to find other members and choose to add them as friends (or drop them if they are already friends). This program has two modes. The first lists all members and their relationships to you, and the second shows a user’s profile.

Viewing a User’s Profile

The code for the latter mode comes first, where a test is made for the GET variable 'view'. If it exists, a user wants to view someone’s profile, so the program does that using the showProfile function, along with providing a couple of links to the user’s friends and messages.

Adding and Dropping Friends

After that the two GET variables 'add' and 'remove' are tested. If one or the other has a value, it will be the username of a user to either add or drop as a friend. This is achieved by looking the user up in the MySQL rnfriends table and either inserting a friend username or removing it from the table.

And, of course, every posted variable is first passed through sanitizeString to ensure it is safe to use with MySQL.

Listing All Members

The final section of code issues a SQL query to list all usernames. The code places the number returned in the variable $num before outputting the page heading.

A for loop then iterates through each and every member, fetching their details and then looking them up in the rnfriends table to see if they are either being followed by or a follower of the user. If someone is both a follower and a followee, they are classed as a mutual friend. ...

Get Learning PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.