【问题标题】:Codeigniter method in controller does not call method in model控制器中的 Codeigniter 方法不调用模型中的方法
【发布时间】:2015-08-17 20:51:15
【问题描述】:

我需要你的帮助.. 在我的视图文件中,我使用 ajax 向我的控制器请求数据。 这是ajax代码:

<script type="text/javascript">
     $(document).ready(function(){
      $('#selCat').change(function(){
       var id = $(this).val(); // selected name from dropdown #table
       $.ajax({
        url: '<?php echo base_url();?>'+'index.php/client/ajax_subcat',  // or "resources/ajax_call" - url to fetch the next dropdown
        async: false,
        type: "POST",     // post
        data: {id:id},
        dataType: "html",    // return type
        success: function(data) {  // callback function
         $('#selSubCat').html(data);
        }
       })
      });
     });
    </script>

同时客户端控制器中的 ajax_subcat() 函数是这样的:

public function ajax_subcat(){
    //  $this->load->view('content/ajax_subcat');

        if($_POST){

            $catcode = $this->input->post('id',TRUE);
            $result = $this->preference_model->getSubCategory3($catcode);
            //$result = $this->preference_model->getSubCategory2();
            echo "<script type='text/javascript'>alert('$catcode');</script>";
            //echo "<script type='text/javascript'>alert('$result->cat2name_EN');</script>";

            foreach ($result as $row) {
            echo $row->cat3name_EN;
            # code...
            }
        }

        //$this->load->view('content/');
    }
}

正如您在控制器中看到的,我尝试调用函数 getSubCategory3($catcode);在我的模型中,但这条线没有被执行。我尝试使用 alert 来查看发生了什么。

因为前三个回显已执行,所以我收到了警报。但似乎遇到时代码停止了

    $result = $this->preference_model->getSubCategory3($catcode);

然后什么都不返回给视图。

我的preference_model中的函数是这样的:

function getSubCategory3($cat2code){
        $return = array();
        $queryString = "SELECT id, cat3code, cat3name_EN FROM `subcategory` WHERE cat2code = '".$cat2code."' ORDER by cat3name_EN";
        echo $queryString;

        $query = $this->db->query($queryString);

        if($query->num_rows()>0){
            foreach($query->result() as $row){
                array_push($return, $row);
            }
        }
        return $return;
    }

preference_model 中没有执行任何操作。

有人知道我的代码有什么问题吗?

注意:

我在客户端控制器的函数 __construct() 中加载了模型

function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
        $this->load->model('preference_model');
    }

当我将控制器中的回显警报更改为回显字符串时,我的屏幕上没有打印任何内容。

【问题讨论】:

  • 是preference_model还是reference_model
  • $this->load->model('Model_name');你设置模型了吗?
  • 这是preference_model,我已将其加载到我的控制器中。我已经更详细地编辑了我的问题
  • model 中不使用alert,您可以使用echoprint_r 进行检查
  • 这真的很奇怪。我尝试按照您的建议使用 print_r 和 echo 更改警报,现在没有打印任何内容

标签: jquery html ajax codeigniter


【解决方案1】:

Codeigniter 中,我们需要在使用ModelsHelpersLibraries 之前加载它们。

在调用模型方法之前在代码中添加以下行

$this->load->model('preference_model');

您还可以在代码中使用备用名称

$this->load->model('preference_model', 'prefrence');
$result = $this->preference->getSubCategory3($catcode);

【讨论】:

  • 我其实是在函数__construct()中加载的,但还是遇到了我这里写的问题。
  • 编写你的__construct方法和preference_model代码
  • 我在我的问题中添加了它。
【解决方案2】:

我已经解决了我自己的问题。 事实证明,我不应该回显任何其他内容,而是要回传给调用者的结果。呵呵

这是我最终的 ajax_subcat() 代码

public function ajax_subcat(){

        if($_POST){
            $catcode = $this->input->POST('id',TRUE);
            //echo "<script type='text/javascript'>alert('$catcode');</script>";
            $result = $this->preference->getSubCategory3($catcode);
            //$result = $this->preference_model->getSubCategory2();
            //echo "<script type='text/javascript'>alert('$catcode');</script>";
            //echo "<script type='text/javascript'>alert('$result->cat2name_EN');</script>";
            $output = null;
            foreach ($result as $row) {

             $output .= "<option value='".$row->cat3code."'>".$row->cat3name_EN."</option>";
        }

         echo $output;
        }

        //$this->load->view('content/');
    }

我的第二个下拉菜单现在已成功填充。

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多