0

I am trying to create a button that will either say "Follow" or "Unfollow", depending on whether or not the current user follows another user.

If John followed Tim, but not Sarah, the web view would look as follows, according to John's view:

_________________________________
|                |              |
|       Tim      |   (unfollow) |       
|________________|______________|
|                |              |
|       Sarah    |    (follow)  |       
|________________|______________|

Where ("    ") denotes a button.

I have a database that indicates who follows whom, but how would I display the correct button based upon validation with said database?

1
  • 2
    great idea, start writing some code Commented May 10, 2012 at 21:13

2 Answers 2

1

Assuming you have three fields "name_id","name" and "followed" where "name_id" is the id of the person, "name" is a string signifying the name of the person, and "followed" is a boolean:

<script type="text/javascript">
function toggleFollowing(name_id) {
  window.location = 'toggleFollowing.php?name_id='+name_id;
}
</script>
...
<?php
  ...
  while ($row = $result->fetch_assoc()) {
    echo '<tr>';
    echo '<td>'.$row['name'].'</td><td><a href=""><button type="button" onclick="toggleFollowing('.$row['name_id'].')">'.($row['followed']==1 ? 'Unfollow':'Follow').'</button></td>';
    echo '</tr>';
  }
  ...
?>

You would have toggleFollowing.php receive the variable $_GET['name_id'] to toggle on the database and come back to this page. I'm assuming you have the current user's ID stored as a session variable or by other means since you would need that as a primary reference to update the record. If you're passing that from page to page by some other means, then you would want to pass that variable as well.

Apparently, this is more truncated code, but a better method would be to use AJAX to perform the toggling on the DB, and DOM manipulation (JQuery?) for a "real-time" update.

Sign up to request clarification or add additional context in comments.

Comments

0

Hard to answer without examples of your code, but something like this?

<?php

  if(follow){
    echo '<input type="button" value="Follow" />';
  } else {
    echo '<input type="button" value="Unfollow" />';
  }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.