Yii Operation Example _php instance for adding and deleting add rows

Source: Internet
Author: User
Tags smarty template yii
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.