学习PHP中yii框架的增删改查

我所在的工作室由原有的项目,作业就是在里面新增一个板块进行练习PHP最基本的增删改查。

1.新建一个数据库

由于我在学习以前的框架时就已经建了一个数据库,对这个库也比较熟悉,所以还是打算用这个库了。
数据库名为db_song,我选择的表为t_songs(一个歌单),其中包含id(主键),name(歌名),belonging(专辑/是我瞎起的名字ヾ(◍°∇°◍)ノ゙)和singer(歌手)四个属性。
数据库和表
表属性

2.连接数据库

打开目录里的main.php
main.php
修改本地数据库代码,将数据库名、用户名、密码设为自己数据库的。
数据库设置

3.定义AR类(创建model)

如果要访问表,首先需要通过集成 CActiveRecord 定义一个 AR 类。每个 AR 类代表一个单独的表,一个 AR 实例则代表那个表中的一行。
在目录里新建PHP文件,继承CActiveRecord。
models
在这个类中主要写两个方法。
第一个方法复制粘贴一下就OK( • ̀ω•́ )✧
第二个方法需要将tableName()中return两个大括号中间后面改为自己数据库中表的名字减去“t_”的部分,因为在前面main.php中我们规定过’tablePrefix’ => ‘t_’

<?php
/**
 * Created by PhpStorm.
 * User: dage
 * Date: 2018/8/1
 * Time: 16:25
 */
class Song extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return '{{songs}}';   //return 't_songs'
    }

}

4.控制器(Controller)

在目录的controller文件夹下新建名为song的文件夹
controller
在song中新建一个名为SongController的controller,并继承Controller
SongController

<?php
/**
 * Created by PhpStorm.
 * User: dage
 * Date: 2018/8/1
 * Time: 16:45
 */

class SongController extends Controller
{

}

5.编写增删改查方法

在目录下新建名为song的文件夹,并将html文件都放在这个文件夹下。
html
home.html

1. 列出所有歌曲
也就是“查所有”。
在controller中写查看全部歌曲的方法,其中的方法名要加action,这样可以自动识别,使用的时候可以不加action。

    /**
     * 列出全部歌曲
     */
    public function actionListAllSong(){
        $list = Song::model()->findAll(); //将t_news表中全部信息存到 $list中
        $this->smarty->assign('list',$list);
        $this->smarty->display('song/home.html');//跳到对应的html页面,内容展示
    }

写完之后在home.html中写好,并使用foreach

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>歌单</title>
</head>
<body>
<table border="1" style="margin-top: 10px;font-size: x-large">
    <thead> <tr>
        <th>编号</th>
        <th>曲名</th>
        <th>收录专辑</th>
        <th>歌手</th>
        <th>操作</th></tr>
    <tr>
    </thead>
    <tbody>
    <{foreach from=$list item=Song}>
    <td><{$Song.id}></td>
    <td><{$Song.name}></td>
    <td><{$Song.belonging}></td>
    <td><{$Song.singer}></td>
    <td>
    <a href="<{$website}>/song/song/DeleteSong?id=<{$Song.id}>">删除</a>
    <a href="<{$website}>/song/song/SearchSongById?id=<{$Song.id}>">删除</a>  
    <a href="<{$website}>/song/song/ToUpdateSong?id=<{$Song.id}>">修改</a>
    </td>
    </tr>
    <{/foreach}>
    <tr> <td colspan="2" style="text-align: center" ><a href="<{$website}>/song/song/ToAddSong">添加</a> </td>
        <td colspan="3" style="text-align: center" >
            <input type="text" name="search">
            <input type="submit" name="search_do" value="查询" formaction="<{$website}>/song/song/SearchSong">
        </td></tr> </tbody>
</table>

</body>
</html>

页面是这样滴罒ω罒虽然不是很好看❥(ゝω・✿ฺ)
home
2.增加歌曲
点击添加便可以跳到增加歌曲的页面
SongController中

/**
     * 跳转到增加歌曲页面
     */
    public function actionToAddSong(){
        $this->smarty->display('song/addSong.html');
    }

addSong.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>增加歌曲页面</title>
</head>
<body>
<form action="/dsjyw0725/song/song/AddSong" method="post">
    <table style="margin-top: 10px;font-size: x-large" >
        <tr>
            <td>歌曲名称:</td>
            <td><input type="text" name="name" style="width: 500px;height: 30px"></td>
        </tr>
        <tr>
            <td>歌曲所属专辑:</td>
            <td><textarea name="belonging" cols="30" rows="10" style="width: 500px;"></textarea></td>
        </tr>
        <tr>
            <td>歌手:</td>
            <td><input type="text" name="singer" style="width: 500px;height: 30px"></td>
        </tr>
        <tr content="center">
            <td  colspan="2" style="text-align: center"><input type="submit" value="提交"></td>
        </tr>
    </table>
</form>

</body>
</html>

页面是这样的
addSong
之后点击提交会使用SongController的这个方法

/**
     * 增加歌曲
     */
    public function actionAddSong(){
        $song=new Song;
        $name=$_POST['name'];
        $belonging=$_POST['belonging'];
        $singer=$_POST['singer'];
        $song->name=$name;
        $song->belonging=$belonging;
        $song->singer=$singer;
        $song->save();
        $this->redirect(array(ListAllSong));
    }

于是点击提交之后跳转回去,就发现已经显示出来啦
result
3.删除歌曲
这个比增加歌曲要简单一些
SongController代码

/**
     * 删除歌曲
     */
    public function  actionDeleteSong()
    {
        $id=$_GET['id'];
        $song=Song::model()->findByPk($id);
        $song->delete();                    // 从数据表中删除此行
        $this->redirect(array(ListAllSong));
    }

