Learning notes-php simple cascading drop-down menu output-2016.4.7

Source: Internet
Author: User

In the use of the cascade output of a small problem in a model, I did not pay attention to the model in different ways each time the data is given to the controller, I am in the form of an array of output, but this array I as a private field, to the time when the cascading menu output today, can not output normally, Finally, I have to. This private field is removed, initializing an array in the scope of each method, so that it does not interfere with each other! (It's too little to think about, too whimsical!) )

No, put the code.

HTML section

<select name= "NAV" >
<option> Please select a column category </option>
{$nav}
</select>

/********************************/
PHP Controller Section
<?php

Class Contentaction extends action{
Public function __construct (& $TPL) {
Parent::__construct ($TPL);
}

Public Function action () {
Switch ($_get[' action ']) {
Case ' show ':
$this->show ();
Break
Case ' Add ':
$this->add ();
Break
Case ' Update ':
$this->update ();
Break
Case ' Delete ':
$this->delete ();
Break
Default
Tool::alertback (' Illegal operation! ‘);
Break
}
}

Show
Private Function Show () {
$this->tpl->assign (' show ', true);
$this->tpl->assign (' title ', ' Document List ');
}

Add
Private function Add () {
$this->tpl->assign (' Add ', true);
$this->tpl->assign (' title ', ' New Document ');
Get the cascading drop-down menu
/*
* Use <optgroup></optgroup> group to get the main menu
* Re-content controller in the instantiation of the navigation model
*/
Initialize a variable
$htm = 0;
Instantiate the NAV model class
$nav = new Navmodel ();
foreach ($nav->getallnav () as $object) {//Here loop output main navigation
Outputs the primary navigation Optgroup label in a grouped form
$htm. = ' <optgroup label= "'. $object->nav_name. '" > '. ' \ r \ n "//here to be able to view the source code more clearly, so added \ r \ n
Give the ID of the primary to the private field of the navigation model
$nav->id = $object->id;
The same ID to find the corresponding PID, and to judge
if (!! $childNav = $nav->getallchildnav ()) {
Loop out the array data if it exists and output it with the option tag
foreach ($childNav as $obj) {
$htm. = ' <option value= "'. $obj->id. '" > '. $obj->nav_name. ' </option> '. ' \ r \ n ";
}
}
$htm. = ' </optgroup> ';
}
Registering variables with templates
$this->tpl->assign (' Nav ', $htm);
}


}

Good night!

Learning notes-php simple cascading drop-down menu output-2016.4.7

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.