[eluser]rebornishard[/eluser]
database structure
Code:
[b]db_users[/b]
id_user
user_name
[b]db_posts[/b]
id_post
user_id
post_title
post_content
[b]db_comments[/b]
id_comment
post_id
user_id <-- 0 if comment come from guest / non member
comment_date
comment_author
comment_content
comment_parent <-- default 0 = not nested
model post
Code:
class Post extends CI_Model
{
function get_post($ID = FALSE)
{
$query = $this->db->get_where('db_posts', array('id_post' => $ID));
return $query->row_array();
}
function get_comment($ID = FALSE)
{
$query2 = $this->db->get_where('db_comments', array('post_id' => $ID));
return $query2->row_array();
}
public function get_author($ID = FALSE)
{
$query3 = $this->db->query("SELECT db_users.id_user, db_users.user_name, db_comments.id_comment, db_comments.post_id, db_comments.user_id, db_comments.comment_date, db_comments.comment_author, db_comments.comment_content, db_comments.comment_parent, db_posts.id_post, db_posts.user_id, db_posts.post_title, db_posts.post_title, db_posts.post_content
FROM (db_users LEFT JOIN db_posts ON db_users.id_user = db_posts.user_id)
LEFT JOIN db_comments
ON db_comments.post_id=db_posts.id_post
WHERE db_comments.post_id = '$ID'
ORDER BY db_comments.comment_date DESC");
return $query3->result_array();
}
}
controller posts
Code:
class Posts extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('post');
}
function view($ID)
{
$data['post'] = $this->post->get_post($ID);
$data['comment'] = $this->post->get_comment($ID);
$data['author'] = $this->post->get_author($ID);
if (empty($data['post']))
{
show_404();
}
$this->load->view('view', $data);
}
}
view
Code:
<?php
echo '<h2>'.$post['post_title'].'</h2>';
echo $post['post_content'].'<br />';
if (empty($comment['post_id']))
{
}
else
{
if ($comment['user_id']==0)
{
if ($comment['comment_parent']==0)
{
foreach ($author as $row)
{
echo '<b>'.$row['comment_author'].'</b>';
echo '<br />'.$row['comment_content'].'<br />';
}
}
else //nested comment should be here
{
foreach ($author as $row)
{
echo '<b>'.$row['comment_author'].'</b>';
echo '<br />'.$row['comment_content'].'<br />';
}
}
}
else
{
if ($comment['comment_parent']==0)
{
foreach ($author as $row)
{
echo '<b>'.$row['user_name'].'</b>';
echo '<br />'.$row['comment_content'].'<br />';
}
}
else //nested comment should be here
{
foreach ($author as $row)
{
echo '<b>'.$row['user_name'].'</b>';
echo '<br />'.$row['comment_content'].'<br />';
}
}
}
}
i aim for
comment 1 <--a
-reply for a <--b
--reply for b <---c
---reply for c
could anyone help me ?
thank you