MainController.class.php:
functionShowAll () {//model: Each table in the database corresponds to a single//class name is a table name, the member variable inside the class is a column name//a table corresponds to a class, one of the data corresponds to an object//if we have no special action on the model of the table, we can not Set up the model//query info table//1. Instantiate Models /*$info = new \home\model\infomodel; Var_dump ($info);*/ //2.D Method /*$info = D ("info"); Var_dump ($info);*/ //3.M method, parent class object, pass parameter $car= M ("Car"); //var_dump ($info); Var_dump ($car->select ());//Returns a two-dimensional array of all data//$attr = $car->where ("brand= ' b003 '")->select (); $attr = $car->table ("Nation")->select ();//table method can switch operation table//$attr = $car->field ("Name")-& Gt;select ();//field can specify the field of the query//$attr = $car->order ("Oil desc")->select ();//Sort//$attr = $car->limit (2,2)->select ();//Paging query, if a parameter n is to fetch n//$attr = $car->page (3,2)->select ();//Take the nth page of several data $attr = $car->field ("brand,count (*)")->group ("Brand")->select ();//group//$attr = $car-&G T;join ("Brand on Car.brand=brand.brand_code")->select (),//join on unites two tables//$attr = $car->distinct (t Rue)->field ("Oil")->select ();//deduplication, true to achieve de-weight, false//$attr = $car->find ("c001");//data based on primary key, not write primary key value default Return the first//$attr = $car->select ("c001");//based on the primary key value query, return a two-dimensional array, you can query more than one /*$attr = $car->select (); $this->assign ("Shuzu", $attr); $this->display ();*/ $attr=$car-Count();//aggregate function, placed at the last Max, Max; Avg,sum,min Echoattr; }//Data Additions functionAdd () {if(Empty($_post)) { $this-display (); } Else { //1. The array to be added must be an associative array, and key must be the field name $model= D ("Info"); /*$attr = Array (' Code ' = ' p007 ', ' Name ' = ' lily ', ' Sex ' =>true, ' Nation ' = ' n002 ', ' Birthday ' = ' 1993-5-8 ');*/ //Assign Value /*$attr ["Code"] = "p006"; $attr ["Name"] = "Jiaojiao"; $attr ["Sex"] = false; $attr ["Nation"] = "P001"; $attr ["Birthday"] = "1992-4-3";*/ //$model->add ($attr);//The method that adds the data requires a parameter, which is an array//2.ar way//1. Connection Class 2. Entity Class 3. Data Access Class /*$model->code = "p005"; $model->name = "Fang Hua"; $model->sex = true; $model->nation = "n003"; $model->birthday = "1995-3-4"; $model->add ();*/ //3. Automatically collect forms, create data $model-Create (); $model->sex =$_post["Sex"]== "male"?true:false; $z=$model-Add (); if($z) { $this->SUCCSS ("Add Data Success", "Add", 5);//prompt to add data successfully, jump to add page after 5 seconds } Else { $this->error ("Add failed," "Add", 3); } } }
InfoModel.class.php:
<? phpnamespace Home\model; Use Think\model; class extends model{}
Showall.html:
<Body><H1>Car Watch</H1><Tablewidth= "100%"cellpadding= "0"cellspacing= "0"Border= "1"><TR> <TD>Code</TD> <TD>Name</TD> <TD>Series</TD> <TD>Time to market</TD> <TD>Fuel</TD> <TD>Power</TD></TR><foreachname= "Shuzu"Item= "V"><TR> <TD><{$v. Code}></TD> <TD><{$v. Name}></TD> <TD><{$v. Brand}></TD> <TD><{$v. Time}></TD> <TD><{$v. Oil}></TD> <TD><{$v. Puwer}></TD></TR></foreach></Table></Body>
Add.html:
<Body><formAction= "__action__"Method= "POST"><Div>Code:<inputtype= "text"name= "Code" /></Div><Div>Name:<inputtype= "text"name= "Name" /></Div><Div>Gender:<inputtype= "text"name= "Sex" /></Div><Div>National:<inputtype= "text"name= "Nation" /></Div><Div>Birthday:<inputtype= "text"name= "Birthday" /></Div><inputtype= "Submit"value= "Submit" /></form></Body>
June 16 Query and add of TP framework Database