Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to provide data for nested overview?
  • Hello,

    having a module with Countries, States, Regions and Cities, I now want to make some kind of an overview page which shows the Countries, within the Countries the connected States for this Counmtry, within the States the connected Regions of the State and so on.

    What is the best way to provide the correct data?
    Is there a way of storing the connected Objects within the current Object?

    Or is it better so make one big Array with all Keys and Names to show to the User?
    If yes, how can I do that?

    Thanks for our help.
    Kay


  • I greatly depends on where this data comes from, and what you want to do with it. So it's not an easy to answer question.
  • ok, let's try to make it clearer :-)

    I have 4 models within one module:
    Countries http://snipr.it/~Co
    States http://snipr.it/~Cp
    Regions http://snipr.it/~Cq
    Cities http://snipr.it/~Cr

    What I now want to make is an overview page which shows data like this:
    http://snipr.it/~Cs

    I suppose that I need to make one big array for the view which looks something like that?
    http://snipr.it/~Cm

    Or could I also work with the full Objects of the different models?

    What I need at minimum is the name of the Objects and the ID, as this page will also habe direct links to edit the data.

    Thanks for your help.

    Happy Easter
    Kay
  • If you have ORM models, and your relations are defined properly, you can just fetch them and loop over them? No need to do any conversion if you just want to echo them out.

    In your Controller:

    // get all information
    $countries = Model_Country::query()->related('states')->related('states.regions')->related('states.regions.cities')->get();
    // pass it to the view
    $this->template->set('countries', $countries, false);

    And in your View:

    foreach ($countries as $country)
    {
        foreach ($country->states as $state)
        {
            foreach ($state->regions as $region)
            {
                foreach ($region->cities as $city)
                {
                }
            }
        }
    }

    And do whatever you want to to inside each foreach.
  • Thanks WanWizard.
    That was exactly I was looking for.

    Greetz Kay
  • One more question:
    To povie Countries, States and Regions in a DropDownBox in the City Model, where to select the current Region, is there a similar way to do this?

    Actually I do the following (what I find realy complicated):

    	// Reading out all Countries, States and Regions for SelectBox
    $regionselect = array('' => __('please_choose'));
    $country_array = array();
    $countries = \Destinations\Model_Country::find('all');
    foreach ($countries as $i => $obj)
    {
    $all_countries[$i] = $obj->to_array();
    }
    foreach ($all_countries as $value)
    {
    $states = \Destinations\Model_State::find('all', array('where' => array(array('country_id', $value['id']),),));
    if(!empty($states)){
    $country_array[$value['name']] = array();
    }
    $state_array = array();
    foreach ($states as $i => $obj)
    {
    $all_states[$i] = $obj->to_array();
    $all_states[$i]['name'] = '  '.$all_states[$i]['name'];
    $regions = \Destinations\Model_Region::find('all', array('where' => array(array('state_id', $all_states[$i]['id']),),));
    if(!empty($regions)){
    $state_array[$all_states[$i]['name']] = array();
    }
    $region_array = array();

    foreach ($regions as $i => $obj)
    {
    $all_regions[$i] = $obj->to_array();
    $region_array[$all_regions[$i]['id']] = '    '.$all_regions[$i]['name'];
    }
    $state_array = $state_array + $region_array;
    }
    $country_array = $country_array + $state_array;
    }
    $data['regions'] = $regionselect + $country_array;


    Thanks
    Kay
  • Dropdowns need an array with key-value pairs, and a multi-dimensional array will automatically create optgroups. So getting that from an ORM structure will always mean some looping. I would use the same query and foreach structure as I showed before:

    $options = array();

    foreach ($countries as $country)
    {
        $options[$country->name] = array();
        foreach ($country->states as $state)
        {
            $options[$country->name][$state->name] = array();
            foreach ($state->regions as $region)
            {
                $options[$country->name][$state->name][$region->name] = array();
                foreach ($region->cities as $city)
                {
                $options[$country->name][$state->name][$region->name][$city->id] = $city->name;
                }
            }
        }
    }
    You probably have to do a bit more if you want to be able to select the region if a region doesn't have any cities defined.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion