Reputation: 2277
I creating a twitter look a like platform for studying purpose, I have reach to the follow and unfollow part.
I have two buttons, the follow and unfollow button, they both works fine If i press follow it insert session ID and the id of the user i want to follow. If i press unfollow it removes that row. all good.
I have now added ad if else statement to the buttons so if you press the follow button it changes to unfollow and by pressing the unfollow it changes back to follow.
The problem is that since the if else works depending if the row is empty or not, it gives me back the same results for every logged in user. Maybe there is a better way to show the buttons ?
This is what I have for now.
<?php foreach(fetch_users($_GET['uid'])as $user){
if(empty($user['follow_id'])){ ?> <form action="" method='POST'>
<button type="submit" class="follow" name="submit" value="<?php echo $user['id'] ?>"/>FOLLOW</button>
</form>
<?php } else{ ?>
<form action="" method='POST'>
<button type="submit" class="follow" name="delete" value="<?php echo $user['id'] ?>"/>UNFOLLOW </button>
</form>
<?php
}
?>
</form>
</div>
<?php } ?>
function fetch_users($uid){
global $db;
$query = $db->query("SELECT user.id, user.username, user.email, userdetails.profile_img, following.follow_id
FROM user
JOIN userdetails ON user.id = userdetails.user_id
LEFT JOIN following ON user.id = following.follow_id
WHERE user.id != '{$uid}' ");
$user = array();
while(($row = $query->fetch(PDO::FETCH_ASSOC)) !==FALSE) {
$user[] = $row;
}
return $user;
}
Upvotes: 0
Views: 59
Reputation: 157880
<?php foreach(fetch_users($_GET['uid']) as $user): ?>
<?php $butt = $user['follow_id'] ? 'UNFOLLOW' : 'FOLLOW'; ?>
<form method='POST'>
<button type="submit" class="follow" name="<?=$butt?>" value="<?=$user['id']?>">
<?=$butt?>
</button>
</form>
<?php endforeach ?>
<?
function fetch_users($uid){
global $db;
$query = "SELECT user.id, username, email, profile_img, follow_id
FROM user
JOIN userdetails ON user.id = userdetails.user_id
LEFT JOIN following ON user.id = following.follow_id
WHERE user.id != ?";
$stm = $db->prepare($query);
$stm->execute(array($uid));
return $stm->fetchAll();
}
Upvotes: 1