eskimopest eskimopest - 1 month ago 17
MySQL Question

Codeigniter dropdown menu with database

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!

Answer

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