使用codeigniter按用户类型在用户的仪表板中显示公告

时间:2019-02-27 02:52:43

标签: php html css database codeigniter

我想在其他用户的仪表板中显示公告。我想要做的是将其显示在仪表板上,用户可以在其中永久关闭公告中的通知,但是发生的是,当某个用户关闭仪表板上的公告选项卡时,所有公告都在同一位置用户类型正在被删除。所以,请您帮我弄清楚我应该使用哪种方法?谢谢!

查看

 <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);
    }

2 个答案:

答案 0 :(得分:0)

我假设您正在会话中存储与用户相关的数据,所以
控制器:

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');
    }

答案 1 :(得分:0)

首先,您要将第二个和第三个参数作为数组传递给模型中的updateItem()

因此,最好像这样$var = array()而不是$var = NULL

第二,您要删除特定用户的公告,因此使用用户ID只能删除一个用户

$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);
}