爆炸数组正在跳过值

时间:2014-02-08 06:01:05

标签: php arrays

// WHEN A TOURNAMENT GETS SUBMITTED TO DATABASE MAKE SURE THE ID IS GRABBED AFTER IT HAS BEEN SUBMITTED
// $Tournamentid = mysqli_insert_id() or $Tournamentid = mysqli_insert_id();
// SET DATA FOR THE PRIVATE MESSAGE TO FOLLOWERS
$defaultP = "x";
$from = $n; // maybe $log_username instead of id
$subject = "New Tournament"; //this can be changed
$message = 'hey, I Just made a New Tournament. <a href="http://localhost/esports/tournament.php?n='.$url_name.'">Click here</a> to view the page. Do not reply to this message as we will not recieve it.'; /// this can be changed can add link to tournament

// GRAB FOLLOWERS LIST FOR THIS PROVIDER
$followArray = mysqli_query ($db_conx, "SELECT followers FROM provider WHERE name='$n'");
while ($followrow = mysqli_fetch_array($followArray)){
       $follower_array = $followrow["followers"];
}

// IF THEY HAVE FOLLOWERS
if ($follower_array != "") {
    $follower_array2 = explode(",", $follower_array);

    // CREATE A LOOP THAT WILL FIRE OFF A PM TO EACH FOLLOWER
    // INSERT PRIVATE MESSAGES FOR EACH SUBSCRIBER INTO PN TABLE USING $VALUE TO ADD THEIR ID
    $sqli = "INSERT INTO pm (receiver, sender, senttime, subject, message, parent)
             VALUES('$value', '$from', NOW(), '$subject', '$message', '$defaultP')"; 
}

这是一个小型自动PM系统,因此当有人创建锦标赛时,所有关注者都会通过链接向其发送关于它的消息。

我遇到的问题是它只发送一个用户下午,这是创建该事件的人的最后一个跟随者。我知道这一点,因为我有3个人关注这1个帐户,并且在pm系统中只创建了1条消息。我需要它发送它需要发送的消息量,而不管数字。

这当前只发送1 PM,这是发送到名为['followers']的数据库行中数组中的最后一个人,所以我坚信我的代码中的错误是foreach ($follower_array2 as $key => $value) {,因为它没有正确设置值,但我可能是错的,可能是错的,但如果有人能帮助我,我会非常感激。

谢谢

1 个答案:

答案 0 :(得分:2)

我认为while代替使用foreach语句,在这种情况下效果会更好。它应该是这样的:

//////////WHEN A TOURNAMENT GETS SUBMITTED TO DATABASE MAKE SURE THE ID IS GRABBED AFTER IT HAS BEEN SUBMITTED
            //////// $Tournamentid = mysqli_insert_id() or $Tournamentid = mysqli_insert_id();
            ///////////// SET DATA FOR THE PRIVATE MESSAGE TO FOLLOWERS
            $defaultP = "x";
            $from = $n; //// maybe $log_username instead of id
            $subject = "New Tournament"; //this can be changed
            $message = 'hey, I Just made a New Tournament. <a href="http://localhost/esports/tournament.php?n='.$url_name.'">Click here</a> to view the page. Do not reply to this message as we will not recieve it.'; /// this can be changed can add link to tournament
            ///////////////////GRAB FOLLOWERS LIST FOR THIS PROVIDER
            $followArray = mysqli_query ($db_conx, "SELECT followers FROM provider WHERE name='$n'");
            ////////IF THEY HAVE FOLLOWERS
            if ($follower_array != ""){
            $follower_array2 = explode(",", $follower_array);
            //////// CREATE A LOOP THAT WILL FIRE OFF A PM TO EACH FOLLOWER
            foreach ($follow_array2 as $follow){
            ////////////////INSERT PRIVATE MESSAGES FOR EACH SUBSCRIBER INTO PN TABLE USING $VALUE TO ADD THEIR ID
            $sqli = "INSERT INTO pm (receiver, sender, senttime, subject, message, parent)
            VALUES('$follow', '$from', NOW(), '$subject', '$message', '$defaultP')"; } }

这样,对于每个人都有的追随者,它会向他们发送信息。