[eluser]awpti[/eluser]
So, I'm now on month 1 of CodeIgniter. Wow.
I'll post my Model and Controller (Only have one of each, for now) here.
What I'm specifically looking for:
Comments.
Be blunt, I can take it.
Tell me I'm doing something completely stupid if I am, in fact, doing something completely stupid.
I currently use Coolfactor's awesome View library.
So, without further adeu:
controller/blog.php
Code:
class Blog extends Controller {
private $awTitle = 'awpti.org - Where geeks roam free!';
private $awHeader = 'awpti.org';
function Blog() {
parent::Controller();
$this->load->model('blogmodel', 'blog', TRUE);
//Set the header/footer template
$this->view->part('header', 'common/header.php');
$this->view->part('footer', 'common/footer.php');
}
function index() {
$this->view->set('Title', $this->awTitle);
$this->view->set('Header', $this->awHeader); //need to rename this variable. maybe later
$this->view->set('Query_News', $this->blog->get_latest_news(3));;
$this->view->load('blog/front');
}
function comments() {
$this->view->set('Title', $this->awTitle.' (Comments)');
$this->view->set('Header', $this->awHeader);
if( !$this->uri->segment(2) || !ctype_digit($this->uri->segment(2)) ) {
$this->view->set('Query_News', $this->blog->get_latest_news(1));
$this->view->set('Query_Comments', $this->blog->get_comments());
} else {
$this->view->set('Query_News', $this->blog->get_news_by_id( $this->uri->segment(2) ));
$this->view->set('Query_Comments', $this->blog->get_comments( $this->uri->segment(2) ));
}
$this->view->set('CommentID', $this->uri->segment(2));
$this->view->load('blog/comments');
}
/*
Code snipped for brevity, nothing to improve here as they are placeholders/static content
*/
} //end class. Duh.
And..
models/blogmodel.php
Code:
class Blogmodel extends Model {
function Blogmodel() {
parent::Model();
}
function get_latest_news($limit) {
$news = $this->db->query('SELECT *,
IF( awNewsComments.comments_news_id IS NOT NULL, count( * ), 0 ) AS num_comments
FROM awNews
LEFT JOIN awNewsComments ON awNews.news_id = awNewsComments.comments_news_id
GROUP BY awNews.news_id ORDER BY awNews.news_id DESC
LIMIT '.$limit);
return $news->result();
}
function get_comments($news_id = '') {
if(!$news_id) {
$query_comments = $this->db->query("SELECT * FROM awNewsComments WHERE comments_news_id = (SELECT MAX(news_id) FROM awNews) ORDER BY comments_id DESC");
} else {
$query_comments = $this->db->query("SELECT * FROM awNewsComments WHERE comments_news_id = {$news_id} ORDER BY comments_id DESC");
}
return $query_comments->result();
}
function get_news_by_id($news_id) {
$news = $this->db->query('SELECT * FROM awNews WHERE news_id = '.$news_id);
return $news->result();
}
}
I attempted to avoid any sort of logic in the models, but I couldn't devise any way to do it without creating a bunch of fluff, redundant functions.
To recap: Blast me as hard as you can, but throw some lessons in there, if possible.