Drop-down list multi-level linkage dropDownList sample code _ PHP Tutorial

Source: Internet
Author: User
The dropDownList sample code is linked to multiple levels in the drop-down list. View: cdnautoviewsconfigindex. the php copy code is as follows: echoCHtml: dropDownList (node, CHtml: listData (Node: model ()-findAll (), name, name ), array (empty -- select View:
Cdnauto/views/config/index. php

The code is as follows:


Echo CHtml: dropDownList ('node', '', CHtml: listData (node: model ()-> findAll (), 'name', 'name '), array ('empty' => '-- select a node --',
'Id' => 'node ',
'Ajax '=> array (
'Type' => 'post ',
'URL' => Yii: app ()-> createUrl ('cdnauto/config/getnodeserversbynodename '),
'Update' => '# servers ',
'Data' => array ('node _ name' => 'JS: $ ("# node"). val ()'),
)
)
);
Echo "";
Echo CHtml: dropDownList ('servers', '', array ('-- Select Server --'));


Controller:
Cdnauto/controllers/ConfigController. php

The code is as follows:


Public function actionGetNodeServersByNodeName (){
// If (! Yii: app ()-> request-> isAjaxRequest)
// Throw new CHttpException (404 );
$ Node_name = $ _ POST ['node _ name'];
$ Nodeid = Node: model ()-> getNodeId ($ _ POST ['node _ name']); // Obtain the node ID using the Node name
$ Server = GossServer: model ()-> getServerByNodeid ($ nodeid); // Obtain server information through node ID
// $ Server is an array type, such as $ server = array ('name' => 'name1'), array ('name' => 'name2 ')); so two foreach requests are required.
If (isset ($ server )){
Foreach ($ server as $ k => $ v ){
Foreach ($ v as $ kk => $ vv ){
Echo CHtml: tag ('option', array ('value' => $ kk), CHtml: encode ($ vv), true );
}
}
} Else {
Echo CHtml: tag ('option', array ('value' => ''), 'servers', true );
}
}


Model:
GossServer. php

The code is as follows:


/**
* Obtain the names of all servers under the node by using the node ID.
* @ Author ysdaniel
*/
Public static function getServerByNodeid ($ nodeid)
{
$ SQL = "SELECT name FROM OSS_Server WHERE nodeid = '{$ nodeid }'";
/// $ SQL = "SELECT name, nodeid FROM OSS_Server WHERE nodeid = '{$ nodeid}'"; // both OK
$ Cmd = Yii: app ()-> db-> createCommand ($ SQL );
$ Ret = $ cmd-> queryAll ();
If (! $ Ret ){
Throw new Exception ("The server corresponding to this node cannot be found ");
}
Return $ ret;
}


Node. php

The code is as follows:


/**
* Get the nodeid name through nodename
* @ Author
*/
Public static function getNodeId ($ name)
{
$ SQL = "SELECT id FROM OSS_Node WHERE name = '{$ name }'";
$ Cmd = Yii: app ()-> db-> createCommand ($ SQL );
$ Ret = $ cmd-> queryAll ();
If (! $ Ret ){
Return null;
// Throw new Exception ("Node {$ name} Not Found }");
}
Return $ ret [0] ['id'];
}


Others:
Data table structure
Effect:
Before selecting a node:

Make up the details.

Export cdnauto/views/config/index. the php code is as follows: echo CHtml: dropDownList ('node', '', CHtml: listData (node: model ()-findAll (), 'name ', 'name'), array ('empty' = '-- select...

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.