Use ThinkPHP to automatically implement unlimited classification instance details, thinkphp instance
I. Implementation results
Ii. Main Code
1. Template
2. Controller
· Index module
· Add Module
3. Model
Iii. Code
This allows you to test the copy function.
1. Template
<Form action = "_ URL _/add" method = "post"> topic <select name = "fid" size = 20> <option value = "0"> topic </option> <volist name = 'LIST' id = 'vo'> <option value = "{$ vo ['id']}"> {$ vo ['blank'] }{$ vo ['name'] }</option> </volist> </select> <br/> Add a topic: <input type = "text" name = 'name'/> <br/> <input type = "submit" value = "add"/> </form>
2. Controller
<? Phpclass IndexAction extends Action {/** + defaults * default operation + ---------------------------------------------------------- */public function index () {$ Column = new ColumnModel (); $ list = $ Column-> field ("id, name, fid, sort, concat (sort, '-', id) nsort")-> order ('nsort asc ') -> select (); foreach ($ list as $ key => $ val) {$ layer = count (explode ('-', $ list [$ key] ['nsort ']); $ list [$ key] [ 'Blank '] = ''; for ($ I = 0; $ I <$ layer; $ I ++) {if ($ I = $ layer-1) {$ list [$ key] ['blank ']. = '--- |';} else {$ list [$ key] ['blank ']. = '---' ;}}$ this-> assign ('LIST', $ list); $ this-> display ();} public function add () {$ Column = new ColumnModel; $ Column-> create (); if ($ Column-> add () {$ this-> success ('added successfully ');} else {$ this-> error ($ Column-> getError () ;}}}?>
3. Model
<? Phpclass ColumnModel extends Model {protected $ _ auto = array ('name', 'trim', 0, 'function '), // filter the blank characters array ('sort ', 'createsort', 0, 'callback') accidentally entered by the user '),); /** automatically complete the sort field * query the parent sort based on the fid of the POST to determine the level of sort */protected function createsort () {$ fid = $ _ POST ['fid']? (Int) $ _ POST ['fid']: 0; // if the user does not select the parent column, the default parent column id is 0if ($ fid! = '0') {$ list = $ this-> where ("id = $ fid")-> find (); $ data = $ list ['sort ']. '-'. $ fid;} else {$ data = '0';} return $ data ;}}?>
The above is a detailed description of the Automatic completion of ThinkPHP to implement unlimited classification instances. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!