Php While循环不在函数后运行
我有一个while循环,用于检查数据库中状态为1的通知。然后,它向OneSignal.com的API发送通知,以便用户获得通知 问题是,我试图运行脚本,以便它在执行while循环时处理状态为1的所有记录,但一旦我包含OneSignal函数,while循环将在仅处理一条记录后停止 我弄不明白,我试着移动东西,当我把send message函数放在while循环之外时,用户ID的php变量没有传递给它 我该怎么办Php While循环不在函数后运行,php,function,while-loop,Php,Function,While Loop,我有一个while循环,用于检查数据库中状态为1的通知。然后,它向OneSignal.com的API发送通知,以便用户获得通知 问题是,我试图运行脚本,以便它在执行while循环时处理状态为1的所有记录,但一旦我包含OneSignal函数,while循环将在仅处理一条记录后停止 我弄不明白,我试着移动东西,当我把send message函数放在while循环之外时,用户ID的php变量没有传递给它 我该怎么办 <?php require('connection.inc.php');
<?php
require('connection.inc.php');
$notification = mysqli_query($con, "SELECT *, t1.id AS NotifID FROM notification AS t1 LEFT JOIN user AS t2 ON t1.action_user_id = t2.id WHERE status = '1' ORDER BY time_updated DESC") or die(mysql_error());
//GET NOTIFCATION RECORD
while ($row = mysqli_fetch_assoc($notification)) {
$notificationID = $row['NotifID'];
$type = $row['notification_type'];
$action_user_id = $row['action_user_id'];
$action_user_name = $row['name'];
$action_user_profile = empty($row['profile_image']) ? '/instachurch/images/profile/profile.png' : '/instachurch/' . $row['profile_image'];
$time = $row['time_updated'];
$post_id = $row['post_id'];
$notify_user_id = $row['notify_user_id'];
// GET NOTIFY USER INFO
$GetUserName = mysqli_query($con, "SELECT * FROM user WHERE id = $notify_user_id ") or die(mysql_error());
while ($row = mysqli_fetch_assoc($GetUserName)) {
$username = $row['name'];
$userMob = $row['mob'];
$userHash = $row['hash'];
$OneSignalPushID = $row['id'];
if ($type == "post_liked")
$message = $action_user_name . " liked your post.";
else if ($type == "post_comment")
$message = $action_user_name . " commented on your post.";
else if ($type == "post_comment_reply")
$message = $action_user_name . " replied to your comment.";
else if ($type == "post_answer")
$message = $action_user_name . " answered to your question.";
else if ($type == "follow_user")
$message = $action_user_name . " started following you.";
else if ($type == "discussion_topic_comment")
$message = $action_user_name . " commented on your forum topic.";
else if ($type == "timeline_mention")
$message = $action_user_name . " mentioned you in a comment.";
else if ($type == "school_lesson_mention" || $type == "school_discussion_mention")
$message = $action_user_name . " mentioned you in a comment on discussion.";
if ($type == "post_comment" || $type == "post_liked" || $type == "post_comment_reply" || $type == "post_answer" || $type == "timeline_mention")
$link = "http://www.gypsychristiannetwork.com/instachurch/post.php?post_id=" . $post_id . '&hash=' . $userHash;
else if ($type == "follow_user")
$link = "http://www.gypsychristiannetwork.com/instachurch/users.php?id=" . $action_user_id . '&hash=' . $userHash;
else if ($type == "discussion_topic_comment")
$link = "http://www.gypsychristiannetwork.com/tgcm/school/account.php?q=24&topic=" . $post_id . '&hash=' . $userHash;
else if ($type == "school_lesson_mention")
$link = "http://www.gypsychristiannetwork.com/tgcm/school/account.php?q=11&qid=" . $post_id . '&hash=' . $userHash;
else if ($type == "school_discussion_mention")
$link = "http://www.gypsychristiannetwork.com/tgcm/school/account.php?q=24&topic=" . $post_id . '&hash=' . $userHash;
}
$q3 = mysqli_query($con, "UPDATE `notification` SET `status` = '2' WHERE `id` = '$notificationID'");
echo $notificationID . ':' . $message;
// echo $link;
$userPushID = $OneSignalPushID;
// echo $userPushID.'<BR><P>';
$heading = 'Check your Notifications';
$content = array(
"en" => $message
);
$heading = array(
"en" => $heading
);
$fields = array(
'app_id' => "APPID",
'include_external_user_ids' => array(
"$userPushID"
),
'channel_for_external_user_ids' => 'push',
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'headings' => $heading,
'url' => $link
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic APPSECRET'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode($return);
print("\n\nJSON received:\n");
print($return);
print("\n");
}
?>
底部似乎有一个“return$response”行,这可能会打破while{}。试着删除那个部分,或者重写那个部分,好吧,如果是我的话,我会先尝试重写这个部分,这样嵌套的,而循环就更少了。在本例中,您可以非常简单地通过迭代结果(正如您所做的)并将其推送到函数外部的数组来完成,如下所示:
$ary = array();
while($row = mysqli_fetch_array($yourQueryResults, MYSQLI_ASSOC))
{
array_push($ary, array($row['valueYouIntendToUseLater']));
}
然后可以在生成的数组上运行interiorwhile
,如
foreach($element in $ary){
while.....etc
}
等等。你得到训练。虽然这种格式当然不是必需的,但根据我的经验,它确实使调试更容易。正如@Jelmer所指出的,这个函数在过程中失败的原因是因为您在其中调用return
,这打破了循环,这也是它的目的。返回肯定会打破循环。