Codeigniter博客评论ID不引用帖子ID

时间:2015-05-28 13:38:08

标签: javascript php ajax codeigniter

我的数据库中有两个名为blog_articles和blog_comments的表。我在我的博客上添加了一个评论系统,该教程使用Javascript和Ajax将注释加载到页面中。问题在于将评论与各个博客帖子联系起来。加载到页面中的注释与博客帖子无关,并且对所有其他帖子都可见。

在创建评论后查看blog_comments表,cmt_article_id字段显示为0.我认为它应该是读取或引用blog_articles表中的article_id编号。

提前致谢。

数据库表Sql:

 CREATE TABLE IF NOT EXISTS `blog_comments` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`emailid` varchar(100) NOT NULL,
`comment` text NOT NULL,
`time` varchar(100) NOT NULL,
`cmt_article_id` int(11) NOT NULL,
PRIMARY KEY (`comment_id`),
KEY `cmt_article_id` (`cmt_article_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

CREATE TABLE IF NOT EXISTS `blog_articles` (
`article_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`article_body` text NOT NULL,
PRIMARY KEY (`article_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

控制器blog.php

function display_comments() {

    $data['query'] = $this->mdl_blog_comments->get_article();
    $data['comments'] = $this->mdl_blog_comments->get_comments();
    $this->load->view('blog/single_post', $data);
}
function insert_comments() {
    $insertinfo = $this->mdl_blog_comments->insertcomments_article();
    //$data['comments']=$this->mdl_blog_comments->get_latestcomment();
    $data['comments'] = $this->mdl_blog_comments->get_comments();
    echo $this->load->view('blog/commentdisplay', $data);
}
function displaycomments() {
    $data['comments'] = $this->mdl_blog_comments->get_comments();
    echo $this->load->view('blog/commentdisplay', $data);
}

模型mdl_blog_comments.php

 //insert comments
function insertcomments_article() {
    $this->load->helper('date');
    $name = $this->input->post('name');
    $email = $this->input->post('email');
    $article_id = $this->input->post('article_id');
    $comment = $this->input->post('comment');
    $datestring = "%Y-%m-%d - %h:%i %a";
    $time = time();
    $date = mdate($datestring, $time);
    $insertcomment = $this->db->insert('blog_comments', array(
               'name' => $name, 
               'emailid' => $email, 
               'comment' => $comment, 
               'time' => $date, 
               'cmt_article_id' => $article_id));
               return $insertcomment;
 }
//retrive comments
function get_comments() {
    $article_id = $this->input->post('article_id');
    $this->db->select('*');
    $this->db->from('blog_comments');
    $this->db->where('cmt_article_id', $article_id);
    return $this->db->get();
}
function get_latestcomment() {
    $article_id = $this->input->post('article_id');
    $this->db->select('*');
    $this->db->from('blog_comments');
    $this->db->where('cmt_article_id', $article_id);
    $this->db->order_by('comment_id', 'DESC');
    $this->db->limit('1');
    return $this->db->get();
}

查看single_post.php

 <form>
    <!-- html comment form -->
 <form class="form-horizontal" method="post" id="form">

 <div class="form-group">
    <label for="name" class="col-sm-2 control-label">Name</label>
    <div class="col-sm-10">

        <input type="text" class="form-control" id="name" name="name"  placeholder="First & Last Name" value="">
    </div>
</div>
<div class="form-group">
    <label for="email" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
        <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
    </div>
</div>
<div class="form-group">
    <label for="message" class="col-sm-2 control-label">Comment</label>
    <div class="col-sm-10">
        <textarea class="form-control" rows="4" id="comment"  name="comment"> </textarea>
    </div>
</div>
 <div class="form-group">
    <div class="col-sm-10 col-sm-offset-2">
        <input id="submit" name="submit" type="submit" value="Submit Comment" class="btn btn-inverse btn-lg">
    </div>
</div>

<script>
   $(document).ready(function () {
     var article_id = $("#article").val();
      $.post('<?php echo base_url();?>blog/displaycomments/', 
         {
         article_id:article_id
       },
            function(data){
                     $("#display_comment").html(data);});
       });

$(function() {
$("#submit").click(function() {

    var name = $("#name").val();
    var email = $("#email").val();
    var comment = $("#comment").val();
    var article_id = $("#article").val();

var dataString ='name='+name+'&email='+email+'&comment='+comment+'   &article_id='+article_id;
if(name=='' || email=='' || comment=='') {

alert('Please Give Valid Details');

} 
else {

$("#display_comment").fadeIn(100).html('<img src="<?php echo    base_url();?>uploads/ajax-loader.gif" />Loading Comment...');
$.ajax({
    type: "POST",
    url: "<?php echo base_url();?>blog/insert_comments/",
    data: dataString,
    cache: false,

success: function(data){

$("#display_comment").html(data);
$("#display_comment").fadeIn(slow);
}
});
}return false;

}); });

</script>

0 个答案:

没有答案