doutangqi4444 2014-03-08 17: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 18: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

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图