1. 程式人生 > >面向對象---封裝增刪改查+數據分頁

面向對象---封裝增刪改查+數據分頁

循環 author ceil 首頁 false conn arr var_dump ref

<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>

面向對象---封裝增刪改查+數據分頁