[php] view plain copy
//關閉csrf 原生代碼
public $enableCsrfValidation = false;
添加 接值
$connection = \Yii::$app->db->createCommand()->insert('denglu',$arr)->execute();
查詢
$command = \Yii::$app->db->createCommand('SELECT * FROM denglu');
$posts = $command->queryAll();
//刪除 接id
$command = \Yii::$app->db->createCommand()->delete('denglu', 'id = '.$id)->execute();
修改 先查詢 接值 id
$command = \Yii::$app->db->createCommand("SELECT * FROM denglu where id='$id'");
$post = $command->queryOne();
修改 先接值 修改要的值
$command = \Yii::$app->db->createCommand()->update('denglu', ['name' => $name,'password'=>$password], 'id = '.$id)->execute();
yii2.0
use Yii;
use yii\web\Controller;
use frontend\models\Denglu;
use yii\data\Pagination; // csrf
<input type="hidden" name="<?= \Yii::$app->request->csrfParam; ?>" value="<?= \Yii::$app->request->getCsrfToken();?>">
//添加
if($re['_csrf-frontend']) {
unset($re['_csrf-frontend']);
//print_r($re);die;
$er1=new Denglu();
// print_r($er1);die;
$er1->name=$re['name'];
$er1->password=$re['password'];
$er1->save();
}
查詢 分頁
$re=Denglu::find();
$pagination = new Pagination([
'defaultPageSize' => 2,
'totalCount' => $re->count(),
]);
$list = $re->offset($pagination->offset)
->limit($pagination->limit)
->all();
// print_r($re);die;
return $this->render('list',[
'list' => $list,
'pagination' => $pagination,
]);
}
不帶分頁查詢
$results=goods::find()->asArray()->all();
// return $this->render("list",["list"=>$results]);
刪除
$a=goods::find()->where(["id"=>$id])->one();
$arr=$a->delete();
修改查詢
1 $acc=goods::find()->where(["id"=>$id])->asArray()->one();
2 $goods = \Yii::$app->db->createCommand()->update('goods', ["goods_name"=>$arr['goods_name'],"money"=>$arr['money']],"id=".$arr['id'])->execute();
視圖層
<?php
use yii\widgets\LinkPager;
?>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
ajax 批刪 視圖層
<td><input type="checkbox" name="dom" id="ss" value="<?php echo $v['id']?>"/><?php echo $v['id']?></td>
<input type="checkbox" value="批量刪除" onclick="check_del();">批量刪chu
function check_del(){
var dom=document.getElementsByName('dom');
var str='';
for(var i=0;i<dom.length;i++){
if(dom[i].checked==true){
str=str+','+dom[i].value;
}
}
str=str.substr(1);
// alert(str)
location.href="?r=lianxi/dele&id="+str;
}
控制器 接受批刪的id值
$id=\yii::$app->request->get('id');
$sql="delete from shang where id in ($id)";
$a= \Yii::$app->db->createCommand($sql)->execute();
if($a){
return $this->redirect("?r=lianxi/list");
}else{
echo "刪出失敗";
}
[html] view plain copy
上傳圖片 加載類
use yii\web\UploadedFile;
$arr=\yii::$app->request->post();
$file = UploadedFile::getInstanceByName('filename');
$path = 'upload'; //設置上傳文件的路徑
$paths = $path.'/'.rand(1,9999).$file; //設置上傳文件的路徑名稱(這裏的數據進行入庫)
$arr['filename']=$paths;
$file->saveAs($paths,true); //保存文件
$goods = \Yii::$app->db->createCommand()->insert('goods', $arr)->execute();
if($goods){
return $this->redirect("?r=lianxi/list");
}else{
echo "添加失敗";
}
簡單的反選全選全不選
<td><input type="checkbox" class="check" name="dom" value="<?php $v['id']?>"/><?php echo $v['id']?></td>
<input type="checkbox" class="quan" value="<?php echo $v['id']?>"/>全選
<input type="checkbox" class="fan" value="<?php echo $v['id']?>"/>全不選
<input type="checkbox" class="bu" value="<?php echo $v['id']?>"/>反選
引進jquery文件 //全選(按鈕)
$(function(){
$('.quan').click(function(){
var ids = $('input:checkbox');
$.each(ids,function(i,item){
ids[i].checked=true;
})
})
})
//全不選
$(function(){
$('.fan').click(function(){
var ids=$('input:checkbox');
$.each(ids,function(i,item){
ids[i].checked=false;
})
})
})
//反選
$(function(){
$('.bu').click(function(){
var ids=$('.check');
$.each(ids,function(i,item){
ids[i].checked=!ids[i].checked;
})
})
})