我得到我的 Ajaxcont 控制器中未定义结果变量。我不确定如何将 $results 从模型传回控制器。我只想让我的 $results 数组保存查询检索到的所有值。我究竟做错了什么?
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ajaxcont extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model("ajax_model");
}
function index()
{
$search = $this->input->post('search', TRUE);
$like_search= '%' .$this->db->escape_like_str($search) . '%';
$query= $this->ajax_model->search_course($like_search);
if ($query)
{
foreach($query->result() as $row)
{
//$course_name_highlighted = str_ireplace($search, '<b>' .$search . '</b>' , $row->full_name);
$start = stripos($row-> Course_Name, $search);
$length= strlen($search);
$new_search = substr($row->Course_Name, $start, $length);
$course_name_highlighted = str_ireplace($new_search, '<b>' .$new_search . '</b>' , $row->Course_Name);
$results[]= array(
'Course_Name' => $row->Course_Name,
'FirstName' => $row->FirstName,
'LastName' => $row->LastName,
'COURSE_ID' => $row->COURSE_ID,
'course_name_highlighted' => $course_name_highlighted
);
}
}
if ( $this->input-> is_ajax_request())
{
$this->output->set_header("Cache-Control: no-cache, must-revalidate");
$this->output->set_header("Expires:Mon, 4 Apr 1994 04:44:44 GMT");
$this->output->set_header("Content-type:application/json");
echo json_encode($results);
}
else
{
$data['results'] = $results;
$this->load->view('ajax_search', $data);
}
}
}
和 Axaj_model 代码:
<?php
Class Ajax_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function search_course($like_search)
{
$this->db->select('FirstName, LastName, COURSE_ID, Course_Name, Semester,Time,Book, SECTION_ID');
$this->db->from('Section');
$this->db->join('faculty', 'faculty.FACULTY_ID = section.FACULTY_ID');
$this->db->like('Course_Name', $like_search);
$this->db->order_by('Course_Name');
$query = $this -> db->get();
//If it is all correct
$results=array();
if($query -> num_rows() > 0 )
{
return $query->result();
}
else
{
return false;
}
}
}