我是CodeIgniter的新手,在过去3个小时中,我一直在努力解决此错误。
在以下代码中调用未定义的方法Product_filter_model :: fetch_data()错误。
您可以在以下链接的“检查”>“网络”选项卡中查看错误: http://admin.millionkidstoschool.org/index.php/product_filter
我已经确认了型号,控制器和功能的名称。我也试过把$ this-> load-> model('product_filter_model');在函数fetch_data()中,它没有帮助。
控制器
class Product_filter extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('product_filter_model');
$this->load->helper('url');
}
function fetch_data(){
$minimum_price = $this->input->post('minimum_price');
$maximum_price = $this->input->post('maximum_price');
$brand = $this->input->post('brand');
$ram = $this->input->post('ram');
$storage = $this->input->post('storage');
$output = array(
/*'pagination_link' => $this->pagination->create_links(),*/
'product_list' => $this->product_filter_model->fetch_data($minimum_price, $maximum_price, $brand, $ram, $storage)
);
echo json_encode($output);
}
查看
$(document).ready(function(){
filter_data();
function filter_data(){
$('#filter_data').html("<div id='loading'></div>");
var action = 'fetch_data';
var minimum_price = $('#hidden_minimum_price').val();
var maximum_price = $('#hidden_maximum_price').val();
var brand = get_filter('brand');
var ram = get_filter('ram');
var storage = get_filter('storage');
$.ajax({
url:"<?php echo base_url(); ?>index.php/product_filter/fetch_data",
method:"POST",
dataType:"JSON",
data:{action:action, minimum_price:minimum_price,maximum_price:maximum_price, brand:brand, ram:ram, storage:storage},
success:function(data){
$('.filter_data').html(data.product_list);
/*$('#pagination_link').html(data.pagination_link);*/
}
})
}
}
型号
function fetch_data($minimum_price, $maximum_price, $brand, $ram, $storage)
{
$query = $this->make_query($minimum_price, $maximum_price, $brand, $ram, $storage);
/*$query .= ' LIMIT '.$start.', ' . $limit;*/
$data = $this->db->query($query);
$output = '';
if($data->num_rows() > 0)
{
foreach($data->result_array() as $row)
{
$output .= '
some data
';
}
}
else
{
$output = '<h3>No Data Found</h3>';
}
return $output;
}
谢谢您的时间。
更新:
我在发布ajax请求的控制器函数中复制了两个模型方法的代码。有效。但是我无法理解为什么控制器功能无法访问模型方法?
public function fetch_data(){
$this->load->model('product_filter_model');
$minimum_price = $this->input->post('minimum_price');
$maximum_price = $this->input->post('maximum_price');
$brand = $this->input->post('brand');
$ram = $this->input->post('ram');
$storage = $this->input->post('storage');
//$output = array(
/*'pagination_link' => $this->pagination->create_links(),*/
// 'product_list' => $this->product_filter_model->fetch_data//($minimum_price, $maximum_price, $brand, $ram, $storage)
//);
/*$query = $this->product_filter_model->make_query($minimum_price, $maximum_price, $brand, $ram, $storage);*/
/*$query .= ' LIMIT '.$start.', ' . $limit;*/
$query = "
SELECT * FROM product
WHERE product_status = '1'
";
if(isset($minimum_price, $maximum_price) && !empty($minimum_price) && !empty($maximum_price))
{
$query .= "
AND product_price BETWEEN '".$minimum_price."' AND '".$maximum_price."'
";
}
if(isset($brand)){
$brand_filter = implode("','", $brand);
$query .="
AND product_brand IN('".$brand_filter."')
";
}
if(isset($ram)){
$ram_filter = implode("','", $ram);
$query .="
AND product_ram IN('".$ram_filter."')
";
}
if(isset($storage)){
$storage_filter = implode("','", $storage);
$query .="
AND product_storage IN('".$storage_filter."')
";
}
/*
return $query;*/
$data = $this->db->query($query);
$result = '';
if($data->num_rows() > 0)
{
foreach($data->result_array() as $row)
{
$result .= '
<div class="col-sm-4 col-lg-3 col-md-3">
<div style="border:1px solid #ccc; border-radius:5px; padding:16px; margin-bottom:16px; height:450px;">
<img src="'.base_url().'images/'. $row['product_image'] .'" alt="" class="img-responsive" >
<p align="center"><strong><a href="#">'. $row['product_name'] .'</a></strong></p>
<h4 style="text-align:center;" class="text-danger" >'. $row['product_price'] .'</h4>
<p>Camera : '. $row['product_camera'].' MP<br />
Brand : '. $row['product_brand'] .' <br />
RAM : '. $row['product_ram'] .' GB<br />
Storage : '. $row['product_storage'] .' GB </p>
</div>
</div>
';
}
$output = array('product_list' => $result);
}
else
{
$result = '<h3>No Data Found</h3>';
}
echo json_encode($output);