Home  >  Article  >  Backend Development  >  PHP drop-down list multi-level linkage dropDownList sample code

PHP drop-down list multi-level linkage dropDownList sample code

怪我咯
怪我咯Original
2017-07-12 15:45:412414browse

DropDownList control is also called drop-down list box control. Multiple rows of data in the control list are expressed in an implicit form. When the user needs to select the desired list item, click the "lower triangle" graphic Display, the user can only select one data item at a time. The DropDownList control is actually a container for list items, and the drop-down list box uses the Items collection to represent the contents of each item. If in ASP. NET page, manually fill in the columns table options of the DropDownList control one by one. When there are many list items, it will be more cumbersome and more troublesome to modify. The DropDownList control is dynamically connected to the database, queries the list option data from the database according to specified conditions, and then binds it to the control, which can quickly and easily display multiple drop-down options. At the same time, by modifying the data in the database, the drop-down options can be changed dynamically. For example, in the tutor selection system, when the graduate tutor fills in the application information, he needs to select the application type, and the same information appears on multiple pages. Bind the data of the application type table in the database to the DropDownList control. , can solve the problem better.

This article will give you a detailed introduction to Drop-down listThe specific implementation code of multi-level linkage dropDownList

View:
cdnauto/views/config/index.php

The code is as follows:

echo CHtml::dropDownList('node', '', CHtml::listData(Node::model()->findAll(),'name','name'),array('empty'=>'--请选择节点--', 
'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('--请选择服务器--'));

Controller:
cdnauto/controllers/ConfigController.php

public function actionGetNodeServersByNodeName(){ 
// if(!Yii::app()->request->isAjaxRequest) 
// throw new CHttpException(404); 
$node_name = $_POST['node_name']; 
$nodeid = Node::model()->getNodeId($_POST['node_name']); //通过节点名称获取该节点ID 
$server = GossServer::model()->getServerByNodeid($nodeid); //通过节点ID获取服务器信息 
//$server 为array类型,形如 $server = array(array('name'=>'name1'),array('name'=>'name2'));所以需要两次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

/** 
* 通过节点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("找不到这个节点对应的服务器"); 
} 
return $ret; 
}

Node.php

/** 
* 通过nodename获取nodeid名 
* @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}"); 
} 
return $ret[0]['id']; 
}

Others:
Data table structure
Effect:
Before no node is selected:
PHP drop-down list multi-level linkage dropDownList sample code

The above is the detailed content of PHP drop-down list multi-level linkage dropDownList sample code. For more information, please follow other related articles on the PHP Chinese website!

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