Home  >  Article  >  Backend Development  >  DropDownList multi-level linkage dropDownList sample code_PHP tutorial

DropDownList multi-level linkage dropDownList sample code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:00857browse

View:
cdnauto/views/config/index.php

Copy code The code is as follows:

echo CHtml:: dropDownList('node', '', CHtml::listData(Node::model()->findAll(),'name','name'),array('empty'=>'--Please select the 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('--Please select server--'));

Controller:
cdnauto/controllers/ConfigController.php
Copy code 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' ]); //Get the node ID through the node name
$server = GossServer::model()->getServerByNodeid($nodeid); //Get the server information through the node ID
//$server is array Type, in the form $server = array(array('name'=>'name1'),array('name'=>'name2')); so two foreach
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
Copy code The code is as follows:

/**
* Get all server names under the node through 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
Copy code 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 no node is selected:
DropDownList multi-level linkage dropDownList sample code_PHP tutorial
I will fill in the details when I have time.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327952.htmlTechArticleView: cdnauto/views/config/index.php Copy the code as follows: echo CHtml::dropDownList('node ', '', CHtml::listData(Node::model()-findAll(),'name','name'),array('empty'='--Please select...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn