dongxinyue2817 2014-12-19 17:16
浏览 67
已采纳

为while循环的每一行生成特定的数组

So I have a while loop which generates all the inbox messages for a user, then inside that while loop, I have a query and another while loop which generate all the recipients of that message. I am trying to build up an array from that inside while loop which will echo all the recipients/participants of each individual message thread. As is, when I var_dump my usernamesarray, It gets built up to contain all the recipients of all the messages (by the final time it is echoed). SO, how would I go about making this array unique to each row? I am sure this is a common issue, but I cannot seem to find the search terms to learn much about it.

 if (count($numbermessages)) {
        while ($stmt->fetch()) {
            $getrecipusernames= $mysqli->prepare("SELECT distinct(message_recips.userid), users.username, users.id FROM message_recips INNER JOIN users ON users.id=message_recips.userid WHERE messageid=?");
            $getrecipusernames->bind_param('i', $messageid);
            $getrecipusernames->execute();
            $getrecipusernames->store_result();
            $getrecipusernames->bind_result($recipuserid, $recipusername, $recipuserid1);
            $getrecipusernames->fetch();
                 while ($getrecipusernames->fetch()) {
                      $usernamesarray[] = $recipusername;
                  }

            $date_time=$created_on;
            $dte_new=date("l H:i A", strtotime($date_time));
               if ($status=="A"){
            $message_read_status="read";
               }
            else {
            $message_read_status="unread";
            }

Sincere thanks for any help!

  • 写回答

2条回答 默认 最新

  • duanbi9202 2014-12-19 18:28
    关注

    The solution was to declare the array prior to filling it, outside of the while loop:

     $usernamesarray = array();
     while($getrecipusernames->fetch()){
         $usernamesarray[] = $recipusername;
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?