background_1

System zum Folgen mit PHP

von pathologicalplay am

Ich habe für mein neustes Projekt ein einfaches System mit PHP umgesetzt, bei dem Nutzer anderen Nutzern folgen können.


CREATE TABLE `users` (
`user_id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci

CREATE TABLE `follows` (
`follow_id` int(10) NOT NULL AUTO_INCREMENT,
`follower` int(10) NOT NULL,
`followed` int(10) NOT NULL,
PRIMARY KEY (`follow_id`),
KEY `follower` (`follower`),
KEY `followed` (`followed`),
CONSTRAINT `follows_ibfk_1` FOREIGN KEY (`follower`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `follows_ibfk_2` FOREIGN KEY (`followed`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci

Ausgabe derjenigen, dem ein Nutzer folgt.


$profile_owner_id = '';
if (isset($_GET['id'])) {
$profile_owner_id = (INT) $_GET['id'];
}

$view_follower = $db->prepare("SELECT follows.follower, follows.followed, users.username, users.user_id
FROM follows
LEFT JOIN users ON follows.followed = users.users_id
WHERE follows.follower = ?");
$view_follower->bind_param('i', $profile_owner_id);
$view_follower->execute();
$view_follower->store_result();
$view_follower->bind_result($follower, $followed, $name_of_followed_user, $id_of_followed_user);
if ($view_follower->num_rows != 0) {
echo "<ul>";
while ($view_follower->fetch()) {
echo '<li>'. '<a href="view_profile.php?id='. $id_of_followed_user .'">'.
htmlspecialchars($name_of_followed_user) .'</a>'. '</li>';
}
echo "</ul>";
} else {
echo "The user doesn't follow anyone yet.";
}
$view_follower->close();

Dann müssen wir noch überprüfen, ob ein Nutzer schon einem anderen Nutzer folgt, um die Schaltfläche zum Folgen oder Entfolgen einzublenden.


if (isset($_SESSION['logged_in']['user_id'])) {
$follower = (INT) $_SESSION['logged_in']['user_id'];
}

$follower_status = $db->prepare("SELECT COUNT(*)
FROM follows WHERE follower = ? AND followed = ?");
$follower_status->bind_param('ii', $follower, $profile_owner_id);
$follower_status->execute();
$follower_status->bind_result($result);
$follower_status->fetch();

if (isset($_SESSION['logged_in']['user_id'])
and $_SESSION['logged_in']['user_id'] != $profile_owner_id and $result == 0) {
echo '<form class="profile_follows_submit" action="" method="post">
<input type="hidden" name="act" value="FOLLOW">
<input type="submit" value="FOLLOW"></form>';
}
if (isset($_SESSION['logged_in']['user_id'])
and $_SESSION['logged_in']['user_id'] != $profile_owner_id and $result == 1) {
echo '<form class="profile_follows_submit" action="" method="post">
<input type="hidden" name="act" value="UNFOLLOW">
<input type="submit" value="UNFOLLOW"></form>';
}
$follower_status->close();

Zuletzt folgt die Anweisung, um die Beziehung zu speichern oder zu entfernen.


if (isset($_POST['act']) and $_POST['act']  == 'FOLLOW') {

if (isset($_SESSION['logged_in']['user_id'])) {
$follower = (INT) $_SESSION['logged_in']['user_id'];
}

$followed = '';
if (isset($_GET['id'])) {
$followed = (INT) $_GET['id'];
}

$save_follower = $db->prepare("INSERT INTO follows(follower, followed) VALUES(?, ?)");
$save_follower->bind_param('ii',$follower, $followed);
$save_follower->execute();
}

if (isset($_POST['act']) and $_POST['act']  == 'UNFOLLOW') {

if (isset($_SESSION['logged_in']['user_id'])) {
$follower = (INT) $_SESSION['logged_in']['user_id'];
}

$followed = '';
if (isset($_GET['id'])) {
$followed = (INT) $_GET['id'];
}

$unfollow = $db->prepare("DELETE FROM follows WHERE follower = ? AND followed = ? LIMIT 1");
$unfollow->bind_param('ii', $follower, $followed);
$unfollow->execute();
}