面向对象---封装增删改查+数据分页

Posted hopelooking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象---封装增删改查+数据分页相关的知识,希望对你有一定的参考价值。

<meta charset="UTF-8">
<?php
class F{
    public $locahost;
    public $name;
    public $pwd;
    public $database;

    public function __construct($locahost,$name,$pwd,$database)
    {
        $this->locahost = $locahost;
        $this->name = $name;
        $this->pwd = $pwd;
        $this->database = $database;
    }
    public function mysqliConnect(){
        $link = mysqli_connect($this->locahost,$this->name,$this->pwd,$this->database);
        mysqli_query($link,‘set names utf8‘);
        return $link;
    }
    public function Insert($data){
        foreach ($data as $k => $v)
        {
            @$value = $value."‘$v‘,";
            @$key = $key."$k,";
        }
        $lastValue = substr($value,0,-1);
        $lastKey = substr($key,0,-1);
        $link = $this->MysqliConnect();
        $sql = "insert into depart ($lastKey) VALUES ($lastValue)";
        if(mysqli_query($link,$sql)){
            $res = 1;
        }else{
            $res = 0;
        }
        return $res;
    }

    //定义一个查询的方法(id的值,id的字段名,表名)
    public function SelectData($table,$id=null,$id_name=null){
        //判断一下id有没有值
        if($id!==null){
            //查询的是单条的数据
            $link = $this->MysqliConnect();
            //拼接sql
            $sql = "SELECT * FROM $table WHERE $id_name=‘$id‘";
            //执行
            $res = mysqli_query($link,$sql);
            //生成数组
            while ($a = mysqli_fetch_assoc($res)){
                $data[] = $a;
            }
        }else{
            //查询的全部的数据
            //连接数据库,获取$link
            $link = $this->MysqliConnect();
            //拼接sql
            $sql = "SELECT * FROM $table";
            //执行
            $res = mysqli_query($link,$sql);
            //生成数组
            while ($a = mysqli_fetch_assoc($res)){
                $data[] = $a;
            }
        }

        return $data;
    }

    //封装一个删除的方法
    public function DeleteData($table,$id,$id_name){
        //链接数据库
        $link = $this->MysqliConnect();
        //拼接sql
        $sql = "delete from $table WHERE $id_name=$id";
        //执行
        if(mysqli_query($link,$sql)){
            $res = 1;
        }else{
            $res = 2;
        }

        return $res;
    }


    //封装一个修改的方法
    public function UpdateData($table_name,$data){
        //拿一下link
        $link = $this->MysqliConnect();
        //循环取出来我们想要的值然后拼接
        foreach ($data as $k=>$v){
            //$k就是所有的键(数据库里面的字段)
            // $v就是所有的值(数据库里面的字段值)
            //var_dump(strpos($k,‘id‘));
            if(strpos($k,‘id‘) !== false){
                $last_id_name = $k; //获取到了id的字段名
                $last_id_value = $v;  //获取的是id的字段值
            }else{
                //拼接
                @ $str .= "$k=‘$v‘,";
            }
        }

        //截取一下最后的拼接
        $last_str = substr($str,0,-1);

        //拼接where条件
        $where = "$last_id_name=$last_id_value";
        //拼接剩下的sql
        $sql = "update $table_name set $last_str WHERE $where";
        if(mysqli_query($link,$sql)){
            return 1;
        }else{
            return 2;
        }
    }



    //定义一个分页的方法
    public function GetPage($table,$length){
        //求出总条数
        $link = $this->MysqliConnect();
        $sql = "select * from $table";
        $res = mysqli_query($link,$sql);
        $count = mysqli_num_rows($res);
        //设置每页的条数
        //$length = 3;
        //总页数
        $last_count = ceil($count/$length);
        //接收当前页
        $current_page = empty($_GET[‘page‘])?1:$_GET[‘page‘];
        //偏移量
        $limit = ($current_page-1)*$length;
        //拼接sql
        $sql = "select * from $table limit $limit,$length";
        //执行sql
        $res1 = mysqli_query($link,$sql);
        //转化成数组
        while ($arr = mysqli_fetch_assoc($res1)){
            $data[] = $arr;
        }

        //判断首页、尾页、上一页、下一页
        $home_page = 1; //首页
        $last_page = $last_count;//尾页
        //上一页
        if($current_page<=1){
            $pre_page = 1;
        }else{
            $pre_page = $current_page-1;
        }

        //下一页
        if($current_page>=$last_count){
            $next_page = $last_count;
        }else{
            $next_page = $current_page+1;
        }


        //返回
        $data2[‘data‘] = $data;  //表单数据
        $data2[‘current_page‘] = $current_page;  //当前页
        $data2[‘home_page‘] = $home_page; //首页
        $data2[‘last_page‘] = $last_page;  //尾页
        $data2[‘pre_page‘] = $pre_page;  //上一页
        $data2[‘next_page‘] = $next_page;  //下一页

        return $data2;
    }
}
//$obj = new F(‘127.0.0.1‘,‘root‘,‘root‘,‘demo‘);
//$res = $obj->GetPage(‘book‘,3);
//print_r($res);

 

 

分页的调用:

<?php
//引入类文件
include "3.php";
//实例化
$obj = new F(‘127.0.0.1‘,‘root‘,‘root‘,‘demo‘);
$data = $obj->GetPage(‘book‘,5);
?>
<table>
    <tr>
        <th>ID</th>
        <th>名字</th>
        <th>价格</th>
        <th>作者</th>
        <th>照片</th>
    </tr>
    <?php foreach ($data[‘data‘] as $k =>$v){?>
        <tr>
            <td><?php echo $v[‘book_id‘];?></td>
            <td><?php echo $v[‘book_name‘];?></td>
            <td><?php echo $v[‘book_price‘];?></td>
            <td><?php echo $v[‘book_author‘];?></td>
            <td><img src="<?php echo $v[‘book_photo‘];?>" width="100px;" height="100px;"></td>
        </tr>
    <?php }?>

    <a href="fenye.php?page=<?php echo $data[‘home_page‘]?>">首页</a>
    <a href="fenye.php?page=<?php echo $data[‘pre_page‘]?>">上一页</a>
    <a href="fenye.php?page=<?php echo $data[‘next_page‘]?>">下一页</a>
    <a href="fenye.php?page=<?php echo $data[‘last_page‘]?>">尾页</a>
</table>

 

以上是关于面向对象---封装增删改查+数据分页的主要内容,如果未能解决你的问题,请参考以下文章

(SQLite3封装) 一行代码实现增删改查 - 值得收藏转发分享

JS高级. 04 增删改查面向对象版歌曲管理递归

面向对象设计

多对多表的增删改查与drf的分页

Android数据库设计——2,面向对象(ORM)操作表:增删改查

Android数据库设计——2,面向对象(ORM)操作表:增删改查