<?php ob_start(); /* * Copyright (c) 2003 NightKids <[email protected]> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #生成下拉框、复选、单选框的几个类,支持数据库结果集生成 /************************************************************************************************************* * The PHP File Create By NightKids * E-Mail:[email protected] * 主页: * 作者:NightKids * * * * 最后修改时间:2003-07-27 10:45:05 * * 声明:任何个人或团体可以任意修改、传播代码、可以用于任何场所(造成的任何损失或纠纷与本人无关)。 * 如果有什么好的建议和意见请与我联系。 * 请你在使用或传播是请保留这段注释,尊重本人的劳动成果。谢谢!! * * 感谢:所有身边的朋友、所有支持我的人们。 * 所有无私提供资料的网友和朋友们。所有 PHP 支持者 ! * * * 开发环境:FreeBSD5.1+Apache/2.0.47+PHP 5.0.1B+Zend Engine v2.0.0+MySQL 4.1.14+Zend Optimizer v2.1.0 * * 要求: *************************************************************************************************************/ //=========== html 下拉栏 类 //根据一个数组生成一个下拉框 //功能:可以初始化为选中其中的一项,默认为第一项 /* 下拉框: select 选项: option 选中的: selected 名字: name ID: id css 样式: class 项目标签: 标签值: */ //---------------------------------------------------------------------------------------- /* $dbc=mysql_connect(); mysql_select_db("felonious",$dbc);//连接数据库 $q=mysql_query("select * from felonious",$dbc);//进行数据库操作 while($row=mysql_fetch_array($q)) { $rows[]=$row; }
$Item=array("NULL"=>"没有可选项","1"=>"ffff","2"=>"aaaaaaa"); $selected=array("10","15","20"); //单选时,第一个元素有效 $ds = new CxSelectDown(); $ds->dbitem=true; $ds->itemvalue="auto_id"; //项的值,数据表里的字段名 $ds->itemlable="felonious";//项的标签,数据表里的字段名 $ds->multiple=false; $ds->SetName("Test"); $ds->SetStyle("ClassName"); $ds->SetID("test"); $ds->SetDir("right"); $ds->SetAccKey("b"); $ds->SetDisabled(false); $ds->SetSize(0); $ds->SetItem($rows,$selected); echo $ds->show(); //--------------------------------------------------------------------------------------- 最简单用法 $ds = new CxSelectDown(); $ds->SetItem($Item); echo $ds->show(); //---------------------------------------------------------------------------------------- 根据从数据库取得的 row 生成一个下拉框 方法: Setitem 设置项目数据 参数 $rows 项目数组,$selected 当前选中的项目 该值为项目的 value 或数组(在multiple时) 属性: dbitem [true/false] default false itemvalue 项目的值,只有 dbitem 是 true 时有效 itemlable 项目的标签,只有 dbitem 是 true 时有效 如果 dbitem 为 true 时 方法 Setitem 的数据必须为 用 mysql_fetch_array 取的数据 否则 数组结构为 $Item=array("NULL"=>"没有可选项","1"=>"ffff","2"=>"aaaaaaa","value"=>"lable"); */ class CxSelectDown { //========================= // 值 标签 var $_NullItem=array("NULL"=>"没有可选项"); var $_Item=array(); var $_Selected=NULL; //========================= var $name=NULL; var $id=NULL; var $dir=NULL; var $accesskey=NULL; var $disabled=NULL; var $size=NULL; var $action=NULL;//其他操作 onChange .... //========================= var $dbitem=false; //如果是从数据库取得的数组还要设置下面几个参数 var $itemvalue; //项的值 var $itemlable;//项的标签 var $multiple=false; //多选,列表框 //========================= function CxSelectDown() { } //========================= function SetItem($item=NULL,$selected=NULL){ if(!empty($item)) $this->_Item=$item; else $this->_Item=$this->_NullItem;
$this->_Selected=$selected;
if($this->dbitem==true) //是直接从数据库 $item[]=$row 形式取得的数组 $this->_Item=$this->ConvItem($item); //转换数组
} //========================= function ConvItem($item){ $value=$this->itemvalue; $lable=$this->itemlable; for ($i=0;$i<=@count($item)-1;$i++){ $key=$item[$i][$value]; $var=$item[$i][$lable]; $ttrow[$key]=$var; } return $ttrow; } //========================= function show(){ $itemStart=$this->GetItemHead(); $itemBody=$this->GetItemBody(); $itemEnd=$this->GetItemFoot(); $item=$itemStart.$itemBody.$itemEnd; return $item; } function GetItemBody(){ $item=$this->_Item; $selected=$this->_Selected; $selectednum=@count($selected); while(@list($value,$lable)=@each($item)){ $isselected=NULL; if($this->multiple==true){ //多选处理 for ($i=0;$i<=$selectednum-1;$i++){ if($value==$selected[$i]){ $isselected="selected"; } } } else{ //单选处理 if($value==$selected) $isselected="selected"; } $itemStr.=" <option value=\"$value\" $isselected>$lable</option>\n"; } return $itemStr; } function GetItemHead() { $name=$this->GetName(); $id=$this->GetID(); $dir=$this->GetDir(); $accesskey=$this->GetAccKey(); $disabled=$this->GetDisabled(); $size=$this->GetSize(); $action=$this->GetAction(); if($this->multiple==true) { $multiple=" multiple "; } $itemStart="<select".$name.$id.$dir.$accesskey.$disabled.$size.$action.$multiple.">\n"; return $itemStart; } function GetItemFoot(){ $itemEnd="</select>\n"; return $itemEnd; } //========================= function SetAction($action){ $this->action=" $action "; } function GetAction(){ return $this->action." "; } function SetName($Name){ $this->name=' name="'.$Name.'"'; if($this->multiple==true) $this->name=' name="'.$Name.'[]"'; } function GetName(){ return $this->name." ";} //========================= function SetID($ID){ $this->id=' id="'.$ID.'"';} function GetID() { return $this->id." ";} //========================= function SetDir($Dir){ $Dir=@strtolower($Dir); switch ($Dir){ case "left": $this->dir=" dir=\"rtl\""; break; case "right": $this->dir=" dir=\"ltr\""; break; } } function GetDir(){ return $this->dir." ";} //========================= function SetAccKey($key) { $this->accesskey=' accesskey="'.$key.'"';} function GetAcckey(){ return $this->accesskey." ";} //========================= function SetDisabled($disabled){ if($disabled==true) $this->disabled=" disabled"; } function GetDisabled(){ return $this->disabled." ";} //========================= function SetSize($size){ $this->Size=' size="'.$size.'"';} function GetSize(){ return $this->Size." ";} //========================= } //============================= //============================= //============================= /* #ClassName z99radio #@ Create By Night #@ 2003-07-28 #生成一个单选组 方法: - SetName 设置名字 - SetItem 设置数据 (二维数组,选中的ID) - ReturnString 直接返回字符串 - ReturnArray 返回一个一维数组 - Show ReturnString 的别名 */ //============================= /* #例子: $rs=new z99radio(); $item=array("-1"=>"保密","1"=>"男","2"=>"女"); $rs->SetName("sex"); $rs->SetItem($item,1); echo $rs->ReturnString(); $rs->RetuanArray(); */ //============================= class z99radio{ var $name=NULL; var $Item=array(); var $Selected=NULL; var $RadioArray=array(); var $RadioString=NULL; var $Action=NULL; //============================= function z99radio(){} //============================= function SetName($name){$this->name=$name;} function GetName(){ return $this->name;} function SetAction($action){$this->Action=$action;} function GetAction(){return $this->Action;} function SetItem($Item,$Selected=NULL){ $this->Item=$Item; $this->Selected=$Selected; } #名字只要一个 #值和标签是一个二维数组 #格式 array("value"=>"label"); function Radio(){ $name=$this->GetName(); $item=$this->Item; $selected=$this->Selected; $action=$this->Action; $i=0; while (list($value,$label)=each($item)){ $isSelected=NULL;
if($value==$selected) $isSelected="checked"; $idName=$name."_".$i; $Radio[]="<input type=\"radio\" name=\"$name\" id=\"$idName\" value=\"$value\" $isSelected $action><label for=\"$idName\">$label</label>\n"; $i++; } return $Radio; } //============================= function RetuanArray(){ return $this->Radio(); } function ReturnString(){ $Radios=$this->Radio(); $RadiosCount=count($Radios); $TmpStr=NULL; for($i=0;$i<=$RadiosCount-1;$i++){ $TmpStr.=$Radios[$i]; } return $TmpStr; } function Show(){ return $this->ReturnString(); } //============================= }//z99Radio Class THE END //============================= //============================= /* #ClassName z99CheckBox #@ Create By Night #@ 2003-07-28 #生成一个复选组 方法: - SetName 设置名字 - SetItem 设置数据 (二维数组,选中的ID一维数组) - ReturnString 直接返回字符串 - ReturnArray 返回一个一维数组 - Show ReturnString 的别名 */ //============================= /* #例子 $rs=new z99CheckBox(); $item=array("-1"=>"保密","1"=>"男","2"=>"女"); $selected=array("2","-1"); $rs->SetName("sex"); $rs->SetItem($item,$selected); $rs->CheckBox(); echo $rs->Show(); */ //============================= class z99CheckBox{ var $name=NULL; var $Item=array(); var $Selected=array(); var $CheckArray=array(); var $CheckString=NULL; var $Action=NULL; var $dbitem=false; var $itemvalue; var $itemlable; //============================= function z99CheckBox(){} //============================= function SetName($name){$this->name=$name;} function GetName(){ return $this->name;} function SetAction($action){$this->Action=$action;} function GetAction(){return $this->Action;} function SetItem($Item,$Selected=NULL){ $this->Item=$Item; $this->Selected=$Selected; } //============================= function ConvItem($item){ $value=$this->itemvalue; $lable=$this->itemlable; for ($i=0;$i<=@count($item)-1;$i++){ $key=$item[$i][$value]; $var=$item[$i][$lable]; $ttrow[$key]=$var; } return $ttrow; } //============================= function CheckBox(){ $name=$this->GetName(); $item=$this->Item; if($this->dbitem==true){ $item=$this->ConvItem($this->Item); } $selected=$this->Selected; $action=$this->Action; $i=0; while (list($value,$label)=each($item)){ $isCheck=NULL; if(is_array($selected)){ reset($selected); $selectedCount=count($selected); for($j=0;$j<=$selectedCount-1;$j++){ if($value==$selected[$j]){ $isCheck="checked"; break; } } } $idName=$name."_".$i; $CheckBox[]="<input type=\"checkbox\" name=\"$name\" id=\"$idName\" value=\"$value\" $isCheck $action><label for=\"$idName\">$label</label>\n"; $i++; } return $CheckBox; } //============================= function RetuanArray(){ return $this->CheckBox(); } function ReturnString(){ $Radios=$this->CheckBox(); $RadiosCount=count($Radios); $TmpStr=NULL; for($i=0;$i<=$RadiosCount-1;$i++){ $TmpStr.=$Radios[$i]; } return $TmpStr; } function Show(){ return $this->ReturnString(); } } //============================= /* 2003-08-13 10:18:03 NightKids 修改 idName 改 . 为 _ 2003-10-17 13:45:11 NightKids z99CheckBox 增加 dbitem 用法和下拉一样 */ //============================= # E-Mail:[email protected] //============================= ob_end_flush(); ?> 
|