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

[eluser]davehedgehog[/eluser]
hey, Ive created a blog to which you can view comments for each of the articles. Now Im attempting to have a create comment field where the logged in username, the article of the blog ID or slug and the comment. at the moment Ive configured for just the comment, how can I call the username from the current logged in user and the ID and or slug from the article thats viewed?

heres the code, the controller~
Code:
public function comment()
{
  $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');

  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');

  $data['title']='Comment';
    
  $this->form_validation->set_rules('comment','Comment','required');

  if($this->form_validation->run())
  {
   $this->blog_model->set_comment();
   $this->session->set_flashdata('flashError', 'Dodgy comment entry :/');
   redirect('/common_auth/blog/', $slug);
  }

}
and the model
Code:
public function set_comment()
{
  $this->load->helper('url');

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

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

Ive tried a number of different methods but cant seem to achieve it by creating a class wide or global var,, any suggestions?
#2

[eluser]TheFuzzy0ne[/eluser]
It probably depends on your login system. Once a user is logged in, I usually set 3 session variables. user_id, username and is_logged_in.

So:
Code:
$this->session->set_userdata(array(
    'user_id' => $user['id'],
    'username' => $user['username'],
    'is_logged_in' => TRUE,
));

Now the user carries these around throughout their session. When the user gets logged out, is_logged_in gets unset. Obviously, you need make sure that your base controller takes care of whether or not a user is/isn't logged in, and whether or not they should be logged out, if you don't have a library already doing that for you.
#3

[eluser]davehedgehog[/eluser]
Um well this is basically the set up Ive got ~ http://jondavidjohn.com/blog/2011/01/sca...r-ion_auth other than the public viewed controllers arent contained in a public folder. Using the Class wide var from there I cant call it to the controller or the model.
#4

[eluser]TheFuzzy0ne[/eluser]
For Ion Auth, these should work:

Code:
// Get the user's email address.
$email = $this->session->userdata('email');

// Get the user's username.
$username = $this->session->userdata('username');

// Get the user's ID.
$user_id = $this->session->userdata('user_id');

I encourage you to study the code. There's a lot to learn from it. Smile
#5

[eluser]davehedgehog[/eluser]
ah brilliant =) that's the username sorted, but any idea how I can pass the slug and id variables from another function?

this for example doesnt work even tho the url is the same :/(to get the $slug)

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

but realistically its the id of the blog entry I need, so if I can store the two in a session or public variable then brilliant, my attempts so far dont seem to work tho :/
#6

[eluser]TheFuzzy0ne[/eluser]
I don't see why you should need to store the blog ID or slug in the session. The slug should be generated and stored in the database when the entry is added.

Then, you could have your view() method, something like this:
Code:
function view($slug = '')
{
    if ( ! $blog_data = $this->blog_model->get_by_slug($slug))
    {
        show_error('Computer says no...');
    }
    
    /* ... */
}

You just need to make sure that you don't use a slug that's already in use.

So long as you have an index on the slug column in your database, and all slugs are unique, the slug is almost as good as having the ID.

I don't know if that helps at all? I find it quite difficult to keep up. Tongue
#7

[eluser]davehedgehog[/eluser]
Um yeah not really being clear, um this is how ive got it set up at the moment with just adding the comments and username...

the controller~

Code:
//this gets the blog item from the index view
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'];
//here the comments for that blog article are retrieved from the comments model
  $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()
{
  $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');

  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');
  
  $data['title']='Comment';

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

  if($this->form_validation->run())
  {
   $this->blog_model->set_comment();
   $this->session->set_flashdata('flashError', 'Comment entry :)');
   redirect('/common_auth/blog/');
  }

}


