php下拉选项的批量操作的实现代码
程序员文章站
2023-11-16 15:27:22
实现功能,操作项可以单独删除,批量项可以批量删除,批量显示,隐藏(改广告的数据库的status_is的值);
1.视图关键点:
复制代码 代码如下:
<td ><input type="checkbox" name="id[]" value="<?php echo $row->id?>">
<?php echo $row->id?></td>
<div class="fixsel">
<input type="checkbox" name="chkall" id="chkall" onclick="checkall(this.form, 'id')" />
<label for="chkall">全选</label>
<select name="command">
<option>选择操作</option>
<option value="addelete">删除</option>
<option value="adverify">显示</option>
<option value="adunverify">隐藏</option>
</select>
<input id="submit_maskall" class="button confirmsubmit" type="submit" value="提交" name="maskall" />
</div>
/*单个删除*/
<a href="<?php echo $this->createurl('batch',array('command'=>'addelete', 'id'=>$row->id))?>" class="confirmsubmit">
/**
* 批量操作
*
*/
public function actionbatch ()
{
if (xutils::method() == 'get') { //单个删除是get方式
$command = trim($_get['command']);
$ids = intval($_get['id']);
} else
if (xutils::method() == 'post') {
$command = trim($_post['command']);
$ids = $_post['id'];
is_array($ids) && $ids = implode(',', $ids);
} else {
xutils::message('errorback', '只支持post,get数据');
}
empty($ids) && xutils::message('error', '未选择记录');
switch ($command) {
case 'addelete':
parent::_acl('ad_delete');
parent::_adminilogger(array('catalog'=>'delete', 'intro'=>'删除广告,id:'.$ids));
parent::_delete(new ad(), $ids, array ('ad' ), array ('attach_file' ));
break;
case 'adverify':
parent::_acl('ad_verify');
parent::_adminilogger(array('catalog'=>'delete', 'intro'=>'广告状态变更为显示,id:'.$ids));
parent::_verify(new ad(), 'verify', $ids, array ('ad' ));
break;
case 'adunverify':
parent::_acl('ad_verify');
parent::_adminilogger(array('catalog'=>'delete', 'intro'=>'广告状态变更为隐藏,id:'.$ids));
parent::_verify(new ad(), 'unverify', $ids, array ('ad' ));
break;
default:
throw new chttpexception(404, '错误的操作类型:' . $command);
break;
}
}
protected function _delete ($model = null, $id = '', $redirect = 'index', $attach = null, $pkfield = 'id')
{
if ($attach) { //如果有附件要删除广告的图片
$data = $model->findall($pkfield . ' in(:id)', array (':id' => $id ));
foreach ((array) $data as $row) {
foreach ((array) $attach as $value) {
if (! empty($row[$value])) {
@unlink($row[$value]);
}
}
}
}
$result = $model->deleteall(array ('condition' => 'id in(' . $id . ')' ));
//刷新缓存
self::_refreshcache($model);
$this->redirect($redirect);
}
protected function _verify ($model = null, $type = 'verify', $id = '', $redirect = 'index', $cdfield = 'status_is', $pkfield = 'id')
{
$criteria = new cdbcriteria();
$criteria->condition = $pkfield . ' in(' . $id . ')';
$showstatus = $type == 'verify' ? 'y' : 'n';
$result = $model->updateall(array ($cdfield => $showstatus ), $criteria);
//刷新缓存
self::_refreshcache($model);
$this->redirect($redirect);
}
实现功能,操作项可以单独删除,批量项可以批量删除,批量显示,隐藏(改广告的数据库的status_is的值);
1.视图关键点:
复制代码 代码如下:
<td ><input type="checkbox" name="id[]" value="<?php echo $row->id?>">
<?php echo $row->id?></td>
复制代码 代码如下:
<div class="fixsel">
<input type="checkbox" name="chkall" id="chkall" onclick="checkall(this.form, 'id')" />
<label for="chkall">全选</label>
<select name="command">
<option>选择操作</option>
<option value="addelete">删除</option>
<option value="adverify">显示</option>
<option value="adunverify">隐藏</option>
</select>
<input id="submit_maskall" class="button confirmsubmit" type="submit" value="提交" name="maskall" />
</div>
复制代码 代码如下:
/*单个删除*/
<a href="<?php echo $this->createurl('batch',array('command'=>'addelete', 'id'=>$row->id))?>" class="confirmsubmit">
2.控制器:
复制代码 代码如下:
/**
* 批量操作
*
*/
public function actionbatch ()
{
if (xutils::method() == 'get') { //单个删除是get方式
$command = trim($_get['command']);
$ids = intval($_get['id']);
} else
if (xutils::method() == 'post') {
$command = trim($_post['command']);
$ids = $_post['id'];
is_array($ids) && $ids = implode(',', $ids);
} else {
xutils::message('errorback', '只支持post,get数据');
}
empty($ids) && xutils::message('error', '未选择记录');
switch ($command) {
case 'addelete':
parent::_acl('ad_delete');
parent::_adminilogger(array('catalog'=>'delete', 'intro'=>'删除广告,id:'.$ids));
parent::_delete(new ad(), $ids, array ('ad' ), array ('attach_file' ));
break;
case 'adverify':
parent::_acl('ad_verify');
parent::_adminilogger(array('catalog'=>'delete', 'intro'=>'广告状态变更为显示,id:'.$ids));
parent::_verify(new ad(), 'verify', $ids, array ('ad' ));
break;
case 'adunverify':
parent::_acl('ad_verify');
parent::_adminilogger(array('catalog'=>'delete', 'intro'=>'广告状态变更为隐藏,id:'.$ids));
parent::_verify(new ad(), 'unverify', $ids, array ('ad' ));
break;
default:
throw new chttpexception(404, '错误的操作类型:' . $command);
break;
}
}
父类的删除函数:
复制代码 代码如下:
protected function _delete ($model = null, $id = '', $redirect = 'index', $attach = null, $pkfield = 'id')
{
if ($attach) { //如果有附件要删除广告的图片
$data = $model->findall($pkfield . ' in(:id)', array (':id' => $id ));
foreach ((array) $data as $row) {
foreach ((array) $attach as $value) {
if (! empty($row[$value])) {
@unlink($row[$value]);
}
}
}
}
$result = $model->deleteall(array ('condition' => 'id in(' . $id . ')' ));
//刷新缓存
self::_refreshcache($model);
$this->redirect($redirect);
}
复制代码 代码如下:
protected function _verify ($model = null, $type = 'verify', $id = '', $redirect = 'index', $cdfield = 'status_is', $pkfield = 'id')
{
$criteria = new cdbcriteria();
$criteria->condition = $pkfield . ' in(' . $id . ')';
$showstatus = $type == 'verify' ? 'y' : 'n';
$result = $model->updateall(array ($cdfield => $showstatus ), $criteria);
//刷新缓存
self::_refreshcache($model);
$this->redirect($redirect);
}