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

[eluser]davehedgehog[/eluser]
its in the model just before get_comments.
#22

[eluser]TheFuzzy0ne[/eluser]
Sorry, I didn't spot that. Properties are normally defined at the top of a class, just before the constructor.

Are you setting the ID from your controller? Please post your controller code.
#23

[eluser]davehedgehog[/eluser]
Yeah sorry just dumped it back in by there for now in the model. 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');

}
}

?>
#24

[eluser]TheFuzzy0ne[/eluser]
The only possible reason I can see for that not to be working, is that the ID is not being passed to the model correctly.

This is why you need to check everything that's sent to the server.

Is $id set? If it's not, tell the user. Otherwise, set the ID of the blog in the model

Does the specified blog even exist? If it doesn't, tell the user.

Is the ID numeric? If it is, redirect to the correct location, using the slug we got when we tried to confirm the blog ID existed.

Now you can get on with performing validation and what-not. With what you've written, it's quite easy to get your server to issue a warning or an error just by not passing a parameter into the method. Code defensively, and keep those potential errors to an absolute minimum.

[url="http://ellislab.com/forums/viewthread/234745/#1052457"]I've already written some code[/url] to try and help you, and I've commented it too. Although it's untested, I'd say it's pretty much the bare minimum you should do in order to prevent unintentional errors. Please study it, and try to understand what it's doing, and more importantly why it's doing it. It should be very simple to trace a path through the code so you can see what it's doing.

If you have any questions, I'd be happy to help you out as much as possible. Now, let's make some progress here, because it feels like we're going around in circles! Smile
#25

[eluser]davehedgehog[/eluser]
Ok so Ive reconfigured it and now getting this warning~

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: blog/index.php

Line Number: 4

#26

[eluser]davehedgehog[/eluser]
heres what I got...

the model~
Code:
<?php
class Blog_model extends CI_Model{

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

function get_blog($id = FALSE)
{
  if( ! $id)
  {
   return False;
  }

  $this->db->where((is_numeric($id)) ? 'id' : 'slug', $id);
  
        $query = $this->db->get('blog');
        
        if ( ! $query->num_rows())
        {
            return FALSE;
        }
        
  return $query->row();
     }

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 = 0)
{
  $query = $this->db->get_where('comment',array('entry_id' => $id));
  return $query->result_array();
}

public function set_comment($id = 0)
{
        if ( ! $id)
        {
         return FALSE;
        }
        
   $this->load->helper('url');
  
   $user = $this->session->userdata('username');

   $data = array(
    'entry_id' => $id,
    'name'  => $user,
    'comment'  => $this->input->post('comment'),
    'date'  => date("Y-m-d H:i:s")
   );
  
  return $this->db->insert('comment',$data);
}
}
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($slug='')
{
  $entry = $this->blog_model->get_blog($slug);

  if ( ! $entry)
      {
          show_error('unable to locate blog entry');
      }
    
  
     if (is_numeric($slug))
      {
          redirect('/blog/comment/' . $entry['slug']);
      }
  
  $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($entry['id']);
   $this->session->set_flashdata('flashError', 'Comment entry :)');
   redirect('/blog/view/' . $entry['slug']);
  }
  
  $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 view~
Code:
<div id="content">
    <div class="container">
      <div class="hero-unit">
&lt;?php foreach ($blog as $blog_item):?&gt;

<table class="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; " alt="($blog_item['Image'])"/></tr>
  <tr><td><h4>Category:&lt;?php echo $blog_item['category']?&gt;</h4></tr>
  <tr><td>Date:&lt;?php echo $blog_item['date']?&gt;</tr>
  <tr><td>Post:&lt;?php echo $blog_item['post']?&gt;</tr>
  <tr><td><a href="/blog/&lt;?php echo $blog_item['slug']?&gt;">View Article</a></tr>
</table>

&lt;?php endforeach ?&gt;
</div>
</div>
</div>
#27

[eluser]davehedgehog[/eluser]
Yipi I got it =)

You can hide the ID in the view ~
Code:
&lt;input type="hidden" name="id" id="id" value ="&lt;?php echo $blog_item['id']?&gt;" /&gt;

#28

[eluser]TheFuzzy0ne[/eluser]
I have no idea which is line 4, or why you have a directory called blog, and a file/controller named index.php. Having a controller named Blog would make much more sense.

Again, you shouldn't need to hide the ID in the view, because it should be present in the URL. You can process the URL to confirm that the ID/slug is valid before having to access any POST data. Your URLs should all follow a similar structure.

/blog/comment/<blog_id>
/blog/edit/<blog_id>
/blog/view/<blog_id>
/blog/delete/<blog_id>
/blog/create

Obviously, creating a blog doesn't require an ID, because we won't know the ID until the blog has been created.

Your code is looking much better now, so well done. The only thing you're missing is comments. Wink
#29

[eluser]davehedgehog[/eluser]
You misunderstand I have a controller named Blog and a view called index.php.

The addresses are like that only they end with the slug not the ID.

And yes your very right I need to comment alot more so shall attempt to do so now.

Again a huge thanks for your help geeze, you sir ROCK!




Theme © iAndrew 2016 - Forum software by © MyBB