如何将变量从模型传递到控制器

时间:2012-08-27 08:33:50

标签: php codeigniter

我得到的结果变量在我的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;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

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

$results = array();

在函数'index'的开头。

您应该阅读variable scope

答案 1 :(得分:1)

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

<强> MODEL

$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
    }
}

答案 2 :(得分:0)

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

function index(){
     $result = array ();
}