Welcome Guest, Not a member yet? Register   Sign In
Displaying menu based on num_rows
#1

[eluser]bcarter[/eluser]
Hi there

Real noob question here. I'm trying to display a sub menu if num_rows > 0 but it's not working. When I view the page it says...

Quote:Fatal error: Call to a member function num_rows() on a non-object

Here's the code...

Model
Code:
class Subnav_model extends Model {
    
    function Subnav_model() {
        
        parent::Model();
    }
    
    function subnav($id){
        
        $query2 = $this->db->get_where('sub_content', array('section' => $id));
        return $query2->result();
        return $query2->num_rows();
    }
}

View
Code:
<?php if ($query2->num_rows() > 0) { ?>
            
            <h2>Content Area</h2>
            
            <ul>
                &lt;?php foreach($query2 as $row) {?&gt;
                
                <li><a >uri->segment(1) ?&gt;/information/&lt;?php echo $row->sub_content_ID ?&gt;">&lt;?php echo $row->sub_content_title ?&gt;</a></li>
                
                &lt;?php } ?&gt;
            </ul>
            
            &lt;?php } //end of if num_rows ?&gt;

Controller
Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();    
    }
    
    function index() {
        
        $id = '1';
        $data['page_title'] = "Welcome to Rebus Training Ltd";
        
        $this->load->model('Nav_model');
        $data['query'] = $this->Nav_model->nav();
        //$this->load->view('head', $data);
        
        $this->load->model('Subnav_model');
        $data['query2'] = $this->Subnav_model->subnav($id);
        
        $this->load->view('head', $data);
        
        $this->load->view('home_view', $data);
    }
}

Can anyone help? I know it's a massively basic question!!!

Thanks a lot!
#2

[eluser]WanWizard[/eluser]
Subnav returns the result before it can return num_rows(). Is that intentional?
#3

[eluser]bcarter[/eluser]
No it's not intentional but I put num_rows() before returning result() and it gives me the same error message.

Thanks
#4

[eluser]danmontgomery[/eluser]
Query results are only ever non-objects if there's an invalid query. Enable the profiler to see what the SQL being run is to determine what's wrong.
#5

[eluser]erik.brannstrom[/eluser]
The methods num_rows and result should both be used on the query object. Try the following.

Code:
// Replace this method in model:
function subnav($id){
  return $this->db->get_where('sub_content', array('section' => $id));
}

// In your view, set the foreach to this:
&lt;?php foreach($query2->result() as $row) { ?&gt;
#6

[eluser]Zack Kitzmiller[/eluser]
Updated your code to work. It's not pretty, but it get's the job done. You can't return twice in one function. 'return' exits the function.

You were returning the 'result' object from the model, not the query object. and you can't run 'num_rows()' on the result object.

I just return the object if there are rows in the model, and check to see if it's null/notnull in the view.

[quote author="bcarter" date="1272925080"]Hi there

Real noob question here. I'm trying to display a sub menu if num_rows > 0 but it's not working. When I view the page it says...

Quote:Fatal error: Call to a member function num_rows() on a non-object

Here's the code...

Model
Code:
class Subnav_model extends Model {
    
    function Subnav_model() {
        
        parent::Model();
    }
    
    function subnav($id){
        
        $query2 = $this->db->get_where('sub_content', array('section' => $id));
        if ($query2->num_rows() > 0) {
            return $query2->result();
        } else {
            return false;
        }

    }
}

View
Code:
&lt;?php if (!empty($query2)) { ?&gt;
            
            <h2>Content Area</h2>
            
            <ul>
                &lt;?php foreach($query2 as $row) {?&gt;
                
                <li><a >uri->segment(1) ?&gt;/information/&lt;?php echo $row->sub_content_ID ?&gt;">&lt;?php echo $row->sub_content_title ?&gt;</a></li>
                
                &lt;?php } ?&gt;
            </ul>
            
            &lt;?php } //end of if num_rows ?&gt;

Controller
Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();    
    }
    
    function index() {
        
        $id = '1';
        $data['page_title'] = "Welcome to Rebus Training Ltd";
        
        $this->load->model('Nav_model');
        $data['query'] = $this->Nav_model->nav();
        //$this->load->view('head', $data);
        
        $this->load->model('Subnav_model');
        $data['query2'] = $this->Subnav_model->subnav($id);
        
        $this->load->view('head', $data);
        
        $this->load->view('home_view', $data);
    }
}

Can anyone help? I know it's a massively basic question!!!

Thanks a lot![/quote]




Theme © iAndrew 2016 - Forum software by © MyBB