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.
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.
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.