Use ThinkPHP to automatically implement unlimited classification instance details, thinkphp instance

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.