1

我有这个函数,如果存在则返回 bool(true) 或 bool(false) movement

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   

我在这里尝试做的是每次循环检查函数 move_performed_today 是真还是假。如果返回 false 则将此 _POST 数据插入到数据库中。不确定我是否正确执行此操作,因为即使该函数返回 true,它仍会调用 completed_movement 函数并将信息发布到 db。

我相信问题可能就在这里。早些时候这将返回真或假,但现在即使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   

感谢大家的帮助!

4

2 回答 2

3

我可能误读了这一点,但看起来您将 $return 设置为 true 或 false,而不是实际返回值。我相信你想改变:

$return = $movement_performed[0] > 0;

if($movement_performed[0] > 0) return true;
else return false;
于 2012-08-29T19:28:55.733 回答
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);
}
于 2012-08-29T19:28:43.663 回答