• 面向对象select方法


    <?php

        class Table{
             protected $tablename;
             protected $arrTable;
            protected $w='';//条件属性
            protected $field="*";//查询字段
            protected $order="";
             function __construct($arrTable=''){
                   $this->tablename=get_class($this);//获取获取当前的对象名
                    $this->arrTable=$arrTable;
             }
             function insert(){
                    echo $this->insertTable();
                 }
             private function insertTable(){
                 $keys="(";
                 $values="(";
                 foreach ($this->arrTable as $key => $value) {
                    $keys.=$key.",";
                    $values.="'".$value."',";
                 }
                 $keys=substr($keys, 0,-1).")";
                 $values=substr($values,0,-1).")";
                 return  "insert {$this->tablename} {$keys} values {$values}<br>";
              }

             
             
              function del(){
                if ($this->w!=''){//表示没有条件
                   $delstr="delete from {$this->tablename} where {$this->w}";               
                }else{
                   $delstr="delete from {$this->tablename}";
                }
                echo $delstr."<br>";

             }
             function where($w=''){
                $this->w=$w;
             }
              function update(){
                $updateData="";
                foreach ($this->arrTable as $key => $value) {
                    $updateData.=$key."=".$value.",";
                }
                $updateData=substr($updateData,0,-1);
                if ($this->w=="") {
                   echo "update {$this->tablename} set {$updateData} <br>";
                }else{
                   echo "update {$this->tablename} set {$updateData} where {$this->w}<br>";
                }
                
             }
              function select(){
                if ($this->w==""){
                   $select="select {$this->field} from {$this->tablename} {$this->order}";  
                }else{
                   $select="select {$this->field} from {$this->tablename} where {$this->w} {$this->order}";  
                }
                echo $select."<br>";


                 
             }
             //查询字段
             function field($field="*"){
                $this->field=$field;
             }
             //排序
             function order($order=''){
                $this->order=$order;
             }
       }
       /**
       *
       */
       class User extends Table
       {
             
           
       }
       /**
       *
       */
       class Article extends Table
       {
           
           
       }
       /**
       *
       */
       class Student extends Table
       {
          
         
       }
       $Datas=array("username"=>"admin","userpass"=>'admin');
       $user1=new User($Datas);
       $user1->insert();
       $Datas=array("title"=>"admin","author"=>'admin');
       $a=new Article($Datas);
       $a->insert();
       $a->where("title='清远'");
       $a->del();
       $a->update();
       $Datas = array('id' =>'001' ,'name'=>'zhangsan' );
       $s1=new Student($Datas);
       $s1->insert();
       $s1->field("title,author");
       $s1->select();
       

       // $iswhere=""

     ?>

  • 相关阅读:
    轻松记账工程冲刺第一天
    课堂练习-找水王
    NABCD模型—轻松记账
    四则运算网页版
    二维数组最大子数组(结对开发)
    软件工程结对作业01
    返回一个二维整数数组中最大联通子数组的和
    学习进度条(第六周)
    整数数组中最大子数组的和
    学习进度条(第五周)
  • 原文地址:https://www.cnblogs.com/zteng/p/6541688.html
Copyright © 2020-2023  润新知