Codeigniter下拉菜单与数据库


Codeigniter dropdown menu with database

首先我要解释一下我的问题。我很新的编码,我试图使一个下拉菜单的编码从数据库获取结果。我已经有了一些经验,但并不像它应该的那样工作。

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;
    }
}

现在控制器:

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);
}

和视图:

 <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>

我想做的是一个大洲的菜单,每个大洲都有一个下拉菜单,下面是这样的国家:

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

我该怎么做?提前感谢!

您需要更新get_continents()函数,以便它为每个大陆附加国家

如何做到这一点?

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;
    }
}
在视图:

 <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>

我们要做的是:

  1. get_continents中,我们将国家对象附加到每个大陆对象。
  2. View中,我们将嵌套foreach循环中的$country替换为$row->country

您的错误很可能来自函数get_contries()有一行是$this->db->where('continent_id', $id);