Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am starting to learn codeigniter. I get this error while trying to run the project.

Parse error: syntax error, unexpected '$this' (T_VARIABLE), expecting ';' or '{' in C:\xampp\htdocs\Codeigniter\application\models\post.php on line 5

My Model-post.php:

class Post extends CI_Model{
    function get_posts($num=20, $start=0)
        $this->db->select()->from('posts')->where('active',1)->order_by('date_added','desc')->limit($start,$num);
        $query=$this->db->get();
        return $query->result_array();
    }
}

My Controller-posts.php:

<?php
class Posts extends CI_Controller{
    function index(){
        $this->load->model('post');
        $data['posts'] = $this->post->get_posts();
        print_r($data['posts']);
    }
}
share|improve this question
    
Compare this function header: function get_posts($num=20, $start=0) with this one: function index(){ – Rizier123 Feb 18 '15 at 10:10
1  
I think the error is on this line: function get_posts($num=20, $start=0). You declare a function without opening the brace. Add the opening brace like this function get_posts($num=20, $start=0) { It's surprising you didn't open the brace, yet you are closing it on the second-to-last line. – Michael Woyo Feb 18 '15 at 10:11
up vote 1 down vote accepted
function get_posts($num=20, $start=0)

You're missing the brace from this function. It should be;

function get_posts($num=20, $start=0) {
share|improve this answer
    
I dunno why have people downvoted your answer. – Linesofcode Feb 18 '15 at 10:51
    
I thought the same. – Craig Feb 18 '15 at 10:53
2  
@AdrienXL wow this answer get's 5 upV for only a typo, no additional errors or anything else to explain or so. To bad that I don't get 35 upV for spotting 7 errors when I'm answering questions. – Rizier123 Feb 18 '15 at 12:00
2  
Also i just will post this here, since it seems that not many people read this: Vote for close -> off-topic: This question was caused by a problem that can no longer be reproduced or a simple typographical error While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting. – Rizier123 Feb 18 '15 at 12:04
2  
@AdrienXL Yes, you shouldn't down vote a answer on a bad question only because of the question, that's right! (meta.stackoverflow.com/q/255459/3933332) But, this answer is neither good nor helpful. Even the error message alone would be a better answer here. Why? 1. It's only a single and simple typo in OP's code nothing more 2. This can be googled in under 1 sec and there are tunes of duplicates already on SO for this 3. It has no explanation (Also just to note here: You even see that OP understands how to write the code right since he did it right in another place) – Rizier123 Feb 18 '15 at 12:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.