gpt4 book ai didi

php - 在 while 循环中执行函数

转载 作者:行者123 更新时间:2023-11-29 04:50:34 25 4
gpt4 key购买 nike

如果 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

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

我认为问题可能出在这里。早些时候这将返回 true 或 false,但现在即使存在 movement 也只返回 false。这段代码:

$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

感谢大家的帮助!

最佳答案

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

$return = $movement_performed[0] > 0;

if($movement_performed[0] > 0) return true;
else return false;

关于php - 在 while 循环中执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12185072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com