The above is 1. database Design 2. the data in the added database is parsed as follows: the parent class id of the self-incrementing idpid column in the cid column. The id is 0, indicating the name of the cat_name column in the top-level column p...
The above is
1. database design
2. the added database data is shown in figure
Resolution: cid column auto-increment id
The parent class id of the pid column. if the id is 0, it indicates that it is a top-level column.
Cat_name topic name
Path indicates the hierarchical relationship of a topic.
The implementation results are mainly used:
A mysql function concat (): returns the string generated by the connection parameter.
1. classification display method of the controller
- Public function fenlei (){
- $ Cate = M ('Category ');
- $ List = $ cate-> field ("cid, cat_name, pid, path, concat (path, '-', cid) as bpath")-> order ('bpath ') -> select ();
- Foreach ($ list as $ key => $ value ){
- $ List [$ key] ['count'] = count (explode ('-', $ value ['bpath']);
- }
- $ This-> assign ('alist', $ list );
- $ This-> display ();
- }
2. how to add a controller category
- Public function addCat (){
- $ Cate = D ('Category ');
- If ($ cate-> create ()){
- If ($ cate-> add ()){
- $ This-> redirect ('/Test/fenlei ');
- } Else {
- $ This-> error ('add topic failed ');
- }
- } Else {
- $ This-> error ($ cate-> getError ());
- }
- }
3. topic model
-
- Class CategoryModel extends Model {
- Protected $ _ auto = array (
- Array ('path', 'tclm', 3, 'callback '),
- );
- Function tclm (){
- $ Pid = isset ($ _ POST ['pid'])? (Int) $ _ POST ['pid']: 0;
- // Echo ($ pid );
- If ($ pid = 0 ){
- $ Data = 0;
- } Else {
- $ List = $ this-> where ("cid = $ pid")-> find ();
- $ Data = $ list ['path']. '-'. $ list ['CID']; // The path of the subclass adds the cid of the parent class to the path of the parent class.
- }
- Return $ data;
- }
- }
- ?>
4. main html code
-
-
-