0

我得到我的 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;
        }
    }
}
4

3 回答 3

1

你的结果数组应该在'if'块之外声明,现在它在你回显它的地方是未定义的。添加这个:

$results = array();

在函数“索引”的开头。

您应该阅读变量范围

于 2012-08-27T08:38:07.600 回答
1

该模型正在返回一个多维数组。所以你应该使用实例来检索数据。

模型

$results=array(); // you don't even have to declare it

if($query -> num_rows() > 0 )
{

 $result= $query->result();
return $result;

 }

else
{
    return false;
}

控制器

function index()

 {

  $search = $this->input->post('search', TRUE);
  $like_search= '%' .$this->db->escape_like_str($search) . '%';
  $query['result']=  $this->ajax_model->search_course($like_search);

if ($query)
{
    foreach($query['result'] as $row)
    {
     //stuff here
    }
}
于 2012-08-27T08:44:39.183 回答
0

在 index 函数的顶部,您应该声明结果。

function index(){
     $result = array ();
}
于 2012-08-27T08:42:24.020 回答