1

I am very new to codeigniter and try to write a simple blog app:

controller: posts.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','dec')->limit(0,20);
        $query = $this->db->get('posts');
        return $query->result_array();

        }

}

Model: post.php

class Posts extends CI_Controller {
    function index() {
        $this->load->model('post');
        $data['posts'] = $this->post->get_posts();
        echo"<pre>";print_r($data['posts']);echo"</pre>";
    }
}

I have populated posts table in the database. However when I try to get results I get this instead:

Array
(
)

mysql> describe posts;

+------------+--------------+------+-----+-------------------+-----------------------------+
| Field      | Type         | Null | Key | Default           | Extra                       |
+------------+--------------+------+-----+-------------------+-----------------------------+
| postID     | int(10)      | NO   | PRI | NULL              | auto_increment              |
| title      | varchar(255) | NO   |     | NULL              |                             |
| post       | text         | NO   |     | NULL              |                             |
| date_added | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| userID     | int(4)       | NO   |     | NULL              |                             |
| active     | tinyint(1)   | NO   |     | NULL              |                             |
| slug       | varchar(255) | NO   |     | NULL              |                             |
+------------+--------------+------+-----+-------------------+------------------

What's wrong with this code and how can I fix it?

1 Answer 1

1

Because your query is getting wrong as you already have ->from('posts') so you dont need the table name any more in get

Change

$query = $this->db->get('posts');

With

$query = $this->db->get();

And

->order_by('date_added','desc')
Sign up to request clarification or add additional context in comments.

4 Comments

You should able to see your last query using echo $this->db->last_query() after $this->db->get() to make sure your query is correct
replacing db->get('posts') with db->get() did not solve the problem. however when I commented out //->order_by('date_added','dec')->limit(0,20); the array contents appered. Any ideas why it was so?
can you check the column name date_added is correct, i checked the query and its working for me. What you get on echo $this->db->last_query() after get();
i got it what your problem is ->order_by('date_added','desc') not -->order_by('date_added','dec')`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.