Welcome Guest, Not a member yet? Register   Sign In
Join Query Help
#15

[eluser]Lee.[/eluser]
Getting there.

I have modified your code so that I can use a "slug" field to generate the url part.

I have this in my controller;

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);
}
}



Messages In This Thread
Join Query Help - by El Forum - 05-28-2013, 07:58 AM
Join Query Help - by El Forum - 05-28-2013, 10:56 AM
Join Query Help - by El Forum - 05-28-2013, 11:39 AM
Join Query Help - by El Forum - 05-28-2013, 11:54 AM
Join Query Help - by El Forum - 05-28-2013, 12:00 PM
Join Query Help - by El Forum - 05-28-2013, 12:00 PM
Join Query Help - by El Forum - 05-28-2013, 12:05 PM
Join Query Help - by El Forum - 05-28-2013, 01:09 PM
Join Query Help - by El Forum - 05-28-2013, 02:42 PM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 05:10 AM
Join Query Help - by El Forum - 05-29-2013, 12:35 PM
Join Query Help - by El Forum - 05-29-2013, 12:36 PM
Join Query Help - by El Forum - 05-29-2013, 12:39 PM
Join Query Help - by El Forum - 05-30-2013, 09:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB