The example of this paper describes the addition and deletion of yii to add rows and modify operations. Share to everyone for your reference, as follows:
:
Controller:
<?phpnamespace backend\controllers;use yii;use yii\web\controller;use backend\models\zhan;class IndexController Extends controller{//Display page Public function Actionindex () {$index =new Zhan (); Accept value if ($_post) {$a =yii:: $app->db; Determine if there is a deletion of the IDs if (Yii:: $app->request->post (' IDs ')) {$ids =yii:: $app->request->post (' IDs '); $str = "; for ($i =0; $i
CreateCommand ()->delete (' Zhan ', [' id ' = $ids [$i]])->execute ()) {$str + +; }} if ($str! = ") {echo '; }} else {//To determine if there is an ID value $cid =yii:: $app->request->post (' CID '); $xu _ids=yii:: $app->request->post (' xu_id '); Print_r ($id);d ie; Add row Data $names =yii:: $app->request->post (' zhan_name '); $ulrs =yii:: $app->request->post (' url '); Traversal array 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 (); The data may not be modified, only a successful one will change the value of the tag 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 (); The data may not be modified, only a successful one will change the value of the V mark if ($res) {echo '; }}}}} else {//query data $models =zhan::find ()->orderby ([' xu_id ' = ' ASC ']) ->asarray ()->all (); Var_dump ($models); return $this->renderpartial ("Show", [' models ' = $models]); }}}?>
View Layer:
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.