如何创建用于停用和激活模块的动态方法

时间:2018-01-18 17:03:18

标签: php codeigniter

我正在开发一个系统。在我的系统中有一些名为Event和Announcement的模块,这两个模块都有去激活和激活下拉按钮。为了减少方法的冗余,我想为此创建一个动态方法。

但身份证的难度来自于此?是来自活动还是公告?我知道你要使用一条路线而且我已经完成了那部分

问题:如果是事件/公告,如何确定ID?

注意:我的架构

Event table
id
event_title
event_description
event_date

Announcement table
id
announcement_title
announcement_description
announcement_date

在我的控制器中,我使用参数将其传递给我的模型以使其动态化。我的参数的顺序:表,数据,条件

控制器

public function deactivate()
    {
        $id = $this->uri->segment(3);


        $where = array('id' => $id);
        $update_status = array(
                'status'    => 'Inactive'
            );
        //$this->Crud_model->update('event',$update_status,$where);
        //redirect('administrator/event/');
    }
    public function activate()
    {
        $id = $this->uri->segment(3);

        $where = array('id' => $id);
        $update_status = array(
                'status'    => 'Active'
            );
        //$this->Crud_model->update('co',$update_status,$where);
        //redirect('administrator/branch/'. $session_location);
    }

查看

<td><div class="dropdown">
                  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Action
                  </button>
                  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                     <?php if($row->status == 'Active'){?>
                    <a class="dropdown-item" href="<?= base_url().'administrator/deactivate/'. $row->id?>">Deactivate</a>
                     <?php } else { ?>
                    <a class="dropdown-item" href="<?= base_url().'administrator/activate/'. $row->id?>">Activate</a>
                    <?php }?>

                     <a class="dropdown-item" href="<?= base_url().'administrator/delete/'. $row->id?>">Delete</a>
                  </div>
              </div></td>

路线

$route['admnistrator/deactivate/'] = 'administrator/deactivate/$1';
$route['admnistrator/activate/'] = 'administrator/activate/$1';

模型

public function update($table,$data,$where=""){
        if($where!="") {
                $this->db->where($where);
        }

        $result = $this->db->update($table,$data);
        if ($result) {
            return TRUE;
        }else{
            return FALSE;
        }
    }

2 个答案:

答案 0 :(得分:1)

尝试使用控件内的参数传递您的表名和ID,如下所示

public function deactivate($table = '',$id = '')
{
    $where = array('id' => $id);
    $update_status = array(
        'status'    => 'Inactive'
    );
    //$this->Crud_model->update($table,$update_status,$where);
    //redirect('administrator/event/');
}
public function activate($table = '',$id = '')
{
    $where = array('id' => $id);
    $update_status = array(
        'status'    => 'Active'
    );
    //$this->Crud_model->update($table,$update_status,$where);
    //redirect('administrator/branch/'. $session_location);
}

在html下拉列表中更改锚标记()的href:

<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <?php if($row->status == 'Active'){?>
    <a class="dropdown-item" href="<?= base_url().'administrator/deactivate/{event/announcement}'. $row->id?>">Deactivate</a>
    <?php } else { ?>
    <a class="dropdown-item" href="<?= base_url().'administrator/activate/{event/announcement}'. $row->id?>">Activate</a>
    <?php }?>
    <a class="dropdown-item" href="<?= base_url().'administrator/delete/'. $row->id?>">Delete</a>
</div>

如果需要,还可以将路线更改为:

$route['admnistrator/deactivate/(:any)/(:any)'] = 'administrator/deactivate/$1/$2';
$route['admnistrator/activate/(:any)/(:any)'] = 'administrator/activate/$1/$2';

答案 1 :(得分:0)

如果可能,请更改您的计划:

One table
id
kind
title
description
date

例如kind = 1可以是事件,kind = 2可以是公告,并在查询中使用where kind=1子句返回您想要的值(id&#39; s)