书面说明: 我需要检查测验ID($ quiz_id)是否存在以及测验进度是否为“1”,如果是,则执行一段代码,否则执行其他操作。
有多行数据。我的目的是阻止我的更新脚本发布另一行数据,如果quiz_id存在且progress = 1
更新:代码
add_action('ssquiz_finished', 'after_quiz_done', 10, 5);
function after_quiz_done( $post_id, $quiz_id, $user_id, $questions_right, $total_questions )
{
$postID = $post_id;
$result = $questions_right / $total_questions * 100;
$field_key = "field_52a22a06c7837";
$hello = "user_".$user_id;
$value = get_field($field_key, $hello);
$value[] = array(
"quiz_name" => get_the_title($postID),
"quiz_id" => $quiz_id,
"quiz_score" => $result,
"quiz_progress" => "1"
);
update_field($field_key, $value, $hello); //Script that updates the row (adds rows)
}
非常感谢任何帮助。
答案 0 :(得分:0)
已更新以反映相关新信息。
add_action('ssquiz_finished', 'after_quiz_done', 10, 5);
$quiz_ids = array();
function after_quiz_done( $post_id, $quiz_id, $user_id, $questions_right, $total_questions )
{
if (!in_array($quiz_id, $quiz_ids))
{
if (isset($value['quiz_id') && $value['quiz_progress'] == 1)
{
$postID = $post_id;
$result = $questions_right / $total_questions * 100;
$field_key = "field_52a22a06c7837";
$hello = "user_".$user_id;
$value = get_field($field_key, $hello);
$value[] = array(
"quiz_name" => get_the_title($postID),
"quiz_id" => $quiz_id,
"quiz_score" => $result,
"quiz_progress" => "1"
);
//Everything is good, let's add this quiz_id to the quiz_ids array
$quiz_ids[] = $quiz_id;
update_field($field_key, $value, $hello);
}
else
{
//$quiz_id wasn't set or the quiz progress wasn't equal to 1
}
}
else
{
//fail, because $quiz_id already exists
}
}
答案 1 :(得分:0)
我的理解是,如果设置了quiz_id,则有一组操作,如果没有,则添加新行。尝试
if(isset($value['quiz_id']) //if the quiz id exists in the array
{
if($value['quiz_progress'] == 1){
//exists and is in progress code
else{
//code for exists but isn't in progress yet
}
else
{
//perform code for if there is not entry
}
如果这不起作用,您可能希望澄清问题......