I would like to display announcements in the different user's dashboards. What I am trying to do is to display it in the dashboard in which the user can close the announcement in the dashboard permanently, but what happens is that when a certain user close the announcement tab in the dashboard, all of the announcement in the same user type is being deleted. So can you please help me to figure out what approach should I use? Thanks!
<div class="clearfix"></div>
<?php
if($announcements = $this->Bid_Model->fetchItems('announcements')):
foreach ($announcements as $announce):
if($announce->user_type == 6){?>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Announcement</h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"></a>
</li>
<li><a href="<?=base_url()?>Admin/deleteAnnouncement/<?=$announce->announcement_id?>" class="close-link"><i class="fa fa-close"></i></a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<img src="<?php echo base_url().'uploads/';?>announcements/<?=$announce->image;?>" height="500" width="700">
<h4><?=$announce->title?></h4>
<h4><?=$announce->content?></h4>
<div class="ln_solid"></div>
<!-- modals -->
</div>
</div>
</div>
</div>
<?php } endforeach; endif; ?>
<!-- /modals -->
<br />
public function announceSubmit(){
$this->session->userdata('user_data');
$id = $this->session->userdata('user_id');
$title = $this->input->post('title', true);
$users = $this->input->post('users', true);
$content = $this->input->post('descr', true);
$config['upload_path'] = './uploads/announcements/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '25000';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('image-source')){
print_r($this->upload->display_errors());
}else{
$announce_image = $this->upload->data('file_name');
$data = array(
'title' => $title,
'user_type' => $users,
'content' => $content,
'image' => $announce_image,
'user_id' => $id
);
$this->Bid_Model->insertItem('announcements', $data);
redirect('dashboard');
}
}
public function deleteAnnouncement(){
$this->Bid_Model->updateItem('announcements',['user_type' => 0],['announcement_id'=>$this->uri->segment(3)]);
redirect('dashboard');
}
function insertItem($table, $data = NULL){
$this->db->insert($table, $data);
}
function insertItems($table,$data = array()){
$this->db->insert_batch($table, $data);
}
function updateItem($table, $data, $var = NULL){
if($var != NULL){
$this->db->where($var);
}
$this->db->update($table, $data);
}
I assume that you are storing your user related data in session so
Controller :
public function deleteAnnouncement() {
$this->Bid_Model->updateItem('announcements', ['user_type' => 0], ['announcement_id' => $this->uri->segment(3), 'user_id' => $this->session->userdata('user_id')]);
redirect('dashboard');
}
First, You are passing 2nd and 3rd parameters to updateItem()
in the model as an array.
So it is better to do like this $var = array()
rather than $var = NULL
Second, you are deleting announcements for a particular user so use user id to delete for only one user
$id = $this->session->userdata('user_id')
function updateItem($table, $data, $var = array()){
$id = $this->session->userdata('user_id');
if(count($var) > 0){
$this->db->where($var);
}
$this->db->where('user_id', $id);
$this->db->update($table, $data);
}