1

first i'll try to explain my problem. I'm very new to codeigniter and i'm trying to make a drop down menu in codeigniter fetching results from database. I already made some experiences but doesn't work as it should.

first my model:

public function get_continents() {
    $this->db->select('*');
    $this->db->from('continents');
    $this->db->order_by("continent_name", "asc");
    $query = $this->db->get();

    if($query->num_rows() > 0) {
        return $query->result();
    }
    else {
        return FALSE;
    }
}

public function get_contries() {
    $this->db->select('*');
    $this->db->from('countries');
    $this->db->join('continents', 'country_continent_id = continent_id', 'left');
    $this->db->where('continent_id', $id);
    $this->db->order_by('country_name', 'asc');
    $query = $this->db->get();

    if($query->num_rows() > 0) {
        return $query->result();
    }
    else {
        return FALSE;
    }
}

now the controller:

public function index() {
    $dados['title'] = 'Places to Visit';
    $dados['page'] = 'home';

    // lista dos continentes
    $this->load->model('option_model');
    $dados['continent'] = $this->option_model->get_continents();

    // lista os paises
    $dados['country'] = $this->option_model->get_contries();

    // chamar a vista -> view
    $this->load->view('home', $dados);
}

and the view:

 <nav>
            <ul>
                <li><a href="" id="home">Homepage</a></li>
                <?php foreach ($continent as $row) {; ?>
                    <li>
                        <a href="" id="<?php echo $row->continent_id; ?>"><?php echo $row->continent_name; ?></a>
                        <ul>
                            <?php foreach($country as $row2) {; ?>
                                <li><a href="" id="<?php echo $row2->country_id; ?>"><?php echo $row2->country_name; ?></a></li>
                            <?php }; ?>
                        </ul>
                    </li>
                <?php } ?>
                <li><a href="" id="pt">Portugal</a></li>
            </ul>
        </nav>

What i want to make is a menu of continents and each continent has a drop down with the countries like this:

> continent 1
   - country 1
   - country 2
   - ...
> continent 2
   - country 1
   - country 2
   - ...
> ...

how can i do this? Thanks in advance!

2 Answers 2

1

You need to update the get_continents() function so that it append the country for each continent.

How to do this?

In get_continents()

public function get_continents() {
    $this->db->select('*');
    $this->db->from('continents');
    $this->db->order_by("continent_name", "asc");
    $query = $this->db->get();

    if($query->num_rows() > 0) {

        // Get Countries list for each continent
        foreach ($query->result() as $row) {

            $this->db->select('*');
            $this->db->from('countries');
            $this->db->where('country_continent_id', $row->continent_id);
            $this->db->order_by('country_name', 'asc');
            $country = $this->db->get()->result();

            // append country to the continent object
            $row->country = $country;

        }

        return $query->result();

    }
    else {
        return FALSE;
    }
}

In View:

 <nav>
            <ul>
                <li><a href="" id="home">Homepage</a></li>
                <?php foreach ($continent as $row) {; ?>
                    <li>
                        <a href="" id="<?php echo $row->continent_id; ?>"><?php echo $row->continent_name; ?></a>
                        <ul>
                            <?php foreach($row->country as $row2) {; ?>
                                <li><a href="" id="<?php echo $row2->country_id; ?>"><?php echo $row2->country_name; ?></a></li>
                            <?php }; ?>
                        </ul>
                    </li>
                <?php } ?>
                <li><a href="" id="pt">Portugal</a></li>
            </ul>
        </nav>

What we do is the following:

  1. In the get_continents we append the country object to each of continent object.
  2. In View, we replace $country in nested foreach loop with $row->country
Sign up to request clarification or add additional context in comments.

1 Comment

Please, please, please do NOT make iterated trips to the database as this answer is recommending.
0

Your error is most probably from the function get_contries() you have one line that has $this->db->where('continent_id', $id);

remove that line and you should be fine.

4 Comments

tks for your reply but in order to know the country continent shouldn't i have a variable for that? that is one of my problems. if i remove that line, i have no way to see what country belongs to what continent.
actually that line is not working at all, because you don't have a variable name $id so it will mess your query, if you want build it using json.
i know its not working. i have to pass a value to that variable but dont know how. my first guess is to join the 2 functions but as i said i'm new to codeigniter.
yes, you have to join the two functions in one function,it will take a bit of time from you, but it will save you fore sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.