0

I'm getting that results variable is undefined in my Ajaxcont Controller. I am not exactly sure how to pass $results back from the model into the controller. i just want my $results array to hold all the value that was retrieved by the query. What am i doing wrong?


<?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);
        }
    }
}

And the Axaj_model code:

<?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
  • Your results array should be declared outside of the 'if' block, now it is undefined at the point where you echo it Commented Aug 27, 2012 at 8:36
  • Are you getting this when there are no query results? Commented Aug 27, 2012 at 8:39
  • Could you share with us where in your code you get the "variable is undefined" notice? Commented Aug 27, 2012 at 8:47

3 Answers 3

1

The model is returning a multi-dimensional array. so you should use instance to retrieve the data.

MODEL

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

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

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

 }

else
{
    return false;
}

Controller

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
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

edited a small mistake..check the code now and report the output
now i'm getting Message: Invalid argument supplied for foreach()
try foreach($result as $row)
if you want to check the output returned from model, add print_r($query['result']) after $query['result']= $this->ajax_model->search_course($like_search);
thanks for trying but it's still not returning anything nor is the print statement working.
|
1

Your results array should be declared outside of the 'if' block, now it is undefined at the point where you echo it. Add this:

$results = array();

at the beginning of your function 'index'.

You should read up on variable scope

2 Comments

yes, you are correct i left out this line, however that it still does not return any data. I think i'm not returing data back from my model correctly how can i debug and see what i'm returning?
@Undermine2k No need to declare that. You are not retrieving the result right.As it is a multi-dimensional array, you have to use instance
0

On the top of the index function you should declare the result.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.