Freddy
Freddy

Reputation: 867

Button to replace another button depending on PHP queries

I need a button to replace another button depending on several PHP if statements. The way it works is that, a user can favourite another user only once, if a user is already favourited, it will display another button rather than the default set of buttons which are available to a non favourited user.

I have a table in my database called favourites. I have inserted one row for testing.

id - 16
favourited_who - Freddy
favourited_by - AliceP

Just for reference, my site at the moment has three users: AliceP, Freddy and Fred. Just to explain, I am logged in as AliceP and and as you can see Fred is not in my favourites, in that case two buttons should be displayed for Fred which are Send Message and Add to Favourites. On the other hand, Freddy is in my favourites, in which case I want to display two buttons, which are Send Message and Remove from Favourites.

With the following code below, the Remove from Favourites button is being shown for both Fred and Freddy, when it shouldn't.

I need the three conditions to be covered in my code, which I believe I have:

// 1. Con 1: Dont display any buttons for the logged in user as they cannot fav or msg themselves.
// 2. Con 2: Display sendmsg and addfriend btn if the user is not in the logged in users favs
// 3. Con 3: Display sendmsg and remfriend btn is the user is in the logged in users favs.

Here is what I have tried:

if (isset($_POST['addfriend'])) { 
    $fav_request    = $_POST['addfriend'];
    $favourited_who = $user; // u variable
    $favourited_by  = $username; // logged in user

    // Get all the favourites of the logged in user...
    $q = mysqli_query ($connect, "SELECT * FROM favourites WHERE favourited_by='$username'");
    $num_of_favs = mysqli_num_rows($q);
        $r_query = mysqli_fetch_array($q);
            $db_fav_who = $r_query['favourited_who'];
            $db_fav_by = $r_query['favourited_by'];

    // Check: See if $user isn't already in there favourites.
    /*if ($db_fav_by == $username){ // if logged in user has anyone favourited
        // if the user already exists in the logged in users favourites, then display remove from favourites button.
        if ($db_fav_who == $user){
            echo "<div class='edit_profile'> 
                <input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
            </div>";
            }
        }*/
        //}// while loop closed
    if ($user != $username) { // Check: See user isnt favouriting themself.
        $favourite_user = mysqli_query($connect, "INSERT INTO favourites VALUES ('', '$favourited_who', '$favourited_by')");
    }
} // if isset closed

// Condition 1:
if ($user == $username){
      // dont display buttons     
} 
// Condition 2:
if ($user != $username && $favourited_who != $db_fav_who){ // if $user is not equal to db fav_who value
    echo "<form method='post'>
                <input type='submit' class='btn btn-info' name='sendmsg' value='Send Message'/>
                <input type='submit' class='btn btn-info' name='addfriend' value='Add to Favourites'>
              </form>";
    }     
// Condition 3:
if ($user != $username && $favourited_who == $db_fav_who) { 
        echo "<form method='post'>
                <input type='submit' class='btn btn-info' name='sendmsg' value='Send Message'/>
                <input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
              </form>";
        }

I have covered all three conditions but I am unable to understand why the code does not show the correct button for each scenario?

Edit:

Found the solution, turns out I had to add the following outside the if isset closed brace:

   $favourited_who = $user; // u variable
    $favourited_by  = $username; // logged in user
    // Get all the favourites of the logged in user...
    $q = mysqli_query ($connect, "SELECT * FROM favourites WHERE favourited_by='$username'");
    $num_of_favs = mysqli_num_rows($q);
        $r_query = mysqli_fetch_array($q);
            $db_fav_who = $r_query['favourited_who'];
            $db_fav_by = $r_query['favourited_by'];

Upvotes: 1

Views: 67

Answers (2)

Michael
Michael

Reputation: 1089

You write while loop closed but where is loop???

may be

while($r_query = mysqli_fetch_array($q)) { ... };

Also you comment one{ and two } in this code

  /*  if ($db_fav_who == $user){
        echo "<div class='edit_profile'> 
            <input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
        </div>";
        }
    }*/

Upvotes: 0

Brian Mc
Brian Mc

Reputation: 151

Do you need a

<form method='post'>

for the following line?

            echo "<div class='edit_profile'> 
            <input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
        </div>";

The other have them.

Upvotes: 0

Related Questions