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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