CodeIgniter Forums

Full Version: Logic problem filtering results using datamapper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Andy78[/eluser]
Ok I have a results set of takeaways that I am looping through, the takeaway objects have the following properties id, name, address, postcode, distance, mile_cost, pizza, Chinese, Indian, kebab. If they have a pizza menu fr example it will contain 1 or a 0 if not etc

I want to filter the results in order to get the closest single pizza, Chinese, Indian and kebab selling restaurants. But also filter the results so that it doesn't show results with a distance greater than 10 and greater than the individual takeaways max deliver distance.

I have to do this in the php code rather than the mysql query and I am kinda stumped as how to do it. This is what I have so far.

Controller:
Code:
function search(){
        //load the geozip library  
        $this->load->library("geozip");
        //get the user's postcode
        $location = $this->input->post('postcode');
        //make sure post upper case and has no spaces
        $location = strip_tags($location);
     $location = strtoupper($location);
     $location = str_replace(" ", "", $location);
        
        //get all postcodes in a 10 mile radius of a postcode-location sorted by distance
        //$postcode = $this->geozip->get_zips_in_range($location, 10, SORT_BY_DISTANCE_ASC, TRUE);
        
        $takeaway = new Takeaway();
        $takeaway->select('id, name, address, postcode, distance, mile_cost, pizza, chinese, indian, kebab'); //Select only the required fields
        $takeaway->get();
        
        $data['location'] = $location;
        $data['takeaway'] = $takeaway;
        
        $this->load->view('results', $data);
    }

View:
Code:
<?php

foreach($takeaway as $takeaway){
            
            $distance = $this->geozip->get_distance($location, $takeaway->postcode);//get the distance between the two postcodes
                        
            if($distance <= $takeaway->distance && $distance <= 10){
            
                $number = 0;
              
                echo $takeaway->name.' is '.$distance.'miles away <br />';
              
             ?&gt;  
               <div class="takeaway_box">
                   <div class="takeaway_box_band">Indian</div>
                    
               </div>                
&lt;?php
            }

}
?&gt;

I guess in order to get the closest single restaurant of each type I will have to find the ones with a distance closest to zero then order them some how?

Any help on how to do this would be much appreciated.

El Forum

[eluser]WanWizard[/eluser]
First a general remark:

Most of the code in search() and in your view should be in your model. This logic does not belong in your controller or view. It should be of no concern to both how you get to the list of adresses.

As to the selection:

If your geozip library can produce an array of zipcodes in a specific radius, you can use a where_in('postcode', $array_of_postcodes) to select only record whose postcode is in the list of zipcodes within radius.

El Forum

[eluser]Andy78[/eluser]
Thanks that's starting to make a bit more sense, yes geozip can return an array of zipcodes in a specific radius. Although I'm not sure exactly which logic should be in model can you elaborate?