Welcome Guest, Not a member yet? Register   Sign In
Create blog comment, attatch user id and article id?
#11

[eluser]davehedgehog[/eluser]
heres the entire model =)

Code:
<?php
class Blog_model extends CI_Model{

function __construct()
{
  $this->load->database();
}

function get_blog($slug = FALSE)
{
  if($slug === FALSE)
  {
   $query = $this->db->get('blog');
   return $query->result_array();
  }

  $query = $this->db->get_where('blog',array('slug' => $slug));
  return $query->row_array();
}

function set_blog()
{
  $this->load->helper('url');

  $slug = url_title($this->input->post('title'),'dash',TRUE);

  $data = $this->upload->data();

  $data = array(
   'date'  => date("Y-m-d H:i:s"),
   'title' => $this->input->post('title'),
   'slug'  => $slug,
   'post'  => $this->input->post('post'),
   'category'  => $this->input->post('category'),
   'image'  => $filepath = 'uploads/blog/'.$data['file_name']
  );
  
  return $this->db->insert('blog',$data);
}

public function get_comments($id)
{
  $query = $this->db->get_where('comment',array('entry_id' => $id));
  return $query->result_array();
}

public function set_comment()
{
  $this->load->helper('url');
  
  $user = $this->session->userdata('username');

  $data = array(
   'entry_id' => $this->input->post('$id'),
   'name'  => $user,
   'comment'  => $this->input->post('comment'),
   'date'  => date("Y-m-d H:i:s"),
  );
  
  return $this->db->insert('comment',$data);
}
}
#12

[eluser]TheFuzzy0ne[/eluser]
As usual, none of my code has actually been tested -- it's purely conceptual.

OK, so first, it would be good if we can make your model method accept both an ID or a slug to make it more robust. Also, you're passing in the entry ID from the form, but we don't need to, since it's already set in the URI.
Code:
class Blog_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        
        $this->load->database();
    }
    
    function get_blog($id = FALSE)
    {
        // Do we have an ID?
        if ( ! $id)
        {
            return FALSE;
        }
        
        // If the ID is numeric, query by ID, otherwise, query by slug.
        $this->db->where((is_numeric($id)) ? 'id' : 'slug', $id);

        // Query the database.
        $res = $this->db->get('blog');
        
        // If we have no rows, return FALSE.
        if ( ! $res->num_rows())
        {
            return FALSE;
        }
        
        // Otherwise return the row.
        return $res->row();
    }

    public function get_comments($id = 0)
    {
        $res = $this->db
            ->where('entry_id', $id)
            ->get_where('comment');
            
        return $res->result_array();
    }

    public function set_comment($id = 0)
    {
        // If we don't have an ID, don't insert anything.
        if ( ! $id)
        {
            return FALSE;
        }
        
        $data = array(
            'entry_id' => $id,
            // Assuming users have to be signed in to post a comment, you should
            // be using the user's ID here, not their name.
            'name'  => $this->session->userdata('username'),
            'comment'  => $this->input->post('comment'),
            'date'  => date("Y-m-d H:i:s"),
        );

        return $this->db->insert('comment', $data);
    }
}

For SEO purposes, we should redirect using the slug if the ID is numeric.
Code:
function comment($slug = '')
{
    // Try to get the entry from the database.
    $entry = $this->blog_model->get_blog($slug);
    
    // Do we have a result?
    if ( ! $entry)
    {
        // I would suggest finding an alternative method to use here.
        // Perhaps you could redirect back to the blog home page with
        // a flash data message. But for now, show_error() will do.
        show_error('unable to locate blog entry');
    }
    
    // If the slug is numeric, redirect to the correct URI.
    if (is_numeric($slug))
    {
        redirect('/blog/comment/' . $entry['slug']);
    }
    
    // Load helpers and libraries (the form helper is loaded by the validation library).
    $this->load->helper('url');
    $this->load->library('form_validation');

    // Set the validation rules.
    $this->form_validation->set_rules('comment', 'Comment', 'required');

    // Attempt to run validation.
    if($this->form_validation->run())
    {
        // Insert the comment into the database. (We pass the blog ID to the
        // method for simplicity).
        $this->blog_model->set_comment($entry['id']);
        
        // This will be executed when validation PASSES, so I don't think you
        // should be setting a flashError here, but you should definitely be
        // setting a message to confirm that the comment was added successfully.
        // $this->session->set_flashdata('flashError', 'Comment entry :)');
        
        // Redirect back to the current blog.
        redirect('/blog/view/' . $entry['slug']);
    }
    
    // Load the view variables.
    $data['title'] = 'Comment';
    $this->load->vars($data);
    
    // Display the view
    $this->load->view('templates/header');
    $this->load->view('templates/blog/blogA');
    $this->load->view('blog/comment');
    $this->load->view('templates/blog/blogB');
    $this->load->view('templates/footer');
}

