http://hi.baidu.com/yesely/item/9e1ce0ca4dac5b7889ad9ebc
dedecms 添加 已经固化在发布表单中字段的作用
dedecms 添加 已经固化在发布表单中字段的作用
例如要添加一个下拉列表,下拉列表的值来自于数据库中某个字段的值.
添加方式
1 添加字段时候 选择 已经固化在发布表单中字段的作用
2 数据类型: 使用option下拉框
3 默认值:要调用的表名,要调用的字段名
dedecms修改方式:
1 修改 文档发布页 默认archives_add.htm中, PrintAutoFieldsAdd函数,PrintAutoFieldsAdd($cInfos['fieldset'],'autofield');
2 修改 文档发布页 默认archives_edit.htm中, PrintAutoFieldsEdit函数,PrintAutoFieldsEdit($cInfos['fieldset'],$addRow,'autofield');
3 dede/inc/inc_archives_functions.php 中修改函数 PrintAutoFieldsAdd
3.1增加自定义函数
//获得指定表的指定字段的内容
function GetTableComnusValue($table,$comnus){
global $dsql;
$query = "select $comnus from $table";
$dsql->SetQuery($query);
$dsql->Execute();
while($row = $dsql->GetArray()){
$data[]=$row[$comnus];
}
return $data;
}
3.2 foreach($dtp->CTags as $tid=>$ctag)循环中, 加上else 判断 已经固化中的情况
else if( $loadtype=='autofield' && $ctag->GetAtt('autofield')==0 ){
$TableComnus=explode(",",$ctag->GetAtt('default'));
$fieldValues=implode(",",GetTableComnusValue("]", $TableComnus[1] ));
$dede_addonfields .= ( $dede_addonfields=="" ? $ctag->GetName().",".$ctag->GetAtt('type') : ";".$ctag->GetName().",".$ctag->GetAtt('type') );
$ctag->SetAtt('default',$fieldValues);
echo GetFormItemA($ctag);
}
4 include/dedetag.class.php中,增加修改对象属性的函数
模仿
function GetAtt($str)
{
return $this->CAttribute->GetAtt($str);
}
增加
function SetAtt($str,$value)
{
return $this->CAttribute->SetAtt($str,$value);
}
模仿
//获得某个属性
function GetAtt($str)
{
if($str=="")
{
return "";
}
if(isset($this->Items[$str]))
{
return $this->Items[$str];
}
else
{
return "";
}
}
增加
function SetAtt($str,$value)
{
if($str=="")
{
return "";
}
if(isset($this->Items[$str]))
{
$this->Items[$str]=$value;
return $this->Items[$str];
}
else
{
return "";
}
}
5 同样修改函数 function PrintAutoFieldsEdit(&$fieldset, &$fieldValues, $loadtype='all')
else if( $loadtype=='autofield' && $ctag->GetAtt('autofield')==0 ){
// 通过传入的表名,字段名,替换为数据库中字段实际值
$TableComnus=explode(",",$ctag->GetAtt('default')); //获取表名,字段名
$fieldValues=implode(",",GetTableComnusValue("]", $TableComnus[1] ));//读取值
$ctag->SetAtt('default',$fieldValues);//赋值给原先对象
$dede_addonfields .= ( $dede_addonfields=="" ? $ctag->GetName().",".$ctag->GetAtt('type') : ";".$ctag->GetName().",".$ctag->GetAtt('type') );
echo GetFormItemValueA($ctag,, $fieldValues[$ctag->GetName()]);
}