在下面的代码中,我正在检查$count_friend = $select_friend_stmt->rowCount();
是否等于0或1.然后根据结果,我有一个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";
下我得到了正确的整数。
现在,当我找到$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
enum('0','1','2')COLLATE utf8_unicode_ci DEFAULT'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.";
}
答案 0 :(得分:0)
关于isset
的问题是,如果值为false
,它将返回null
如果var存在并且值不是NULL ,则返回TRUE。否则就错了。
所以这只是声明变量,但它仍然是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) {}
那是因为it's a loose comparison会隐含地将所有类型的事物投射到真正的价值观中。最好使用filter_var
验证整数,并使用相同的比较===
$friend_status = filter_var($friend_status, FILTER_VALIDATE_INT);
if ($friend_status === 1) {
$friend_status_pending = true;
}
答案 1 :(得分:0)
看到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
这将为您提供一些测试代码的基础,这就是什么 我过去常常重构一下。