如何将值从控制器传输到查看codeigniter到下拉列表

时间:2014-03-08 17:28:18

标签: php codeigniter

  if (is_array($jsonhome)) {
                foreach ($jsonhome as $query) {
                    foreach ($query['results']['place'] as $places) {


                        if (is_array($places['country'])) {
                            echo "Country:\n";
                            echo "Content: " . $places['country']['content'] . "\n\n";
                        }

                        if (is_array($places['admin1'])) {
                            echo "State:\n";
                            echo "Content: " . $places['admin1']['content'] . "\n\n";
                        }

                        if (is_array($places['admin2'])) {
                            echo "District/City:\n";
                            echo "Content: " . $places['admin2']['content'] . "\n\n";
                        }
                    }
                }
            }

    $location_data['user_current_country'] = $places['country']['content'];

            $this->load->view('header_register', $location_data);
            $this->load->view('body_complete_register', $location_data);
            $this->load->view('footer_register');

我想在视图codeigniter中将国家/地区转移到输入类型选择:

说我通过上述查询得到两个国家说印度和美国我想把它传递到视图

我是通过ajax吗?

请帮助我是codeigniter的新手

1 个答案:

答案 0 :(得分:1)

您的控制器中 您希望为<select>构建一系列选项。像

这样的东西
//Build array of country options
$aData['countryOptions'] = array();
foreach ($jsonhome as $query) {
    foreach ($query['results']['place'] as $places) {
        $aData['countryOptions'][] = $places['country']['content'];
    }
}

$this->load->view('body_complete_register', $aData);

然后在您的视图中 ,您可以使用form_dropdown函数,只要您已加载表单助手

$this->load->helper('form');
echo form_dropdown('countries', $countryOptions);

如果你不想使用你可以做的助手

<select name="countries" id="countries">
    <?php foreach($countryOptions as $key => $countryName) { ?>
        <option value="<?php echo $key ?>"><?php echo $countryName ?></option>
    <?php } ?>
</select>

请注意您不应该在控制器中回显任何内容。控制器仅用于在模型,库和视图之间传递数据