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

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么