0

In the code below I am checking if $count_friend = $select_friend_stmt->rowCount(); equates to 0 or 1. Then based on the result, I have an if statement to determine other elements. What I do not understand is the first child if condition if ($friend_status == 2) {:

    if ( $count_friend == 1 ) {
        //echo $count_friend;
        if ($friend_status == 2) {
            $friend_status_approved;
        }
        if ($friend_status == 1) {
            $friend_status_pending;
        }
        if (isset($friend_status_approved)) {
            $friend_status_button = "Approved";
        }
        else if (isset($friend_status_pending)) {
            $friend_status_button = "Pending";
        }
       echo var_dump($friend_status) . "*1 - status";
    }

Is not running the child if statements. I think I have narrowed down the issue, but I am unsure of how to fix it. I do not think, if ( $count_friend == 1 ) { is the issue because under echo var_dump($count_friend) . "Count Friend"; I get the correct integer.

Now, when I go to a user who's $friend_status = 1; I get the following output from my var_dump()'s

int(1) Count 
Friendstring(1) "1"
*1 - statusstring(1) "1"
*0 - status

Is string(1) "1" causing issues for me?

I have tried doing: if ($friend_status == "1") { .. and .. if ($friend_status == '1') { , but it did not help.

My status column is configured in my database like the following:

status enum('0','1','2') COLLATE utf8_unicode_ci DEFAULT '0'

Any ideas why my code is not producing results for my if ($friend_status 's?

$okay = true;
$friend_status_button = null;
$profile_viewer_message = null;
    //Checking to see if $user_id and $profile_user IS listed to allow $friend_status conditions to work for them below
    $friend_sql = "
        SELECT *
        FROM friends
        WHERE friend_one = ?
        AND friend_two= ?
    ";
    $select_friend_stmt = $con->prepare($friend_sql);
    $select_friend_stmt->execute(array($user_id, $profile_user));
    $friend_rows = $select_friend_stmt->fetchAll(PDO::FETCH_ASSOC);
    $count_friend = $select_friend_stmt->rowCount();
    foreach ($friend_rows as $friend_row) {
        $select_friend_1    = $friend_row['friend_one'];
        $select_friend_2    = $friend_row['friend_two'];
        $friend_status      = $friend_row['status'];
        $friend_status_date = $friend_row['date'];
    }

//Query to check if a friend request was sent

    //checking occurrence in db
    if ( $count_friend === 1 ) {
        //echo $count_friend;
        if ($friend_status === 2) {
            $friend_status_approved = true;
        }
        if ($friend_status === 1) {
            $friend_status_pending = true;
        }
        if (isset($friend_status_approved)) {
            $friend_status_button = "Approved";
        }
        if (isset($friend_status_pending)) {
            $friend_status_button = "Pending";
        }
        //echo var_dump($friend_status) . "*1 - status";
    }
    else if ( $count_friend === 0 ) {
        $friend_status = 0;
        $select_friend_2 = 0;
        if ( $user_id == $profile_user ) {
            $friend_status_my_profile = true;
        }
        else if ($friend_status == $select_friend_2) {
            $friend_status_norelate = true;
        }
        if (isset($friend_status_my_profile)) {
            $friend_status_button = "";
            $profile_viewer_message = "This is your profile.";
        }
        if (isset($friend_status_norelate)) {
            $friend_status_button = '<div id="add-friend"><img src="../icons/collection/add.png" alt="Add Friend">' . "Add Friend" . '</div>';
        }
    }
    else {
        echo "Friend Status not found.";
    }
Paul
  • 3,348
  • 5
  • 32
  • 76

2 Answers2

0

The thing about isset is that it will return false if the value is null

Returns TRUE if var exists and has value other than NULL. FALSE otherwise.

So this just declares the variable, but it's still null

    if ($friend_status == 2) {
        $friend_status_approved;
    }
    if ($friend_status == 1) {
        $friend_status_pending;
    }

Hence isset($friend_status_approved) will always return false

You should set those to true

    if ($friend_status == 2) {
        $friend_status_approved = true;
    }
    if ($friend_status == 1) {
        $friend_status_pending = true;
    }

Furthermore, you'll get yourself into trouble sooner or later by making this comparison:

if ($friend_status == 1) {}

That's because it's a loose comparison that will implicitly cast all kinds of things to truthy values. Best to validate the integer with filter_var and use an identical comparison ===

$friend_status = filter_var($friend_status, FILTER_VALIDATE_INT);
if ($friend_status === 1) {
    $friend_status_pending = true;
}
Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • There is also the case where $friend_status and $select_friend_2 are not defined when $friend_rows comes back as an empty array. That also needs to be dealt with. – TimBrownlaw Nov 19 '16 at 04:24
0

Seeing as the OP has had a stab at debugging this - this is just more of an FYI.

I've taken the DB stuff out of the picture here so I can create some test data and removes one level of potential issues.

If this isn't quite up to what the OP has or is considered off topic, then my apologies.

This was all done in the one file and is segmented for ease of reading.

Setting up of the test data

/**
 * Mock up of Database Data - at a guess.
 */
$db_array = [
    [
        [ // 1 Friend - Status = 1
            'friend_one' => 1,
            'friend_two' => 2,
            'status'     => 1,
            'date'       => '2016-11-20 10:10:10'
        ]
    ],
    [
        [ // 1 Friend - Status = 2
            'friend_one' => 1,
            'friend_two' => 2,
            'status'     => 2,
            'date'       => '2016-11-20 10:10:10'
        ]
    ],
    [
        // No Friend
    ]
];

Creating the test function.

// As the test code is defined in a function, this has no return values
// defined so it's just a container for testing the code in question.
function get_friend_status(array $friend_rows) {
    // replaces current Database for testing
    $friend_exists        = count($friend_rows);
    $friend_status_button = null;
    $friend_status        = 0;
// We always get an array, even if it is empty
    foreach ( $friend_rows as $friend_row ) {
        echo '<br>' . __LINE__ . ' Inside Foreach Loop';
        $select_friend_1    = $friend_row['friend_one'];
        $select_friend_2    = $friend_row['friend_two'];
        $friend_status      = $friend_row['status'];
        $friend_status_date = $friend_row['date'];
    }

    $friend_status = (int) $friend_status;
    if($friend_exists === 1) {
        if($friend_status === 2) {
            $friend_status_button = "Approved";
        }
        else if($friend_status === 1) {
            $friend_status_button = "Pending";
        } else {
            // Don't need this as $friend_status_button
            // is already set to a default value
        }

    } else {
        echo "<br>Friend Status not found.";
    }
    // Debug
    var_dump('Line: ' . __LINE__ . ' - Count Friend ' . $friend_exists);
    var_dump('Line: ' . __LINE__ . ' - Friend Status ' . $friend_status);
    var_dump('Line: ' . __LINE__ . ' - Friend Status Button - ' . $friend_status_button);
}

Performing the test cases.

// Debug
echo 'Test 1- 1 Friend - Status 1';
get_friend_status($db_array[0]); // Test Case 1 - 1 Friend, Status = 1
echo 'Test 2- 1 Friend - Status 2';
get_friend_status($db_array[1]); // Test Case 2 - 1 Friend, Status = 2
echo 'Test 3- 0 Friends';
get_friend_status($db_array[2]); // Test Case 2 - No result

And that will give you a bit of play ground to test out your code, which is what I used to refactor this a little.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
  • I appreciate your attempt. I have altered my code some. I will update my question momentarily with the current code. Please note, my database structure will never have the same two id's in a row at once. IE `friend1` = 2 ... `friend2` = 8. This will be the only occurrence of their relation. I simply update the status of 1 (pending) to 2 (friend). Here is my database structure. http://sqlfiddle.com/#!9/ef792f – Paul Nov 19 '16 at 05:25
  • You still have some unnecessary code in your $count_friend === 1 case. And you'll be better off if you make friend_one and friend_two into indexes in your DB table. – TimBrownlaw Nov 19 '16 at 05:38
  • I don't understand why I would be better off doing that? That is creating more records for no reason. Say I had 10,000 records my way, with your way it would be 20-30,000. Seems like it would kill the performance....Do you see anything that could be preventing this from working? – Paul Nov 19 '16 at 05:41
  • Well I would take a look at http://dev.mysql.com/doc/refman/5.7/en/mysql-indexes.html and you can decide. – TimBrownlaw Nov 19 '16 at 05:45