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

时间:2014-12-19 17:16:53

标签: php arrays

所以我有一个while循环为用户生成所有收件箱消息,然后在while循环中,我有一个查询和另一个while循环,它生成该消息的所有收件人。我正在尝试从内部构建一个数组while循环,它将回显每个消息线程的所有收件人/参与者。因此,当我var_dump我的usernamesarray时,它被构建为包含所有消息的所有收件人(到最后一次它被回显)。那么,我怎样才能使这个数组成为每一行的独特之处?我确信这是一个常见问题,但我似乎无法找到搜索条件来了解它。

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

真诚地感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

解决方案是在填充之前声明数组,在while循环之外:

 $usernamesarray = array();
 while($getrecipusernames->fetch()){
     $usernamesarray[] = $recipusername;
 }

答案 1 :(得分:0)

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($messageid);//use one bind
        $getrecipusernames->execute();
        $getrecipusernames->store_result();
        $getrecipusernames->bind_result($recipuserid, $recipusername, $recipuserid1);
             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";
        }