[eluser]Andy78[/eluser]
Ok I have a search function that returns a list of takeaways based on a postcode radius search as the code shows below.
But I want to limit the results to the closest one takeaway of each type. There are 4 types of takeaway based on a takeaway_type_id field. So if the user enters their own postcode a single takeaway of each type will be presented to them which is also the closest takeaway of that type to the entered postcode.
Whats the best way to do this with datamapper? I know I should move this existing logic into the model rather than the controller but Ill wait till logic is all working.
Code:
function search(){
//get the user's postcode
$postcode = $this->input->post('postcode');
//make sure post upper case and has no spaces
$location = strip_tags($postcode);
$location = strtoupper($postcode);
$location = str_replace(" ", "", $postcode);
$d = 10;
//get all postcodes in a 10 mile radius of a postcode-location sorted by distance
$postlist = $this->geozip->get_zips_in_range($location, 10, SORT_BY_ZIP_ASC, TRUE);
//Get footer menu items
$footer_menu = new Menu_item();
$footer_menu->where('menu_id', 2);
$footer_menu->where('published', 1);//make sure item is published
$footer_menu->order_by('order', 'asc')->get();
$takeaway = new Takeaway();
//$list = $takeaway->get_takeaways($postlist);
$takeaway->select('id, name, address, postcode, distance, mile_cost, pizza, chinese, indian, kebab'); //Select only the required fields
$takeaway->where_in('postcode', $postlist);
$takeaway->get();
$distance = $this->geozip->get_distance($location, $takeaway->postcode);//get the distance between the two postcodes
$content_data['footer_menu'] = $footer_menu;
$data['footer_m'] = $this->load->view('footer_menu', $content_data, TRUE); //pull in footer menu view
$data['location'] = $location;
$data['takeaway'] = $takeaway;
$this->load->view('results', $data);
}