phpcms v9,我们在做类似于酒店房型等类型的时候,需要用到文本组字段模型,但phpcms并未提供该模型。如下图所示效果:
展示效果如下:
![PHPCMS V9二次开发]自定义字段模型-文本组 PHPCMS V9二次开发]自定义字段模型-文本组](https://image.miaokee.com:8440/aHR0cDovL3NtZWxsYWdlLmNvbS9uZXdzcGhvdG8vMjAxMi0xMC8xOTIxNjgwMTU2MjAxMjEwMjcwOTI4NTExLmpwZw%3D%3D.jpg?w=700)
![PHPCMS V9二次开发]自定义字段模型-文本组 PHPCMS V9二次开发]自定义字段模型-文本组](https://image.miaokee.com:8440/aHR0cDovL3NtZWxsYWdlLmNvbS9uZXdzcGhvdG8vMjAxMi0xMC8xOTIxNjgwMTU2MjAxMjEwMjcwOTI4NTIyLmpwZw%3D%3D.jpg?w=700)
步骤/方法
打开phpcms\modules\content\fields目录,复制文件夹downfiles,并改名为textgroups。
打开phpcms\modules\content\fields\fields.inc.php文件,增加字段类型:
'textgroups'=>'多文件上传',
打开phpcms\modules\content\fields\textgroups目录(第一步复制的文件夹),修改以下文件:
form.inc.php
function textgroups($field, $value, $fieldinfo) {
extract(string2array($fieldinfo['setting']));
$list_str = '';
if($value) {
$value = string2array(html_entity_decode($value,ent_quotes));
if(is_array($value)) {
foreach($value as $_k=>$_v) {
$list_str .= "<div id='textsgroups{$_k}'> <input type='text' name='{$field}_fx[]' value='{$_v[fx]}' style='width:100px;' class='input-text'> <input type='text' name='{$field}_fj[]' value='{$_v[fj]}' style='width:100px;' class='input-text'> <input type='text' name='{$field}_cx[]' value='{$_v[cx]}' style='width:100px;' class='input-text'> <input type='text' name='{$field}_kd[]' value='{$_v[kd]}' style='width:100px;' class='input-text'> <input type='text' name='{$field}_vip[]' value='{$_v[vip]}' style='width:100px;' class='input-text'> <a href=\"javascript:remove_div('textsgroups{$_k}')\">".l('remove_out')."</a></div>";
}
}
}$string ='<script type=text/javascript>
function add_textsfile(returnid) {
var ids = parseint(math.random() * 10000);
var str = "<li id=\'textsgroups"+ids+"\'> <input type=\'text\' name=\'"+returnid+"_fx[]\' value=\'\' style=\'width:100px;\' class=\'input-text\'> <input type=\'text\' name=\'"+returnid+"_fj[]\' value=\'\' style=\'width:100px;\' class=\'input-text\'> <input type=\'text\' name=\'"+returnid+"_cx[]\' value=\'\' style=\'width:100px;\' class=\'input-text\'> <input type=\'text\' name=\'"+returnid+"_kd[]\' value=\'\' style=\'width:100px;\' class=\'input-text\'> <input type=\'text\' name=\'"+returnid+"_vip[]\' value=\'\' style=\'width:100px;\' class=\'input-text\'> <a href=\"javascript:remove_div(\'textsgroups"+ids+"\')\">remove</a> </li>";
$(\'#\'+returnid).append(str);
}</script>';
$string .= '<input name="info['.$field.']" type="hidden" value="1">
<fieldset class="blue pad-10">
<legend>'.l('mm_fxlist').'</legend><div id="tt">
<input type="text" value="'.l('mm_fx').'" readonly style="width:100px;border:0;" class="input-text">
<input type="text" value="'.l('mm_fj').'" readonly style="width:100px;border:0;" class="input-text">
<input type="text" value="'.l('mm_cx').'" readonly style="width:100px;border:0;" class="input-text">
<input type="text" value="'.l('mm_kd').'" readonly style="width:100px;border:0;" class="input-text">
<input type="text" value="'.l('mm_lyj').'" readonly style="width:100px;border:0;" class="input-text">
</div>';
$string .= $list_str;
$string .= '<ul id="'.$field.'" class="piclist"></ul>
</fieldset>
<div class="bk10"></div>
';
$string .= $str."<input type=\"button\" class=\"button\" value=\"".l('mm_addfx')."\" onclick=\"add_textsfile('{$field}')\">";
return $string;
}修改input.inc.php
function textgroups($field, $value) {
$hotel = $_post[$field.'_fx'];
$hotel_fj = $_post[$field.'_fj'];
$hotel_cx = $_post[$field.'_cx'];
$hotel_kd = $_post[$field.'_kd'];
$hotel_vip = $_post[$field.'_vip'];
$array = $temp = array();
if(!empty($hotel)) {
foreach($hotel as $key=>$hote) {
$temp['fx'] = $hote;
$temp['fj'] = $hotel_fj[$key];
$temp['cx'] = $hotel_cx[$key];
$temp['kd'] = $hotel_kd[$key];
$temp['vip'] = $hotel_vip[$key];
$array[$key] = $temp;
}
}
$array = array2string($array);
return $array;
}修改的output.inc.php
function textgroups($field, $value) {
return string2array($value);
}
更新后台缓存。在模型中新建字段,可以看到文本组,创建后就可以添加。
前台调用:<table>
<tr><th>房型</th><th>房价</th><th>床型</th><th>路游价</th></tr>
{loop $fxinfo $v} <!--$fxinfo为字段名称-->
<tr><td>{$v[fx]}</td><td>¥{$v[fj]}元</td><td>{$v[cx]}</td><td><strong style="color:#f60;font-size:18px;font-family:tahoma,helvetica,arial,sans-serif;">¥{$v[vip]}</strong>元</td></tr>
{/loop}
</table>
注意事项
注意修改几个文件中的函数名称,这个很容易忽略。
注意文件内容中引号的闭合和js代码。