本文实例讲述了Yii针对添加行的增删改查操作。分享给大家供大家参考,具体如下:
效果图:
控制器:
PHP;">
db;
//判断是否有删除ids
if(Yii::$app->request->post('ids'))
{
$ids=Yii::$app->request->post('ids');
$str='';
for($i=0;$icreateCommand()->delete('zhan',['id'=>$ids[$i]])->execute())
{
$str++;
}
}
if($str!='')
{
echo '';
}
}
else
{
//判断是否有id传值
$cid=Yii::$app->request->post('cid');
$xu_ids=Yii::$app->request->post('xu_id');
//print_r($id);die;
//添加行的数据
$names=Yii::$app->request->post('zhan_name');
$ulrs=Yii::$app->request->post('url');
//遍历数组
foreach($names as $k=>$v)
{
if(!empty($cid[$k]))
{
$c_id=$cid[$k];
//echo $c_id;die;
$url=$ulrs[$k];
$xu_id=$xu_ids[$k];
$name=$v;
$res=$a->createCommand()->update("zhan",['zhan_name'=>$name,'url'=>$url,'xu_id'=>$xu_id],"id=$c_id")->execute();
//数据可能没被修改,只有成功一条就改变标记的值
if($res)
{
echo '';
}
}
else
{
$url=$ulrs[$k];
$xu_id=$xu_ids[$k];
$name=$v;
$res=$a->createCommand()->insert("zhan",['xu_id'=>$xu_id,'zhan_name'=>$name,'url'=>$url])->execute();
//数据可能没被修改,只有成功一条就改变v标记的值
if($res)
{
echo '';
}
}
}
}
}
else
{
//查询数据
$models=Zhan::find()->orderBy(['xu_id'=>'asc'])->asArray()->all();
//var_dump($models);
return $this->renderPartial("show",['models'=>$models]);
}
}
}
?>
视图层:
PHP;">