点击删除之后跳回到主页面,就不见啦
delete
4.修改歌曲信息
首先跳到修改歌曲的界面
SongController层:

/**
     * 跳转到修改歌曲信息界面
     */
    public function actionToUpdateSong(){
        $id=$_GET['id'];
        $song=Song::model()->find('id=:id',array(':id'=>$id));
        $this->smarty->assign('song',$song);
        $this->smarty->display('song/updateSong.html');
    }

跳过去的updateSong.html页面代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改歌曲信息页面</title>
</head>
<body>
<form action="/dsjyw0725/song/song/UpdateSong" method="post">
    <table border="1" style="margin-top: 100px;margin-left: 100px">
        <tr >
            <input type="text" name="id" value="<{$song.id}>" hidden/>
        </tr>
        <tr>
            <td>歌名</td>
            <td>
                <textarea  type="text" name="name" style="width: 1000px"><{$song.name}></textarea>
            </td>
        </tr>
        <tr>
            <td>歌曲所属专辑</td>
            <td>
                <textarea  type="text" name="belonging" style="width: 1000px"><{$song.belonging}></textarea>
            </td>
        </tr>
        <tr>
            <td>歌手</td>
            <td>
                <textarea  type="text" name="singer" style="width: 1000px"><{$song.singer}></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" name="submit" value="提交"></td>
        </tr>
    </table>
</form>

</body>
</html>

点击之后的页面是这样滴(ノ´▽`)ノ♪
update
修改之后点击提交,跳转到结果
SongController中

/**
     * 修改歌曲信息
     */
    public function actionUpdateSong(){
        $id=$_POST['id'];
        $song=Song::model()->find('id=:id',array(':id'=>$id));
        $song->name=$_POST['name'];
        $song->belonging=$_POST['belonging'];
        $song->singer=$_POST['singer'];
        $song->save(); // 将更改保存到数据库
        $this->redirect(array(ListAllSong));
    }

最终结果
resulllllt

5.通过id查找歌曲
点击查看详情就会用id查出这首歌的所属专辑
SongController中

/**
     * 通过id查找歌曲
     */
    public function  actionSearchSongById(){
        $id=$_GET['id'];
        $song=Song::model()->find('id=:id',array(':id'=>$id));
        $this->smarty->assign('song',$song);
        $this->smarty->display('song/information.html');
    }

information.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>歌曲信息</title>
</head>
<body>
    <table border="1" style="width: 1000px;margin-top: 10px;font-size:x-large">
        <tr>
            <td>编号</td>
            <td><{$song.id}></td>
        </tr>
        <tr>
            <td>歌名</td>
            <td><{$song.name}></td>
        </tr>
        <tr>
            <td>所属专辑</td>
            <td><{$song.belonging}></td>
        </tr>
        <tr>
            <td>歌手</td>
            <td><{$song.singer}></td>
        </tr>
        <tr>
            <a href="/dsjyw0725/song/song/ListAllSong">返回</a>
        </tr>
    </table>
</body>
</html>

页面依旧是这样滴罒ω罒虽然不是很好看❥(ゝω・✿ฺ)
searchbyid
6.模糊查询
在框框中搜索要进行模糊查询的内容,点击查询,会跳转输出结果
首先是搜索框,我加了一个好看的提示(๑>ڡ<)☆
mohusearch
代码如下

<form name="form" action="/dsjyw0725/song/song/SearchSong" method="post" >
            <input type="text" name="search" placeholder="请输入要进行模糊查询的内容">
            <input type="submit" name="search_do" value="查询"></form>

点击查询后SongController中

 /**
     * 模糊查询
     */
    function actionSearchSong(){
        $s = $_POST['search'];
        echo $s;
        $sql = "SELECT * 
                FROM t_songs
                WHERE t_songs.id LIKE '%".$s."%' 
                OR t_songs.name LIKE '%".$s."%'
                OR t_songs.belonging LIKE '%".$s."%'
                OR t_songs.singer LIKE '%".$s."%'
                ";
        $songList = Yii::app()->db->createCommand($sql)->queryAll();
        var_dump($songList);
        $this->smarty->assign('songList',$songList);
        $this->smarty->display('song/searchResult.html');
    }

跳转的页面代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模糊查询结果</title>
</head>
<body>
<table border="1" style="margin-top: 10px;font-size: x-large">
    <thead> <tr>
        <th>编号</th>
        <th>曲名</th>
        <th>歌手</th>
        <th>操作</th></tr>
    <tr>
    </thead>
    <tbody>
    <{foreach from=$songList item=Song}>
    <td><{$Song.id}></td>
    <td><{$Song.name}></td>
    <td><{$Song.singer}></td>
    <td>
        <a href="/dsjyw0725/song/song/DeleteSong?id=<{$Song.id}>">删除</a>
        <a href="/dsjyw0725/song/song/SearchSongById?id=<{$Song.id}>">查看详情</a>
        <a href="/dsjyw0725/song/song/ToUpdateSong?id=<{$Song.id}>">修改</a>
    </td>
    </tr>
    <{/foreach}>
    <tr> <td colspan="2" style="text-align: center" ><a href="/dsjyw0725/song/song/ToAddSong">添加</a> </td>
        <td colspan="3" style="text-align: center" >
            <a href="/dsjyw0725/song/song/ListAllSong">返回</a>
        </td></tr> </tbody>
</table>

</body>
</html>

在搜索框中输入“1”结果如下
mohuresult
我的模糊查询就做!好!啦!
好!写完了!开溜!

猜你喜欢

转载自blog.csdn.net/qq_41715397/article/details/81350389
今日推荐