You might be fortunate enough for that to work, but as you can see I've tinkered with it a bit. I would suggest you use this structure for now, and make it work. It avoids some of the design problems introduced by your current structure, but it's by no means perfect.

Please study the code and understand what it's doing and why. If you don't understand anything, post back, and I'll be happy to explain.

Your view() method will probably benefit from checking the slug, and redirecting if it's been passed an ID, too. You could put that code into a private controller method, so that you can reuse it without repeating yourself.

For example:
Code:
function _get_blog($id, $redirect_uri)
{
    // Do we have a result?
    if ( ! $entry)
    {
        // I would suggest finding an alternative method to use here.
        // Perhaps you could redirect back to the blog home page with
        // a flash data message. But for now, show_error() will do.
        show_error('unable to locate blog entry');
    }
    
    // If the slug is numeric, redirect to the correct URI.
    if (is_numeric($slug))
    {
        redirect(rtrim($redirect_uri, '/') . '/' . $entry['slug']);
    }
    
    return $entry;
}

So in your controller, your methods would become:
Code:
function view($slug = '')
{
    $entry = $this->_get_blog($slug, '/blog/view');
    
    /* ... */
}

function comment($slug = '')
{
    $entry = $this->_get_blog($slug, '/blog/comment');
    
    /* ... */
}

After that, we can safely assume that we have the valid blog entry, since our _get_blog() method checks to make sure everything is as we expect.

You could probably do with adding another private controller method to save you from having to repeat that f'ugly view loading code in each controller method. Remember, if you find yourself repeating yourself, you've probably just put your code in the wrong place for the second time. Wink

Hope this helps.
#13

[eluser]davehedgehog[/eluser]
what I dont understand is~
Code:
//if Im using the correct $id by here
public function get_comments($id)
{
  $query = $this->db->get_where('comment',array('entry_id' => $id));
  return $query->result_array();
}

public function set_comment()
{
  $this->load->helper('url');
  
  $user = $this->session->userdata('id');

  $data = array(
//then why cant I just use that same ID here
   'entry_id' => $id,
   'name'  => $user,
   'comment'  => $this->input->post('comment'),
   'date'  => date("Y-m-d H:i:s"),
  );
  
  return $this->db->insert('comment',$data);
}
#14

[eluser]TheFuzzy0ne[/eluser]
How would your method know what $id is? In get_comments you're passing the ID to the method. In set_comment $id is unset, you you either need to pass it in somehow, or extract it from the URL.

Functions and method have absolutely no idea about any variables defined outside of them. This means that they either need to be passed in as a parameter, or imported using the "global" keyword, which is find for procedural programming, but it's ugly, and unnecessary most of the time.

If you wanted to, you could add a method which will allow you to set the ID, and your method can work from that.
Code:
class Blog_model extends CI_Model {

    protected $blog_id = 0;
    
    public function set_id($blog_id = 0)
    {
        $this->blog_id = $blog_id;
    }

    public function get_comments()
    {
        $query = $this->db->get_where('comment',array('entry_id' => $this->blog_id));
        return $query->result_array();
    }

    public function set_comment()
    {
        $this->load->helper('url');

        $user = $this->session->userdata('id');

        $data = array(
            //then why cant I just use that same ID here
            'entry_id' => $this->blog_id,
            'name'  => $user,
            'comment'  => $this->input->post('comment'),
            'date'  => date("Y-m-d H:i:s"),
        );

        return $this->db->insert('comment',$data);
    }
}

So from your controller:
Code:
$this->blog_model->set_id($id);
$data = array(
    'blog'     => $this->blog_model->get_blog(),
    'comments' => $this->blog_model->get_comments(),
);

Does that help?
#15

[eluser]davehedgehog[/eluser]
Na if I do it that way it still doesnt get the id plus it just shows all my comments as id 0.
#16

