doutangqi4444 2014-03-08 09:28
浏览 37
已采纳

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

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


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

";
                        }

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

";
                        }

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

";
                        }
                    }
                }
            }

    $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');

I want to transfer the country to input type select in the view codeigniter:

Say i get two countries via the above queries say India and USA i want to pass it to the view

Do i do it via ajax?

Please help i am new to codeigniter

展开全部

  • 写回答

1条回答 默认 最新

  • douwen5681 2014-03-08 10:02
    关注

    So in your controller you want to build an array of options for your <select>. Something like

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

    Then in your view you can use the form_dropdown function providing you have the form helper loaded

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

    Of if you don't want to use the helper you could do

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

    Please note that you shouldn't be echoing anything out in the controller. The controller is just for passing data between models, libraries and views

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?