the model then~
Code:
//gets all the associated comments to the article id(hence why I also need the id)
public function get_comments($id)
{
  $query = $this->db->get_where('comment',array('entry_id' => $id));
  return $query->result_array();
}
//and finally set the comment(note this is all done in the same view)
public function set_comment()
{
  $this->load->helper('url');
//sets the user
  $user = $this->session->userdata('username');
//this should set the slug :/ but doesnt.
  $slug = url_title($this->input->post('title'),'dash',TRUE);
//the slug shows as a 0 as stands here, if I add to the array 'entry_id' => $id, it falls over :/
  $data = array(
   'name'  => $user,
   'comment'  => $this->input->post('comment'),
   'date'  => date("Y-m-d H:i:s"),
   'slug'  => $this->input->post('slug')
  );
  
  return $this->db->insert('comment',$data);
}

and the view~
Code:
<div id="content">
<br></br>
<table>
  <tr><td><h2>&lt;?php echo $blog_item['title']?&gt;</h2></tr>
  <tr><td><img src="&lt;?php echo site_url($blog_item['Image'])?&gt;" /></tr>
  <tr><td><h3>Category:&lt;?php echo $blog_item['category']?&gt;</h3>
  <tr><td>Date:&lt;?php echo $blog_item['date']?&gt;</tr>
  <tr><td>Post:&lt;?php echo $blog_item['post']?&gt;</tr>
</table>
<br></br>
<div id="title">
<h3>Comments:</h3>
</div>
<br></br>
<div id="comment">

&lt;?php if ($comments): ?&gt;&lt;?php foreach($comments as $com): ?&gt;

<table>
    <tr><td>Name:&lt;?php echo $com['name']; ?&gt;</tr>
    <tr><td>Post:&lt;?php echo $com['comment']; ?&gt;</tr>  
    <tr><td>Date:&lt;?php echo $com['date']; ?&gt;</tr>
</table>&lt;?php endforeach;?&gt;&lt;?php else: ?&gt;
<br></br>
<br></br>
<p>
    There are no comments for this blog.
</p>&lt;?php endif;?&gt;  

&lt;?php echo validation_errors();?&gt;

&lt;?php echo form_open_multipart('common_auth/blog/comment') ?&gt;

<label for="comment">Comment:</label>
&lt;textarea name="comment" &gt;&lt;/textarea><br/>

&lt;input type="submit"  name="submit" value=" Add Comment"/&gt;
<br></br>
&lt;/form&gt;
</div>
</div>

as it stands this works, so if I can hold the slug and the id. then I could use the id to display the comments and the slug to route back to that blog article view.
#8

[eluser]TheFuzzy0ne[/eluser]
You should be passing the slug or ID through the URL to the comment() method.

Code:
function comment($slug = '')
{
    // Check that the blog ID exists before allowing the comment to be posted.
    if ( ! $blog = $this->blog_model->get_blog($slug))
    {
        show_error('Unable to locate the requested blog.');
    }
    
    $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_comment();
        $this->session->set_flashdata('flashError', 'Comment entry :)');
        redirect('/common_auth/blog/');
    }

    $data['title'] = 'Comment';
    
    // Pass the data variable into the views.
    $this->load->vars($data);
    
    // Loading views should be the last thing to be done.
    $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');
}

The slug should only be generated once, and once only. That is when the blog entry is posted. From then on, you should be using the slug from the database.

Also, in order to keep your database normalised, you shouldn't be storing the username of the person that commented, but rather their ID. Then you can join the users table onto the comments table, and get their usernames that way. As well as saving you space, this has other advantages, such as if the user was able to change their username, everything would automatically be up-to-date.

You should be doing the same with the slug. It should only be stored in a single place in the database, this gives you the ability to change a slug if you wanted to, without having to update it everywhere else. You can then simply join any other tables using the blog ID as the foreign key, and if you wanted to, you could get all the blog data and comments complete with dates, usernames and anything else, in a single query.

In your case, I don't think there's anything wrong with breaking it up into a few separate queries, however.
#9

[eluser]davehedgehog[/eluser]
I see how you mean but I still cant get the ID of the blog entry :/

if I do this instead of the slug then it just shows the error.

Code:
public function comment($id='')
{
  if( ! $blog = $this->blog_model->get_blog($id))
  {
   show_error('unable to locate blog entry');
  }
#10

[eluser]TheFuzzy0ne[/eluser]
Please post your get_blog() model method.




Theme © iAndrew 2016 - Forum software by © MyBB