[eluser]TheFuzzy0ne[/eluser]
That's because you haven't changed $id to $this->blog_id.
#17

[eluser]davehedgehog[/eluser]
I have and the set id just doesnt seem to work and just keep getting 0 :/
#18

[eluser]TheFuzzy0ne[/eluser]
Show me your code. I suspect you think you've set it, but you don't understand how variable scope works

If that's the case, please see here: http://php.net/manual/en/language.variables.scope.php

Also, please check your private messages.
#19

[eluser]davehedgehog[/eluser]
heres the controller~
Code:
<?php

include APPPATH.'core/Common_Auth_Controller.php';

class Blog extends Common_Auth_Controller{

public function __construct()
{
  parent::__construct();
  $this->load->model('blog_model');
}

public function index()
{
  $data['blog'] = $this->blog_model->get_blog();
  $data['title'] = 'blog archive';

  $this->load->view('templates/header');
  $this->load->view('templates/blog/blogA');
  $this->load->view('blog/index', $data);
  $this->load->view('templates/blog/blogB');
  $this->load->view('templates/footer');
}

public function view($slug)
{
  $data['blog_item']=$this->blog_model->get_blog($slug);
  
  if(empty($data['blog_item']))
  {
   show_404();
  }
  
  $data['title']=$data['blog_item']['title'];

  $data['comments'] = $this->blog_model->get_comments($data['blog_item']['id']);
  
  $this->load->view('templates/header',$data);
  $this->load->view('templates/blog/blogA');
  $this->load->view('blog/view',$data);
  $this->load->view('templates/blog/blogB');
  $this->load->view('templates/footer',$data);
}

public function comment($id='')
{
  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');

  $this->form_validation->set_rules('comment','Comment','required');

  if($this->form_validation->run())
  {
   $this->blog_model->set_id($id);
   $this->blog_model->set_comment();
   $this->session->set_flashdata('flashError', 'Comment entry :)');
   redirect('/common_auth/blog/');
  }
  
  $data['title']='Comment';
  $this->load->vars($data);

  $this->load->view('templates/header');
  $this->load->view('templates/blog/blogA');
  $this->load->view('blog/comment');
  $this->load->view('templates/blog/blogB');
  $this->load->view('templates/footer');

}
}

?>
and the model =)
Code:
<?php
class Blog_model extends CI_Model{

function __construct()
{
  $this->load->database();
}

function get_blog($slug = FALSE)
{
  if($slug === FALSE)
  {
   $query = $this->db->get('blog');
   return $query->result_array();
  }

  $query = $this->db->get_where('blog',array('slug' => $slug));
  return $query->row_array();
}

function set_blog()
{
  $this->load->helper('url');

  $slug = url_title($this->input->post('title'),'dash',TRUE);

  $data = $this->upload->data();

  $data = array(
   'date'  => date("Y-m-d H:i:s"),
   'title' => $this->input->post('title'),
   'slug'  => $slug,
   'post'  => $this->input->post('post'),
   'category'  => $this->input->post('category'),
   'image'  => $filepath = 'uploads/blog/'.$data['file_name']
  );
  
  return $this->db->insert('blog',$data);
}
     protected $blog_id;
    
    public function set_id($blog_id)
    {
        $this->blog_id = $blog_id;
    }

public function get_comments($id)
{
  $query = $this->db->get_where('comment',array('entry_id' => $id));
  return $query->result_array();
}

public function set_comment()
{
  $this->load->helper('url');
  
  $user = $this->session->userdata('username');

  $data = array(
   'entry_id' => $this->blog_id,
   'name'  => $user,
   'comment'  => $this->input->post('comment'),
   'date'  => date("Y-m-d H:i:s")
  );
  
  return $this->db->insert('comment',$data);
}
}
#20

[eluser]TheFuzzy0ne[/eluser]
Where is $this->blog_id defined or set?

In my code, there was a set_id() method, which needs to be called to... well, set the ID. This keeps it available globally within the scope of the class, which means all of the methods within your class can access it, and you only need to set it once.

The only other alternative, as I mentioned before, is to pass the ID of the blog into each method. You need to decide which way you want to do it, and implement it. As it stands, you've only implemented half of the first method.




Theme © iAndrew 2016 - Forum software by © MyBB