"One" preparatory work
(1) Create menu, modify jump Path
<li> Class= "Workermanage" > Staff management </a> <ul> class= "Workermanage > Staff list </a></li> class= "Workermanage" > Add staff </a></li> </ul> </li>
(2) Create Controller UserController.class.php
<? php namespace Admin\controller; Use Think\controller; class extends controller{ }?>
"Two" staff added
Analysis: Controller UserController.class.php
Method: Add
Template: add.html
(1) Add method, display template to add staff
Public function Add () { $this,display (); }
(2) Copy template file add.html to the specified location admin/view/user/add.html, modify the static resource path
(3) Rewrite the Add method, query the department information, display the drop-down list of the template
public function add () { // Query Department information $data = M (' dept ')->field (' Id,name ')->select (); $this ->assign (' data ', $data Span style= "color: #000000"); $this ->display (); }
Department: <select name= "dept_id" > <foreach name= "Data" item= "fo" > <option value= "{$fo . ID} ">{$fo.name}</option> </foreach> </select>
(4) Check the template form
Submit a single current page can not be written, or the current controller under the Add method
<form action= "" method= "post" > <fieldset> <legend> Add Staff </legend> <p><labe L for= "username" > User name: </label><input type= "text" name= "username" id= "username" ></p> <p><la Bel for= "Password" > Password: </label><input type= "text" name= "password" id= "password" ></p> <p><lab El for= "nickname" > Name: </label><input type= "text" name= "nickname" Id= "nickname" ></p> <p><lab El for= "Truename" > Full name: </label><input type= "text" name= "Truename" id= "Truename" ></p> <p>Department:<select name= "dept_id" > <foreachName= "Data" item= "fo" > <option value= "{$fo. ID} ">{$fo.name}</option> </foreach> </select> </p> <p>Gender:<label for= "man" > Male </label><input type= "Radio" name= "Sex" value= "1" id= "mans" > <label for= "Woman" > Female </label><input type= "Radio" name= "Sex" value= "2" id= "Woman" > </p> <P&G T;<label for= "Birthday" > Birthdays: </label><input type= "date" name= "Birthday" id= "Birthday" ></p> <p><lab El for= "Tel" > Tel: </label><input type= "Text" name= "Tel" id= "Tel" ></p> <p><label for= "Email" > Email: </label><input type= "text" name= "email" id= "email" ></p> <p><label for= "Remark" > Remarks: </label><textarea name= "Remark" id= "remark" ></textarea></p> <p> <a href= "javascript:;" id= "submitbtn" > Submit </a> <a href= "javascript:;" id= "resetbtn" > Empty < ;/a> </p> </fieldset></form><script src= "Https://cdn.bootcss.com/jquery/3.3.1/jquery.js" ></script><script type= "Text/javascript" >$ (document). Ready (function(){ $(' #submitBtn '). On (' click ',function(){ $(' Form ').submit (); }); $(' #resetBtn '). On (' click ',function(){ $(' form '). Get (0).Reset(); }); });</script>
Attention:
① empty method Remember to convert the jquery object to JS Dom object, otherwise you cannot reset the operation with JS's reset
The ② time control uses the H5 type= "date"
(5) Rewrite the Add method, write processing form code, to achieve the data receiving and storage
Public functionAdd () {if(Is_post) {//Data submission//Data object creation method receive data $model= M (' user '); //Create a data object (because there is a release time, not in the template, so add it here) $data=$model-Create (); //Add Time field $data[' addtime '] = Time(); //Writing data Tables $result=$model->add ($data); if($result) { $this->success (' Add Success ', U (' Showlist '), 3); }Else{ $this->error (' Add failed '); } }Else{//Template Display//query Department information $data= M (' dept ')->field (' Id,name ')->select ();//Select returns a two-dimensional array, find returns a one-dimensional array $this->assign (' Data ',$data); $this-display (); } }
Display of the "three" staff list
Controller: UserController.class.php
Method: Showlist
Template: showlist.html
(1) Create Showlist method, display data and templates
Public function showlist () { // display data $data = M (' user '),Select (); $this->assign (' data ',$data); // Presentation Templates $thisdisplay (); }
(2) Copy the template file showlist.html to the specified location admin/view/user/showlist.html, modify the static resource path
(3) Displaying data to a template
Note: Because data is the result returned by the select query, it is a two-dimensional array. In the template you need to traverse
<table border= "1" cellspacing= "0" cellpadding= "ten" > <thead> <tr><td> serial number </td><td& gt; Name </td><td> Nickname </td><td> Department </td><td> gender </td><td> birthday </td> <td> phone </td><td> mailbox </td> <td> Add time </td><td> edit </td></tr> </ Thead> <foreach> <volist name= "Data" Id= "Vol" > <tr> <td>{$vol.id}</td> <td>{$vol.username}</td> <td>{$vol.nickname}</td> <td>{$vol.dept_id}</td> <td>{$vol.sex}</td> <td>{$vol.birthday}</td> <td>{$vol.tel}</td> <td>{$vol.email}</td> <td>{$vol. addtime|Date= ' y-m-d h:i:s ',###}</td><td><a href= "javascript:;" > Edit </a></td> </tr> </volist> </foreach></table>
Four
Video Learning Transcript---thinkphp---case 2--staff management function