dtmm0148603 2018-12-04 14:24
浏览 85

根据最后在另一个表中使用的结果对表结果进行排序

I have a page on my site that prints out all the customer names and id's in the database for use in a report to another database on work done.

What I would like to do is for this function to sort customers so that I get that list sorted in the order of when the customer was last used for in report. Where the newest report would make that reports customer land on first place in my result customer.

I am using Codeigniter and my controller has this code:

$data['reports'] = $this->db->get('reports');

$data['customers'] = $this->db->get('customers');

$this->load->view('list', $data);

That add this in my view:

foreach($customers->result() as $customer){ 
   echo "<option value='".$customer->id."'>".$customer->name."</option>";
}

I added a picture that hopefully makes this easier to understand.

enter image description here

UPDATE #1:

So I have with help in the comments below worked out this:

Controller:

$this->db->group_by('customer');
$this->db->order_by('date', DESC);
$data['reports'] = $this->db->get('reports');

$data['customers'] = $this->db->get('customers');

$this->load->view('list', $data);

View:

foreach($reports->result() as $rep){ 
    foreach($customers->result() as $customer){
        if($rep->customer == $customer->id){
            echo "<option value='".$customer->id."'>".$customer->name."</option>";
        }
    }
}

This gives me the customer echoed one time in the order of report date.

enter image description here

HOWEVER! This is not working as I intended it to. Because, yes, it sorts in order of the date, but it has grouped the reports first so it takes the date that comes last and present in the echo. Which will be the first date ever recorded for that customer. The opposite of what I wanted.

I have not figured out a way to change in what order grouped by is processed. If that is even possible..

Also it does not pick up any customer that does not have a report.

  • 写回答

1条回答 默认 最新

  • dongquexi1990 2019-02-05 16:03
    关注

    It might not be an answer to the question, but it made my project work at least. Instead of using a SQL command to pick out the last date from reports and combine it with the customer list I started in the other end with a new approach.

    I had another project where I needed to sort posts on a site and there I set the posts to a specific sorting value each time they moved.

    I used a variant of this and inserted it on this issue. So now when I do a report I also insert the current timestamp into "last_report" on the customer so now when I check the customer list I don't even need to call the reports, but only sort by the last_report which is able to be ordered with ASC and DESC.

    This might be a bad use of database, but it works and the total counts of database questions through a complete cycle is still the same.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集