【问题标题】:While loop not running after functionWhile循环在函数后没有运行
【发布时间】:2021-05-17 08:02:49
【问题描述】:

我有一个 while 循环来检查我的数据库中状态为 1 的通知。然后它会向 OneSignal.com 的 API 发送通知,以便用户收到通知。

问题是,我正在尝试运行脚本,以便它在进行 while 循环时处理状态为 1 的所有记录,但是一旦我包含 OneSignal 的函数,while 循环在仅处理一条记录后停止.

我想不通,我试着四处移动,当我将发送消息函数放在 while 循环之外时,用户 ID 的 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");
    
    
    
}

?>

【问题讨论】:

    标签: php function while-loop


    【解决方案1】:

    好吧,如果是我,我会先尝试重做这个,这样嵌套的while 循环就会更少。在这种情况下,您可以通过迭代结果(就像您正在做的那样)并将它们推送到函数外部的数组来相当简单地做到这一点,如下所示:

     $ary = array();
    
        while($row = mysqli_fetch_array($yourQueryResults, MYSQLI_ASSOC))
        {
            array_push($ary, array($row['valueYouIntendToUseLater']));
        }
    

    然后你可以在结果数组上运行内部while,就像

        foreach($element in $ary){
            while.....etc
        }
    

    等等。你得到了演习。虽然这种格式当然不是必需的,但根据我的经验,它确实使调试更容易。正如@Jelmer 指出的那样,这个while 函数之所以尘埃落定,是因为您在其中调用return,这打破了循环,这也是它的目的。

    【讨论】:

      【解决方案2】:

      底部附近似乎有一条“return $response”行,这可能会破坏 while{}。尝试删除它,或者重写该部分

      【讨论】:

      • 返回将肯定打破一个循环。
      猜你喜欢
      • 2019-02-24
      • 2015-08-06
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2021-06-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多