繁体   English   中英

如果不满足条件,则使用var_dumped正确的值

[英]If condition not being met with the correct values being var_dumped

在下面的代码中,我正在检查$count_friend = $select_friend_stmt->rowCount(); 等于0或1。然后基于结果,我有一个if语句来确定其他元素。 我不明白的是第一个孩子if条件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";
    }

没有运行子if语句。 我认为我已经缩小了问题的范围,但是不确定如何解决。 我不认为if ( $count_friend == 1 ) {是问题,因为在echo var_dump($count_friend) . "Count Friend"; echo var_dump($count_friend) . "Count Friend"; 我得到正确的整数。

现在,当我转到$friend_status = 1;的用户时$friend_status = 1; 我从var_dump()的以下输出

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

string(1) "1"对我造成了问题吗?

我尝试做: if ($friend_status == "1") { ..和.. if ($friend_status == '1') { ,但是它没有帮助。

我的status列在数据库中的配置如下:

status枚举('0','1','2')COLLUT utf8_unicode_ci默认'0'

为什么我的代码没有为我的if ($friend_status的结果产生结果?

$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.";
    }

关于isset的事情是,如果该值为null ,它将返回false

如果var存在并且具有非NULL的值,则返回TRUE。 否则为FALSE。

所以这只是声明了变量,但它仍然为null

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

因此isset($friend_status_approved)将始终返回false

您应该将其设置为true

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

此外,通过进行以下比较,您迟早会遇到麻烦:

if ($friend_status == 1) {}

这是因为这是一个松散的比较 ,它将把所有事物隐式转换为真实值。 最好使用filter_var验证整数并使用相同的比较===

$friend_status = filter_var($friend_status, FILTER_VALIDATE_INT);
if ($friend_status === 1) {
    $friend_status_pending = true;
}

好像OP在调试此功能时有刺痛-这只是FYI的一部分。

我从这里拿走了数据库的东西,所以我可以创建一些测试数据并消除潜在的问题。

如果这不完全符合OP的要求或不属于主题,那么我深表歉意。

所有这些都在一个文件中完成,并且为了便于阅读而进行了细分。

设置测试数据

/**
 * 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
    ]
];

创建测试功能。

// 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);
}

执行测试用例。

// 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

这将为您测试代码提供一些基础,这就是我以前对其进行重构的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM