Welcome Guest, Not a member yet? Register   Sign In
Model working in one controller, but not in another.
#1

[eluser]mdrisser[/eluser]
Hi all. I have a rather perplexing problem. I have a Model with to functions each does a select * on a different table. I'm using these values to populate two select lists. In one controller everything works as expected, in a second controller, using the exact same code, it doesn't work. That is, it doesn't return anything.

Here's the code:

In both controllers:
Code:
$this->load->model('Search_model', 'Search');
$this->data['types'] = $this->Search->get_types();
$this->data['cities'] = $this->Search->get_cities();

And the Model is:
Code:
class Search_model extends Model {
    function Search_model() {
        parent::Model();
    }
    
    function get_types() {
        $query = $this->db->query('SELECT * FROM types ORDER BY types ASC');
        return $query->result_array();
    }
    
    function get_cities() {
        $query = $this->db->query('SELECT * FROM cities ORDER BY cities ASC');
        return $query->result_array();
    }
}

Function in 1st controller, which works fine:
Code:
function search() {
        if($this->input->post('term') || $this->input->post('types[]') || $this->input->post('cities[]')) {
            $this->data['search_term'] = '';
            if($this->input->post('term')) {
                $this->data['search_term'] .= ' '.$this->input->post('term');
            }
            if($this->input->post('types[]')) {
                $this->data['search_term'] .= ' '.$this->input->post('types[]');
            }
            if($this->input->post('cities[]')) {
                $this->data['search_term'] .= ' '.$this->input->post('cities[]');
            }
            
            $this->data['search'] = true;
            
            $this->db->select('id, name, address, summary, link, img_thumb, img');
            
            if($this->input->post('term')) {
                $term = $this->input->post('term');
                
                $this->db->like('name', $term);
                $this->db->orlike('address', $term);
                $this->db->orlike('summary', $term);
                $this->db->orlike('img', $term);
            }
            
            if($this->input->post('types[]')) {
                $types = $this->input->post('types[]');
                foreach($types as $type) {
                    $this->db->orlike('types', $type);
                }
            }
            
            if($this->input->post('cities[]')) {
                $cities = $this->input->post('cities[]');
                foreach($cities as $city) {
                    $this->db->orlike('cities', $city);
                }
            }
            
            $this->db->limit(50);
            $this->db->orderby('name', 'asc');
            $results = $this->db->get('places');
            
            if($results->num_rows() > 0) {
                foreach($results->result_array() as $row) {
                    $this->data['places'][] = $row;
                }
            }
            
            $results->free_result();
            
            $this->load->view('places_view', $this->data);
        } else {
            $this->data['search'] = false;
                    
                        // This works here
            $this->load->model('Search_model', 'Search');
                $this->data['types'] = $this->Search->get_types();
                $this->data['cities'] = $this->Search->get_cities();
            
            $this->load->view('search_view', $this->data);  
        }
    }

Function in 2nd Controller, which doesn't work:
Code:
function process_form() {
        $validation_rules = array(
            array(
                'field' => 'placeName',
                'label' => 'Name',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'placeAddress',
                'label' => 'Address',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'placeDesc',
                'label' => 'Summary',
                'rules' => 'trim'
            ),
            array(
                'field' => 'placeUrl',
                'label' => 'URL',
                'rules' => 'trim|prep_url'
            )
        );
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules($validation_rules);
        
        if($this->form_validation->run() === FALSE) {
                        // This doesn't work here
            $this->load->model('Search_model', 'Search');
                $this->data['types'] = $this->Search->get_types();
                $this->data['cities'] = $this->Search->get_cities();
            
            $this->load->view('addplace_view', $this->data);
        } else {
            $this->load->view('success_view', $this->data);
        }
    }
Trying a print_r() on both $cities and $types in the view loaded by the 2nd controller results in:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: types

Filename: views/addplace_view.php

Line Number: 19
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: cities

Filename: views/addplace_view.php

Line Number: 20

I'm sure I must be doing something wrong, but I can't for the life of me figure out what it is.


Messages In This Thread
Model working in one controller, but not in another. - by El Forum - 12-29-2008, 01:56 PM
Model working in one controller, but not in another. - by El Forum - 12-29-2008, 03:14 PM
Model working in one controller, but not in another. - by El Forum - 12-29-2008, 04:33 PM
Model working in one controller, but not in another. - by El Forum - 12-29-2008, 05:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB