Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter App getting data from the wrong mySQL table?
#1

[eluser]Dave Blencowe[/eluser]
Hey,

Recently I have been coding the Blog section to my personal site which is based on Codeigniter and have run across a problem.
I have made a model for getting things like posts etc from the database and then have the controller use the model to get the appropriate results yet when I ask for data from one table for comments it returns another table altogether and I just can't see why.

The section of the model
Code:
function get_blog($id, $limit = NULL)
    {
        
        $this->db->order_by("id", "desc");
        if($limit !== NULL)
        {
            $this->db->limit($limit);
        }
        
        if($id)
        {
            $this->db->where('id', $id);
        }
        $blog = $this->db->get('content_blog');
        return $blog->row_array();
    }
    
    function get_comments($id)
    {
        $this->db->order_by('id', 'desc');
        $this->db->where('post_id', $id);
        $comments = $this->db->get('content_test');
        return $comments->row_array();
    }

The section of the controller
Code:
function comments()
    {
        $this->load->view('template/default/header');
        
        $id = $this->uri->segment(3);    
        $get_blog = $this->mdl_blog->get_blog($id, '1');        

        $get_comments = $this->mdl_blog->get_comments($id, NULL);

        $this->load->view('blog/comments/main_view', $get_blog);
        $this->load->view('blog/comments/comments_view', $get_comments);
        $this->load->view('blog/comments/comments_add');
        $this->load->view('template/default/footer');
    }

These two views seem to have the the $get_blogs variable passed to them each time...
Code:
$this->load->view('blog/comments/main_view', $get_blog);
        $this->load->view('blog/comments/comments_view', $get_comments);


I've attached the files in case they are any use and I thank you for any help you might be able to provide.

Thanks,
Dave
#2

[eluser]xwero[/eluser]
And you get result form which other table?
#3

[eluser]Dave Blencowe[/eluser]
content_blog instead of content_test
#4

[eluser]xwero[/eluser]
And you are sure
Code:
$get_comments = $this->mdl_blog->get_comments($id, NULL);
Isn't
Code:
$get_comments = $this->mdl_blog->get_blog($id, NULL);
Because there is no second parameter in your get_comments method.
#5

[eluser]Dave Blencowe[/eluser]
Yeah I am sure, Have removed that extra NULL now as it was needed earlier before I edited the code a bit. Still having the same problem Sad.
If you want me to do something like post the whole project somewhere I could?
#6

[eluser]xwero[/eluser]
You can try to debug it with $this->db->last_query() to see if the content_test sql statement actually runs
#7

[eluser]Dave Blencowe[/eluser]
I'm just watching Stargate at the moment so I'll give that a try in about 20 minutes Big Grin

Thanks a lot dude,
Dave
#8

[eluser]Dave Blencowe[/eluser]
Yeah, the last query run is:
Code:
SELECT * FROM (`content_test`) WHERE `post_id` = '1' ORDER BY `id` desc
#9

[eluser]xwero[/eluser]
So get_comments should contain that data? You can check by using print_r.
#10

[eluser]Dave Blencowe[/eluser]
print_r($get_comments); gives me Array ( )




Theme © iAndrew 2016 - Forum software by © MyBB