Answer the question
In order to leave comments, you need to log in
How can an admin delete any comments on a specific post on codeigniter?
Help, it is necessary for the admin to be able to delete any comments on a certain post,
Here's what happened, but it doesn't work...
And how can I edit comments in the future?
Model for getting comments with pagination:
public function view($slug = NULL, $offset=0) {
$this->data['posts_item'] = $this->posts_model->getPosts($slug); //обращение к нашей модели
$post_slug = $this->posts_model->getPosts($slug);
if (empty($this->data['posts_item'])) {
//show_404();
$this->load->helper('url_helper');
redirect('/', 'location');
}
//вызовем модель для комментов
$this->load->model('comments_model');
$this->data['comments'] = $this->comments_model->getComments($post_slug['id']);
$this->data['id'] = $post_slug['id'];
$this->data['slug'] = $this->data['posts_item']['slug'];
$this->data['title'] = $this->data['posts_item']['title'];
$this->data['content'] = $this->data['posts_item']['text'];
$this->data['author_post'] = $this->data['posts_item']['author'];
$this->data['date'] = $this->data['posts_item']['date'];
// Настройки пагинации
$data_cmts = $this->db
->where('post_id', $post_slug['id'])
->get("comments");
$config['total_rows'] = $data_cmts->num_rows();
$config['base_url'] = base_url(). 'index.php/posts/view/'.$slug;
$config['per_page'] = 5;
$config['first_link'] = 'Первая';
$config['last_link'] = 'Последняя';
$config['first_url'] = '0';
//pagination bootstrap
$config['full_tag_open'] = "<ul class='pagination pull-right'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
//init pagination
$this->pagination->initialize($config);
$this->data['pagin_liks'] = $this->pagination->create_links();
$this->data['offset'] = $offset;
$this->data['arr_comments'] = $this->comments_model->getCommentsOnPage($post_slug['id'], $config['per_page'], $offset);
$this->load->view('templates/header', $this->data);
$this->load->view('posts/view', $this->data);
$this->load->view('templates/footer');
}
public function delComment($post_id=NULL) {
if(!$this->dx_auth->is_admin()) {
show_404();
}
$this->data['comments_delete'] = $this->comments_model->getCommentsOnPage($post_id, $per_page, $offset);
if (empty($this->data['comments_delete'])) {
show_404();
}
$this->data['result'] = "Ошибка удаления ";
if($this->comments_model->delComments($post_id)) {
$this->data['result'] = " Успешно удалён";
}
$this->load->view('templates/header', $this->data);
$this->load->view('comment/delete', $this->data);
$this->load->view('templates/footer');
}
public function delCommentsOnPage($post_id, $per_page, $offset) {
return $this->db->delete('comments', array('post_id' => $post_id));
}
<?php foreach ($arr_comments as $cmts): ?>
<div class="panel panel-info">
<div class="panel-heading"><i class="glyphicon glyphicon-user"></i> <?php echo getUserNameById($cmts->user_id)->username; ?></div>
<div class="panel-body">
<?php echo $cmts->comment_text ?>
<a href="/posts/view/<?php echo $slug; ?>/delComment/<?php echo $post-id ?>">Удалить</a>
</div>
</div>
<?php endforeach ?>
<?php echo $pagin_liks; ?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question