1

I have a while loop that checks my database for notifications with a status of 1. It then sends a notification to OneSignal.com's API so the user will get the notification.

The problem is, I'm trying to run the script so that it processes all records with a status of 1 as it makes the while loop, but once I include the function for OneSignal the while loop stops after processing only one record.

I cant figure it out, I tried moving things around, and when I put the send message function outside the while loop, the php variables for the user ID don't get passed to it?

What should I do?

<?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");
    
    
    
}

?>

2 Answers 2

2

There seems to be a 'return $response' line near the bottom, which would probably break the while{}. Try removing that, or maybe rewrite that part

Sign up to request clarification or add additional context in comments.

1 Comment

The return will definitely break a loop.
0

Well, if it were me, I would try to rework this first so there are fewer nested while loops. In this case, you can do it fairly simply, by iterating through the results (as you're doing) and pushing them to an array outside the function, like so:

 $ary = array();

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

Then you can run the interior while on the resulting array, like

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

And so on. You get the drill. While this format is certainly not necessary, it does make debugging easier in my experience. As @Jelmer pointed out, the reason this while function is biting the dust is because you're calling return in it, which breaks the loop, as is its purpose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.