douwei4370 2018-02-05 02:52
浏览 27
已采纳

查询仅返回codeigniter中的最后一个值

I am new to CI/PHP/development. Im stuck on this problem. It seems to be really basic but i just cant get it right! I am able to returning only last value while sql excecution .

controller

$data['leadd']= $this->dashboard_model->countt($this->session->userdata('user_id'));

     foreach ($data['leadd'] as $q) { $date = $q['followup_date'];

     $data['lead']= $this->dashboard_model->lead_count($this->session->userdata('user_id'),$date); }

     $data['count'] = count($data['lead']);

Model

public function countt($userid)
{
    $sql = $this->db->query("SELECT followup_date FROM tg_partner_data WHERE assign_to = '$userid' AND active = 'Y'");

    return $sql->result_array();
}

public function lead_count($userid,$date)
{   
    $sql1 = $this->db->query("SELECT * FROM tg_partner_data
                             WHERE assign_to = '$userid' AND followup_date = '$date'
                             AND active='Y'");
    //echo $this->db->last_query();

    return $sql1->result_array();
}
  • 写回答

1条回答 默认 最新

  • 「已注销」 2018-02-05 03:28
    关注

    First you select all followup dates from one table by executing this: "SELECT followup_date FROM tg_partner_data WHERE assign_to = '$userid' AND active = 'Y'".

    Then for each followup_date, you execute another query but you don't merge prevoius results just overwrites them in foreach loop.

    Foreach loop should look like this:

    $data['lead'] = array();
    foreach ($data['leadd'] as $q) { 
        $date = $q['followup_date'];
    $data['lead'][] = $this->dashboard_model->lead_count($this->session->userdata('user_id'),$date));
    }
    

    You can also rewrite SQL query: SELECT COUNT(*) FROM tg_partner_data WHERE assign_to = '$userid' AND followup_date = '$date' AND active='Y'"

    It will allow you to get number of rows directly from DB.

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部