douhezhan5348 2015-01-27 01:46
浏览 78
已采纳

PHP循环 - 显示倍数

My Database table looks like this (rough outline):

    +--------+-----------+-------------+---------+------------+------------+
    | log_id | host_name |   status    | host_id | profile_id | event_date |
    +--------+-----------+-------------+---------+------------+------------+
    |   1    |   site1   |   Online    |    2    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+
    |   2    |   site1   |   Online    |    2    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+
    |   3    |   site1   |   Offline   |    2    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+
    |   4    |   site2   |   Online    |    4    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+ 
    |   5    |   site2   | Maintenance |    4    |    1       |  <*date*>  |
    +--------+-----------+-------------+---------+------------+------------+

Here are my codes:

VIEW:
<?php foreach($result as $row): ?> 
  ['<?php echo $row->host_name;?>',<?php echo $this->help_model->filter_online($id,$row->host_id, 'Online');?>,
  <?php echo $this->help_model->filter_online($id,$row->host_id, 'Offline');?>,
  <?php echo $this->help_model->filter_online($id,$row->host_id,'Maintenance');?>],
 <?php endforeach; ?> 

CONTROLLER:
public function userreports()
    {       
        $data['id']=$this->session->userdata('profile_id');
        $this->session->set_userdata('from_date', '2015-01-01');
        $from_date = $this->session->userdata('from_date');
        $date = date('Y-m-d',now());
        $this->session->set_userdata('to_date', $date);
        $to_date = $this->session->userdata('to_date');
        $data['result'] = $this->help_model->all_logs($this->session->userdata('profile_id'));
        $this->load->view('squidtopus1-host-reports', $data);
    }

MODEL:
function all_logs($id)
    {
        $id = $this->session->userdata('profile_id');
        $this->db->select("*");
        $this->db->from('server_log');
        $this->db->where('profile_id',$id);
        $query = $this->db->get()->result();
        return $query;
    }

function filter_online($data, $host_id, $status)
    {
        $from_date = $this->session->userdata('from_date');
        $to_date = $this->session->userdata('to_date');
        $id = $this->session->userdata('profile_id');
        $this->db->select()->from('server_log');
        $this->db->where('profile_id',$id);
        $this->db->where('status',$status);
        $this->db->where('host_id',$host_id);
        $this->db->where('event_date >=',$from_date);
        $this->db->where('event_date <=',$to_date);
        $data =  $this->db->get()->result();
        $stat_count = 0;
        foreach ($data as $row) {
            $stat_count++;
        }

        return $stat_count;
    }

The information that needs to displayed is shown properly, but the problem is that it repeatedly shows it for each row in the database that matches 'profile_id = $id'(in this case, profile_id=1). Ideally it should show:

['site1', 2,1,0], ['site2', 1,0,1]

But instead it shows:

['site1', 2,1,0],['site1', 2,1,0],['site1', 2,1,0],['site2', 1,0,1],['site2', 1,0,1]

I can't determine where I've gone wrong... So any insight into solving this issue is greatly appreciated. Thanks ahead!

  • 写回答

1条回答 默认 最新

  • douzi4724 2015-01-27 02:12
    关注
    function all_logs($id)
    {
        $id = $this->session->userdata('profile_id');
        $this->db->select("DISTINCT(host_id), host_name");
        $this->db->from('server_log');
        $this->db->where('profile_id',$id);
        $query = $this->db->get()->result();
        return $query;
    }
    

    try to change your function like above

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动