溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Yii針對添加行如何實(shí)現(xiàn)增刪改查操作

發(fā)布時間:2021-08-30 14:52:24 來源:億速云 閱讀:127 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Yii針對添加行如何實(shí)現(xiàn)增刪改查操作,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體如下:

效果圖:

Yii針對添加行如何實(shí)現(xiàn)增刪改查操作

控制器:

<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\models\Zhan;
class IndexController extends Controller
{
  //顯示頁面
  public function actionIndex()
  {
    $index=new Zhan();
    //接受值
     if($_POST)
     {
      $a=Yii::$app->db;
      //判斷是否有刪除ids
      if(Yii::$app->request->post('ids'))
      {
        $ids=Yii::$app->request->post('ids');
        $str='';
        for($i=0;$i<count($ids);$i++)
        {
         if($a->createCommand()->delete('zhan',['id'=>$ids[$i]])->execute())
         {
           $str++;
         }
        }
        if($str!='')
        {
          echo '<script>alert("刪除成功");location.href="index.php?r=index/index"</script>';
        }
      }
      else
      {
         //判斷是否有id傳值
         $cid=Yii::$app->request->post('cid');
         $xu_ids=Yii::$app->request->post('xu_id');
         //print_r($id);die;
         //添加行的數(shù)據(jù)
         $names=Yii::$app->request->post('zhan_name');
         $ulrs=Yii::$app->request->post('url');
         //遍歷數(shù)組
         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();
            //數(shù)據(jù)可能沒被修改,只有成功一條就改變標(biāo)記的值
            if($res)
            {
             echo '<script>alert("修改成功");location.href="index.php?r=index/index"</script>';
            }
           }
           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();
            //數(shù)據(jù)可能沒被修改,只有成功一條就改變v標(biāo)記的值
            if($res)
            {
              echo '<script>alert("添加成功");location.href="index.php?r=index/index"</script>';
            }
           }
         }
      }
     }
     else
     {
      //查詢數(shù)據(jù)
      $models=Zhan::find()->orderBy(['xu_id'=>'asc'])->asArray()->all();
      //var_dump($models);
      return $this->renderPartial("show",['models'=>$models]);
     }
  }
}
?>

視圖層:

<center>
<form action="index.php?r=index/index" method="post">
<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
<table>
<tr>
<td>ID</td>
  <td>顯示順序</td>
  <td>站點(diǎn)名稱</td>
  <td>站點(diǎn)URL</td>
</tr>
<?php foreach ($models as $key => $v) {?>
<tr>
<input type="hidden" name="cid[]" value="<?php echo $v['id']; ?>" />
<td><input type="checkbox" name="ids[]" class='ids' value="<?= $v['id'] ?>"></td>
<td><input type="text" name="xu_id[]" value="<?= $v['xu_id'];?>"></td>
<td><input type="text" name ='zhan_name[]'value="<?= $v['zhan_name'];?>"></td>
<td><input type="text" name="url[]" value="<?= $v['url'];?>"></td>
</tr>
<?php }?>
<tr>
 <td><a href="javascript:void(0)" onclick="add(this);">+添加友情鏈接</a></td>
 <td><input type="checkbox" onclick="jian(this);">刪除?</td>
</tr>
 <tr>
  <td><input type="submit" value="提交" ></td>
 </tr>
</table>
</form>
</center>
<script src="style/jquery.js"></script>
<script>
//添加一行
function add(ts)
{
  var tr=$(ts).parent().parent();
  var newtr='<tr><td></td><td><input type="text" name="xu_id[]"></td><td><input type="text" name="zhan_name[]"></td><td><input type="text" name="url[]"></td><td><input type="button" value="刪除該行" onclick="del(this);"></td></td></tr><br />';
  tr.after(newtr);
}
//刪除當(dāng)前行
function del(ts)
{
  $(ts).parent().parent().remove();
}
//刪除所有
function jian(ts)
{
  var ids=$('.ids');
  //alert(ids.length);
  for(var i=0;i<ids.length;i++)
  {
    if(ts.checked==true)
    {
     ids[i].checked=true;
    }
    else
    {
     ids[i].checked=false;
    }
  }
}
</script>
</head>

以上是“Yii針對添加行如何實(shí)現(xiàn)增刪改查操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

yii
AI