dongshukou0240 2014-02-06 05:09
浏览 31
已采纳

PHP foreach值在数组中

I am making an Staff Online section for members to see weather any staff member is in-game to help them or not. I began tackling this idea with an array of the Staff Members account ID's. It looks something like this:

$this->view->staffAdmins = array(64, 80, 96);

Then I used a foreach statement to get the following details for each account:

  • Are they logged in?
  • If so, use their ID from the array and get more information from the users table

My foreach statement looks like this:

foreach ($this->view->staffAdmins as $query) {
    //Are they logged in?
    $sql = "SELECT * FROM point WHERE uid = :ID AND zoneid > -1";
    $arr = array(":ID" => $query);
    $this->view->result = $this->database->DBCtr($sql, $arr);
    //Get their details!
    $sql = "SELECT * FROM users WHERE ID = :ID";
    $arr = array(":ID" => $query);
    $this->view->staffmem = $this->database->DBQry($sql, $arr);
    $this->view->name = $this->view->staffmem[0]['name'];
    $this->view->truename = $this->view->staffmem[0]['truename'];
    if ($this->view->result == 1){
        echo $this->view->truename;
    }
}

That returns the following output:

Hulu is Online
Cookiez is Online

Which is exactly what I need, but it outputs that at the very top of the page, which is not what I need. And when I try to put echo $this->truename; in the correct spot on the actual page it renders to, the output is

Cookiez is Online

It only gets the second staff member's ID (80) in the array, while we are both logged in at the same time.

Also, this is the code I am using to attempt to get same output as the one the working foreach statement. This is on the page the class renders too.

foreach ($this->staffAdmins as $staff){
    if ($this->result == 1){
        foreach ($this->staffmem as $logged){
        echo $logged['truename'];
        }
    }
}
  • 写回答

1条回答 默认 最新

  • duanju8308 2014-02-06 05:15
    关注

    you can make a array for logged in users like this

    $arr_logged_users = array(); // array to store logged in users
    
    foreach ($this->view->staffAdmins as $query) {
        //Are they logged in?
        $sql = "SELECT * FROM point WHERE uid = :ID AND zoneid > -1";
        $arr = array(":ID" => $query);
        $this->view->result = $this->database->DBCtr($sql, $arr);
        //Get their details!
        $sql = "SELECT * FROM users WHERE ID = :ID";
        $arr = array(":ID" => $query);
        $this->view->staffmem = $this->database->DBQry($sql, $arr);
        $this->view->name = $this->view->staffmem[0]['name'];
        $this->view->truename = $this->view->staffmem[0]['truename'];
        if ($this->view->result == 1){
            $arr_logged_users [] = $this->view->truename; // assign here to array
        }
    }
    

    now you can use $arr_logged_users where ever you want like this

    foreach($arr_logged_users as $val)
    {
        echo $val;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