Welcome Guest, Not a member yet? Register   Sign In
Basic M > C > V Problem - can't see where I am going wrong
#1

[eluser]Chad Crowell[/eluser]
In my Model:

Code:
<?php

class Information_model extends Model {

    function Information_model()
    {
        
        parent::Model();
        
    }


    function get_faq_topics() {
        
        $query = $this->db->get('faq_topics');
        return $query->result();
        
    }

}

?>


In my Controller:
Code:
<?php
class Information extends Controller {

    function Information() {
        
        parent::Controller();
        
        $this->load->model('Information_model');

    }
    

    function index() {
        
        
        
    }
    
    
    function faq() {
        
        $data['result'] = $this->Information_model->get_faq_topics();
        $this->load->view('information/faq', $data);
        
    }    
    
}
?>


In my View:
Code:
<body>

    <h1>Hello</h1>
    
    &lt;?php
    foreach($result as $row) {
      echo '<p>'.$row['question'].'</p>';
    }
    ?&gt;
    
&lt;/body&gt;

When I didn't have the model involved, it worked just fine. When I move the foreach loop to the model, it outputs the questions. As is, I get the h1 Hello /h1 but nothing beyond that. Somehow I am doing something simple wrong. This always happens and I never get farther than my first query in CI before I quit and outsource the project... help!
#2

[eluser]eilrahc[/eluser]
The only blatantly off-kilter thing I see at first glance is that "Information_model" should be all lowercase in your controller. If the model filename contains an uppercase 'I', try making that lowercase as well. I don't know if CI really cares, but that's the convention anyway.

I understand the overall frustration of getting started, I'm in much the same boat. Smile
#3

[eluser]erik.brannstrom[/eluser]
Hi!

If I'm not mistaken, the active records method result() returns an object. Try $row->question instead of $row['question'] in your view. Hope this helps!

Best regards,
Erik
#4

[eluser]Colin Williams[/eluser]
Alternatively, you can return $query->result_array() from your model method.
#5

[eluser]Chad Crowell[/eluser]
erik thanks, your solution was the one. You guys all rock! Let's hope page 2 goes better!




Theme © iAndrew 2016 - Forum software by © MyBB