Welcome Guest, Not a member yet? Register   Sign In
Comments on blog
#11

[eluser]TheFuzzy0ne[/eluser]
[quote author="davehedgehog" date="1365690149"]Im using MySql no Ajax.[/quote]

Hmm. Are comments viewed on a different page from the blog itself, or are you using a frame or something?

Code:
function get_comments()
{
    // Here you're not passing an ID to get_comments().
    // You're also using an extra variable unnecessarily.
    $query = $this->blog_model->get_comments();
    $data['comment'] = $query;

    // This isn't being passed into the view, and has now been called twice.
    // Where is $blog_item coming from?
    $comments = $this->blog_model->get_comments($blog_item->id);

    if(!$comments)
    {
        // This should be handled by your view.
        echo 'No comments';
    }
    else
    {
        $this->load->view('blog/comments',$data);
    }
}

Here are my proposed changes:

get_comments() controller method:
Code:
function get_comments($id = 0)
{
    $data['comments'] = $this->blog_model->get_comments($id);
    $this->load->view('blog/comments', $data);
}

View:
Code:
<?php if ($comments): ?><?php foreach($comments as $com): ?>

<table>
    <tr><td>Date:&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;

<p>
    There are no comments for this blog.
</p>&lt;?php endif;?&gt;

Your presentation logic should be within your view. Now you view decides what to do (if anything), when there are no comments.
#12

[eluser]davehedgehog[/eluser]
I wanted the comments to show below the View.php after the post. so that when you click to view article the user is redirected to the view.php with the article by $slug and then below that the comments for that article.

I have now altered it as you've said changing the controller and adding the comment to the view.php after the article and im getting and undefined variable on line 9 for $comments
#13

[eluser]davehedgehog[/eluser]
OH and in the Sql, ther are two tables one for the blog and another "comment" for the comments that holds the id of the blog article you see.
#14

[eluser]davehedgehog[/eluser]
Ok think Ive almost got it now, besides a small problem with the model~
Code:
$query2 = $this->db->query("select * from 'comment' where 'entry_id' = $id ORDER BY 'date' DESC");

  if($query2->num_rows() != 0){
  return $query2->row_array();
  }
  else
  {
   return false;
  }

This gives me the error~
Fatal error: Call to a member function num_rows() on a non-object

if I change the query to say~
Code:
$query = $this->db->get_where('comment',array('entry_id' => $id));

then it kinda works, as in it echos out the right comment for each article, but it gives me the first character for each coloumn entry, for all three fields~
name:2
Post:2
Date:2
name:9
Post:9
Date:9
name:j
Post:j
Date:j
name:b
Post:b
Date:b
name:2
Post:2
Date:2
name:a
Post:a
Date:a
#15

[eluser]davehedgehog[/eluser]
Ah got it, instead of row_array() I needed results_array()

for anyone who may be interested heres the full code~
controller~
Code:
&lt;?php
class Blog extends CI_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);
}
}
?&gt;
the model~
Code:
&lt;?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();
}
}

and view~
Code:
<div id="content">
<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>

&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;

<p>
    There are no comments for this blog.
</p>&lt;?php endif;?&gt;  
</div>

Cheers for you help again TheFuzzy0ne =)




Theme © iAndrew 2016 - Forum software by © MyBB