如果movement
存在,我有这个函数返回bool(true)或bool(false)。
function movement_performed_today($class_id, $client_id){
$class_id = (int)$class_id;
$client_id = (int)$client_id;
$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `date` = CURDATE()");
$movement_performed = mysql_fetch_row($query);
return ($movement_performed[0] > 0);
}
我有这个while循环,我想调用这个函数,如果它返回false,执行函数completed_movement
。我尝试了很多不同的方法,但我似乎无法让它发挥作用。任何信息将不胜感激。
if (empty($_POST)=== false){
$i = 0;
while (isset($_POST["first_name"][$i])) {
$movement_data = array(
'user_id' => $session_user_id,
'class_id' => $class_id,
'class_name' => $class_name,
'client_id' => $_POST['client_id'][$i],
'first_name' => $_POST['first_name'][$i],
'last_name' => $_POST['last_name'][$i],
'nickname' => $_POST['nickname'][$i],
'order' => $_POST['order'][$i],
'movement' => $_POST['movement'][$i],
'rep_set_sec' => $_POST['rep_set_sec'][$i],
'rest' => $_POST['rest'][$i],
'date' => $today
);
//assign variable to the function
$isPerformed = movement_performed_today($class_id, $_POST['client_id'][i]);
//if return false perform this function
if(! $isPerformed){ completed_movement($movement_data);}
$i++;
}
} // if empty
我在这里尝试做的是每次循环检查以查看函数moving_performed_today是真还是假。如果返回false,则将此_POST数据插入db。不确定我是否正确执行此操作,因为即使该函数返回true,它仍然会调用completed_movement函数并将信息发布到db。
我认为这个问题可能就在这里。之前这会返回true或false但现在只是返回false,即使movement
存在。这段代码:
$movement_performed = mysql_fetch_row($query);
//return ($movement_performed[0] > 0);
var_dump($movement_performed);
返回:
array(1) { [0]=> string(1) "0" }
array(1) { [0]=> string(1) "0" }
如果movement
存在,则“0”不应为“1”或更高,具体取决于与查询匹配的次数。
以下是工作查询:
function movement_performed_today($class_id, $client_id, $movement){
$class_id = (int)$class_id;
$client_id = (int)$client_id;
$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `movement` = '$movement' AND `date` = CURDATE()");
$movement_performed = mysql_fetch_row($query);
return ($movement_performed[0] > 0);
}
这是工作while循环。
if (empty($_POST)=== false){
$i = 0;
while (isset($_POST["first_name"][$i])) {
$movement_data = array(
'user_id' => $session_user_id,
'class_id' => $class_id,
'class_name' => $class_name,
'client_id' => $_POST['client_id'][$i],
'first_name' => $_POST['first_name'][$i],
'last_name' => $_POST['last_name'][$i],
'nickname' => $_POST['nickname'][$i],
'order' => $_POST['order'][$i],
'movement' => $_POST['movement'][$i],
'rep_set_sec' => $_POST['rep_set_sec'][$i],
'rest' => $_POST['rest'][$i],
'date' => $today
);
//check not already performed today
$isPerformed = movement_performed_today($class_id, $_POST['client_id'][$i], $_POST['movement'][$i]);
//if not performed then do insert
if (! $isPerformed){ completed_movement($movement_data);}
$i++;
}
} // if empty
谢谢大家的帮助!
答案 0 :(得分:3)
我可能会误读这个,但看起来你将$ return设置为true或false,而不是实际返回值。我相信你想改变:
$return = $movement_performed[0] > 0;
到
if($movement_performed[0] > 0) return true;
else return false;
答案 1 :(得分:1)
编辑:
听起来你的SQL查询也有问题。尝试:
$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = $class_id AND `client_id` = $client_id AND `date` = CURDATE()");
由于列class_id和client_id似乎是整数,因此请删除其值周围的单引号。
编辑:更正语法,首先将SQL结果转换为整数。
return ((int)$movement_performed[0] > 0 ? true : false);
如果使用此返回行,函数completed_movement()
将仅返回布尔值。
在这种情况下,您希望在进行调用函数的比较逻辑时要严格。
你想要这样做:
//if return false perform this function
if ($isPerformed === false) {
completed_movement($movement_data);
}