Front-to-back thinkphp development Whole station (4)

Source: Internet
Author: User
Tags getmessage

Tonight to continue my development of this project, tonight is not much to write, mainly wrote a menu management function of the Curd method, the front-end interface has not been written.

Menu Management code for the model layer:

<?phpnamespace common\model;use think\model;class Menumodel extends model{private $_db= ';p ublic function __ Construct () {$this->_db=m ("menu");} /** * Insert menu Data */public function Insert ($data =array ()) {if (!data | |!is_array ($DATA)) {return 0;} return $this->_db->add ($data);} /** * Get Menu data */public function Getmenus ($data, $pageIndex, $pageSize =10) {$data [' status ']=array (' Neq ', -1); $offset = ($ PAGEINDEX-1) * $pageSize; $list = $this->_db->where ($data)->order (' Listorder desc,menu_id desc ')->limit ($ Offset, $pageSize); return $list;} /** * Gets the total number of menus */public function Getmenuscount ($data =array ()) {$data [' status ']=array (' Neq ', -1); return $this->_db- >where ($data)->count ();} /** * Gets the menu ID */public function Find ($id) {if (! $id | | |!is_numeric ($id)) {return array () according to ID;} return $this->_db->where ("menu_id={} $id")->find ();} /** * Update menu based on ID */public function Updatemenubyid ($id, $data) {if (! $id | |!is_numeric ($id)) {throw_exception ("id not valid");} if (! $data | |!is_array ($DATA)) {throw_exception (' updated data not valid ');} return $this->_db->where ("menu_id={$id}")->save ($data);} /** * Update queued ordinal */public function Updatemenulistorderbyid ($id, $listorder) {if (! $id | |!is_numeric ($id)) {throw_exception (' ID not valid ');} $data =array (' Listorder ' =>intval ($listorder);); return $this->_db->where ("menu_id={$id}")->save ($data );} /** * Get background menu */public function Getadminmenus () {$data =array (' Status ' =>array (' Neq ', -1), ' type ' =>1); return $this- >_db->where ($data)->order (' Listorder desc,menu_id desc ')->select ();} /** * Get front menu */public function Getbarmenus () {$data =array (' status ' =>1, ' type ' =>0); return $this->_db->where ($data)->order (' Listordre desc,menu_id desc ')->select ();}? >

The code for the menu management controller class:

<?phpnamespace admin\controller;use think\controller;class Menucontroller extends Commoncontroller{public function index () {$data =array (), if (Isset ($_request[' type ') ' && In_array ($_request, Array (0,1))) {$data [' type ']=intval ($_request[' type '); $this->assign (' type ', $data [' type ']);} else{$this->assign (' type ',-100);}} Public function Add () {if ($_post) {if (!isset ($_post[' name ')]) {return Jsonresult (0, ' menu name cannot be empty ')} if (!isset ($_post[' m ']) | |!$_post[' m ']) {return Jsonresult (0, ' module name cannot be null ');} if (!isset ($_post[' C ')) | |!$_post[' C ']) {return Jsonresult (0, ' controller cannot be empty ');} if (!isset ($_post[' F ')) | |!$_post[' F ']) {return Jsonresult (0, ' method name cannot be null ');} if ($_post[' menu_id ') {return $this->save ($_post);} $menuId =d ("menu")->insert ($_post), if ($menuId) {return Jsonresult (1, ' new success ', $menuId);} Return Jsonresult (0, ' new failed ', $menuId);} else{$this->display ();}} Public function edit () {$menuId =$_request[' id '), $menu =d ("menu")->find ($menuId), $this->assign (' menu ', $menu); $this->display ();}public function Save ($data) {$menuId = $data [' menu_id '];unset ($data [' menu_id ']); try{$id =d ("menu") Updatemenubyid ($menuid, $data), if ($id ===false) {return Jsonresult (0, ' save Failed ');} Return Jsonresult (0, ' Save As ');} catch (Exception $ex) {return Jsonresult (0, $ex->getmessage ());}} Public Function SetStatus () {try{if ($_post) {$id =$_post[' id '); $status =$_post[' status ']; $ret =d ("menu") Updatestatusbyid ($id, $status); if ($ret) {return Jsonresult (1, ' Operation succeeded ');} Else{return jsonresult (0, ' operation failed ');}}} catch (Exception $ex) {return Jsonresult (0, $ex->getmessage ());} Return Jsonresult (0, ' no data submitted ');} /** * Data sort */public function Listorder () {$listoreder =$_post[' listorder ']; $data =array (' jump_url ' = $_server[' http_ REFERER '), $errors =array (), if ($listoreder) {Try{foreach ($listorder as $emnuId = $v) {$id =d ("menu") Updatemenulistorderbyid ($menuId, $v), if ($id ===false) {$errors []= $menuId;}}} catch (Exception $ex) {return Jsonresult (0, $ex->getmessage (), $data)}if ($errors) {return Jsonresult (0, "Sort failed-". Implode (', ', $errors), $data);} Return Jsonresult (1, ' sort succeeded ', $data)}return jsonresult (0, ' data sort failed ', $data);}}? >

Tonight to write this little bit, tomorrow night began to do the front-end development, the next Friday, if not to work overtime in Saturday, I will increase horsepower in this week to end the project. (^_?) ☆

Source Address: Https://github.com/YoZiLin/TP-CMS

Front-to-back thinkphp development Whole station (4)

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.