我有3个控制器方法名称为博客, load_messages 和 load_comments 。现在我想在用户点击按钮时提取评论。 post id传递给方法名称load_comments但现在如果我加载名为 blog 的视图,那么它会出错,因为我已经将一些数据传递给了视图。我只想传递那些是注释控制器的数据。
功能是:
public function blog() {
$user_session_email = $this -> session -> userdata('user_email','name','id');
if(!$user_session_email) {
redirect('/');
}
$user_data = $this -> users_model -> MyAccount([
'id' => $this -> session -> userdata('id'),
'name' => $this -> session -> userdata('name'),
'email' => $this -> session -> userdata('user_email'),
]);
$data["persoanl"] = $user_data;
$returned_result = $this -> users_model -> load_messages();
$messages["message"] = $returned_result['message'];
$this -> load -> view('users/inc/header', $data);
$this -> load -> view('users/inc/blog_form', $data);
$this -> load -> view('users/blog', $messages);
$this -> load -> view('users/inc/footer');
}
public function load_messages() {
$returned_result = $this -> users_model -> load_messages();
$messages["message"] = $returned_result["message"];
$messages['id'] = $this -> load_comments();
$this -> load -> view('users/blog', $messages);
}
public function load_comments() {
$messages['id'] = $this -> input -> post('post_id');
$this -> load -> view('users/blog', $messages);
}
在最后一个方法中,我只想传递$ message变量,但是当我传递这个值时,其他函数中的博客视图加载了不同的参数,如$ messages [' message'],它生成一个错误,说在博客视图页面上$ message是未定义的,但如果我删除了最后一个方法,它将完美地工作。如何将数据传递给博客应该怎么做?
博客查看代码
<?php
foreach($message as $blog) :
?>
<div class="row blog_post<?php echo $blog -> blog_id; ?>" id="blog_posts">
<div class="col-lg-1 col-md-2 col-sm-2 col-xs-3 user_blog_image">
<?php if($blog -> profile_image == null) { ?>
<img src="<?php echo base_url(); ?>public/dist/img/user2-160x160.jpg" alt="User Image">
<?php } else { ?>
<img src="<?php echo base_url().$blog -> profile_image; ?>" alt="User Image">
<?php } ?>
</div> <!-- End of user blog image -->
<div class="col-lg-11 col-md-10 col-sm-10 col-xs-9 post_message">
<?php if($blog -> id == $this -> session -> userdata('id')) : ?>
<a class="pull-right" onclick="delete_post('<?php echo $blog -> blog_id; ?>', '<?php echo base_url('users/delete_post'); ?>')">
×
</a>
<?php endif; ?>
<p>
<?php echo $blog -> message; ?>
</p>
<span class="pull-left">
<small> Posted By: <?php echo $blog -> name; ?></small>
</span>
<a onclick="get_comments(this.id, '<?php echo base_url('users/load_comments'); ?>')" class="stop_interval" id="<?php echo $blog -> blog_id; ?>">
<i class='fa fa-comment' aria-hidden='true'></i> View Comments
</a>
<a onclick="update_like('<?php echo $blog -> blog_id; ?>', '<?php echo base_url('users/update_like'); ?>')">
<i class='fa fa-thumbs-up' aria-hidden='true'></i> Likes (<small><?php echo $blog -> likes; ?></small>)
</a>
</div> <!-- End of post message -->
<div class="col-lg-offset-1 col-lg-11 comment_field">
<div class="show_comments col-lg-12 col-md-12 col-sm-12 col-xs-12" id="show_comments<?php echo $blog -> blog_id; ?>">
<?php echo $id; ?>
<div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 commentor_pic">
<img src="<?php echo base_url(); ?>public/dist/img/user2-160x160.jpg" alt="User Image">
</div> <!-- End of commentor picture -->
<div class="col-lg-11 col-md-11 col-sm-11 col-xs-11 comment">
<p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
<p> <small> Posted By: Rashid Khokhar </small> </p>
</div> <!-- End of commentor picture -->
</div> <!-- End of show comments -->
<form method="post" id="comment_Form<?php echo $blog -> blog_id; ?>">
<input type="text" id="blog_comment<?php echo $blog -> blog_id; ?>" required name="comment_field" class="form-control" placeholder="Type your comment">
<button type="submit" id="cmntBtn<?php echo $blog -> blog_id; ?>" class="btn btn-primary"><i class="fa fa-share" aria-hidden="true"></i></button>
</form>
</div> <!-- End of comment field -->
</div>
<script type="text/javascript" src="<?php echo base_url('public/dist/js/jQuery.js'); ?>"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>public/dist/js/custom.js"></script>
<script type="text/javascript">
comment_blog('<?php echo $blog -> blog_id; ?>', '<?php echo base_url('users/comment_blog'); ?>');
jQuery( "#show_comments<?php echo $blog -> blog_id; ?>" ).each(function( value ) {
jQuery(this).hide();
});
</script>
<?php endforeach; ?>
</div>
</div>
答案 0 :(得分:0)
在load_comments方法中,您需要使用model从数据库中获取数据。