Reputation: 2755
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?
Upvotes: 1
Views: 1980
Reputation: 446
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')
Upvotes: 1