Getting there.
I have modified your code so that I can use a "slug" field to generate the url part.
Code:
<?php
class News_model extends CI_Model {
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
// Grab the data from the database.
$result = $this->db
->select(array(
'a.article_id as a_id',
'a.user_id AS a_user_id',
'a.content AS a_content',
'a.slug AS a_slug',
'a.title AS a_title',
'a.created_on as a_created_on',
'au.username AS a_username',
'c.comment_id AS c_comment_id',
'c.comment AS c_comment',
'c.user_id AS c_user_id',
'cu.username AS c_username',
'c.created_on AS c_created_on'
))
->from('articles AS a')
->join('comments AS c', 'a.article_id = c.article_id', 'left')
->join('users AS au', 'au.user_id = a.user_id', 'left')
->join('users AS cu', 'cu.user_id = c.user_id', 'left')
->get()->result_array();
// Do we have a result?
if ( ! $result)
{
return FALSE;
}
// Now we need to format the array for the view.
$ret = array();
foreach ($result as &$res)
{
$article_id = $res['a_id'];
// If the article hasn't been added to the return array, add it now.
if ( ! isset($ret[$article_id]))
{
$ret[$article_id] = array(
'a_id' => $article_id,
'a_user_id' => $res['a_user_id'],
'a_username' => $res['a_username'],
'a_created_on' => $res['a_created_on'],
'a_title' => $res['a_title'],
'a_content' => $res['a_content'],
'a_slug' => $res['a_slug'],
'a_comments' => array(),
);
}
}
// Free up some memory.
unset($result);
// Return the array we just built.
return $ret;
}
$query = $this->db->get_where('articles', array('slug' => $slug));
// Grab the data from the database.
$result = $this->db
->select(array(
'a.article_id as a_id',
'a.user_id AS a_user_id',
'a.content AS a_content',
'a.slug AS a_slug',
'a.title AS a_title',
'a.created_on as a_created_on',
'au.username AS a_username',
'c.comment_id AS c_comment_id',
'c.comment AS c_comment',
'c.user_id AS c_user_id',
'cu.username AS c_username',
'c.created_on AS c_created_on'
))
->from('articles AS a')
->join('comments AS c', 'a.article_id = c.article_id', 'left')
->join('users AS au', 'au.user_id = a.user_id', 'left')
->join('users AS cu', 'cu.user_id = c.user_id', 'left')
->get_where('articles', array('a.slug' => $slug))->result_array();
// Do we have a result?
if ( ! $result)
{
return FALSE;
}
// Now we need to format the array for the view.
$ret = array();
foreach ($result as &$res)
{
$article_id = $res['a_id'];
// If the article hasn't been added to the return array, add it now.
if ( ! isset($ret[$article_id]))
{
$ret[$article_id] = array(
'a_id' => $article_id,
'a_user_id' => $res['a_user_id'],
'a_username' => $res['a_username'],
'a_created_on' => $res['a_created_on'],
'a_title' => $res['a_title'],
'a_content' => $res['a_content'],
'a_slug' => $res['a_slug'],
'a_comments' => array(),
);
}
// If we have a comment, add it to the article.
if ($res['c_comment'])
{
$ret[$article_id]['comments'][] = array(
'c_id' => $res['c_comment_id'],
'c_user_id' => $res['c_user_id'],
'c_username' => $res['c_username'],
'c_created_on' => $res['a_created_on'],
'c_comment' => $res['c_comment'],
);
}
}
// Free up some memory.
unset($result);
// Return the array we just built.
return $ret;
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'content' => $this->input->post('content')
);
return $this->db->insert('news', $data);
}
}