欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

thinkphp信息修改和分页

程序员文章站 2022-03-12 20:49:21
关联两个数据表,在Model里建立StuModel.class.php: 在view中建立Stu文件夹,建立主页面index.html: 在Stu中建立修改页面edit.html: 在Controller中建立StuController.class.php: ......

关联两个数据表,在Model里建立StuModel.class.php:

<?php
//Belongs_to 关联表示当前模型从属于另外一个父对象
namespace Admin\Model;
use Think\Model\RelationModel;
class StuModel extends RelationModel
{
     protected $_link = array(
       'Clas' => array(
           'mapping_type'  => self::BELONGS_TO,
           'class_name'    => 'Clas',
           'foreign_key'   => 'clas_id',
//           'mapping_name'  => 'dept',
       ),
     );
}

  

 

在view中建立Stu文件夹,建立主页面index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>名字</td>
        <td>性别</td>
        <td>生日</td>
        <td>班级</td>
        <td>修改</td>
    </tr>
    <volist name="Stu" id="vo">
        <tr>
            <td>
                {$vo.uname}
            </td>
            <td>
                <if condition="$vo.sex eq 1">
                    男
                    <else/>
                    女
                </if>
            </td>
            <td>
                {$vo.birthday}
            </td>
            <td>
                {$vo.Clas.cname}
            </td>
            <td><a href="{:U('Stu/editShow',array('id'=>$vo[id]))}">编辑</a></td>
        </tr>
    </volist>
</table>
<hr>
<table>
    <tr>
        <td>名字</td>
        <td>性别</td>
        <td>生日</td>
        <td>班级</td>
        <td>修改</td>
    </tr>
<tbody id="data">

</tbody>
</table>
<input type="hidden" id="page" value="1">
<input type="submit" id="prev" value="上一页">
<input type="submit" id="next" value="下一页">
<script src="__JS__/jquery-2.2.3.min.js"></script>
<script>
    function dataGet(pag_num,count) {
//ajax
    $.ajax({
        url:'{:U("Stu/ajaxSelect")}',
        dataType:'json',
        type:'post',
        data:{
          pag_num:pag_num,
          count:count
        },
        success:function (data) {

            var str='';
            for (var i in data){

                if (data[i].sex == 1){
                    data[i].sex = '男';
                }else{
                    data[i].sex = '女';
                }

                str +=
                    '<tr>' +
                    '    <td>' +
                    data[i].uname +
                    '    </td>' +
                    '    <td>' +
                      data[i].sex +
                    '    </td>' +
                    '    <td>' +
                    data[i].birthday.substr(0,10) +
                    '    </td>' +
                    '    <td>' +
                    data[i].Clas.cname +
                    '    </td>' +
                    '    <td><a href="{:U("Stu/editShow")}?id=' + data[i].id + '">编辑</a>' +
                    '</td>' +
                    '</tr>'

            }

            $('#data').html(str);
            $('#page').val(pag_num);
        }
    })
    }
//数据显示数量 dataGet(1,1);
//点击切换 $('#prev').click(function () { var page = parseInt($('#page').val()) dataGet(page-1,1); }); $('#next').click(function () { var page = parseInt($('#page').val()) dataGet(page+1,1); }) </script> </body> </html>

  在Stu中建立修改页面edit.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>名字</td>
        <td>性别</td>
        <td>生日</td>
        <td>班级</td>
        <td>修改</td>
    </tr>
    <form action="{:U('Stu/editPost')}" method="post">
        <tr>
            <td><input type="text" name="uname" value="{$stu.uname}"></td>

            <td>
                <if condition="$stu['sex'] eq 1">
                    <input type="radio" name="sex" value="1" id="man" checked><labl for="man">男</labl>
                    <input type="radio" name="sex" value="0" id="woman"><labl for="woman">女</labl>
                    <else/>
                    <input type="radio" name="sex" value="1" id="man"><labl for="man">男</labl>
                    <input type="radio" name="sex" value="0" id="woman" checked><labl for="woman">女</labl>
                </if>

            </td>
            <td><input type="text" name="birthday" value="{$stu.birthday}"></td>
            <td>
              <select name="clas_id">
                  <option value="">选择班级</option>
                  <volist name="clas" id="vo">
                      <if condition="$stu['clas_id'] eq $vo['id']">
                        <option value="{$vo.id}" selected>{$vo.cname}</option>
                          <else/>
                          <option value="{$vo.id}">{$vo.cname}</option>
                      </if>
                  </volist>
              </select>
            </td>
            <td>
                <input type="hidden" name="id" value="{$stu.id}">
                <input type="submit" value="提交">
            </td>
        </tr>
    </form>
</table>
</body>
</html>

  在Controller中建立StuController.class.php:

<?php

namespace Admin\Controller;


use Think\Controller;

class StuController extends Controller
{
    public function index(){
        $db_stu = D('Stu');
        $data_stu = $db_stu->relation(true)->select();
//        dump($data_stu);
        $this->assign('Stu',$data_stu);
        $this->display();
    }

    public function ajaxSelect(){
        $pag = I('post.pag_num');
        $count = I('post.count');
        $db_stu = D('stu');
        $data_stu = $db_stu->relation(true)->page($pag,$count)->select();
        $this->ajaxReturn($data_stu);

    }
    public function editShow(){
         if (IS_GET){
             $id = I('get.id');
//             dump($id);
             $db_stu = M('Stu');
             $db_clas = M('Clas');
             $data_stu = $db_stu->where('id = '.$id)->find();
             $data_clas = $db_clas->where('status = 1')->select();
             $this->assign('clas',$data_clas);
             $this->assign('stu',$data_stu);
             $this->display('Stu/edit');
         }
    }
    public function editPost(){
        if (IS_POST){
            $data = I('post.');
//            dump($data);
            $db_stu = M('stu');
            $db_stu->save($data);
//            dump($m);
            $this->success('成功','index');
        }
    }
}