应用场景:有两个模型,设计师,设计案例。需要在设计师的页面添加他的案例,需要在案例的页面添加设计师。
思路实现,使用“相关文章”功能,但是只有同模型的文章才可以选择,而其他模型的文章是选择不了的,所以就需要对它进行一些修改了。
涉及到的文件如下:
statics\js\content_addtop.js phpcms\modules\content\content.php phpcms\modules\content\templates\relationlist.tpl.php
1.修改系统默认的relation字段表单代码如下:
<input type='hidden' name='info[relation]' id='relation' value='{FIELD_VALUE}' style='50' > <ul class="list-dot" id="relation_text"></ul> <div> <input type='button' value="添加相关" onclick="omnipotent('selectid','?m=content&c=content&a=public_relationlist&modelid=目标模型ID&modelname=relation','添加相关文章',1)" class="button" style="width:66px;"> <span class="edit_content"> <input type='button' value="显示已有" onclick="show_myrelation(当前模型ID,目标模型ID,{ID},'relation')" class="button" style="width:66px;"> </span> </div>
其它默认即可。
2.打开网站根目录文件statics\js\content_addtop.js
修改remove_relation函数为:
function remove_relation(sid, id, modelname) { var relation_ids = $('#' + modelname).val(); if (relation_ids != '') { $('#' + sid).remove(); var r_arr = relation_ids.split('|').filter(function (item) { return item != id }); $('#' + modelname).val(r_arr.join('|')); } }
修改show_relation函数为:
function show_relation(modelid, id, fieldname) { $.getJSON("?m=content&c=content&a=public_getjson_ids&modelid=" + modelid + "&id=" + id, function(json) { var newrelation_ids = ''; if (json == null) { return alert('没有添加相关文章'); } $.each(json, function(i, n) { newrelation_ids += "<li id='" + n.sid + "'>·<span>" + n.title + "</span><a href='javascript:;' class='close' onclick=\"remove_relation('" + n.sid + "'," + n.id + ",'" + fieldname + "')\"></a></li>"; }); $('#relation_text').html(newrelation_ids); }); }
新增show_myrelation函数:
function show_myrelation(modelid, modelid2, id, fieldname) { $.getJSON("?m=content&c=content&a=public_getjson_ids2&modelid=" + modelid + "&modelid2=" + modelid2 + "&id=" + id + "&fieldname=" + fieldname, function(json) { var newrelation_ids = ''; if (json == null) { return alert('没有添加相关文章'); } var exsist = [] $.each(json, function(i, n) { exsist.push(n.id) newrelation_ids += "<li id='v"+modelid2+n.id+"'>·<span>" + n.title + "</span><a href='javascript:;' class='close' onclick=\"remove_relation('v"+modelid2+n.id+"'," + n.id + ",'" + fieldname + "')\"></a></li>"; }); $('#' + fieldname).val(exsist.join('|')) $('#' + fieldname + '_text').html(newrelation_ids); }); }
3.打开phpcms\modules\content\content.php文件
修改public_relationlist函数,在$infos = $this->db->listinfo($where,'',$page,12);这句上面增加一句:
$modelname=$_GET['modelname'];
新增函数public_getjson_ids2:
public function public_getjson_ids2() { $modelid = intval($_GET['modelid']); $modelid2 = intval($_GET['modelid2']); $fieldname = $_GET['fieldname']; $id = intval($_GET['id']); $this->db->set_model($modelid); $tablename = $this->db->table_name; $this->db->table_name = $tablename.'_data'; $r = $this->db->get_one(array('id'=>$id),$fieldname); if($r["{$fieldname}"]) { $myrelation = str_replace('|', ',', $r["{$fieldname}"]); $myrelation = trim($myrelation,','); $where = "id IN($myrelation)"; $infos = array(); $this->db->set_model($modelid2); $this->model = getcache('model', 'commons'); $this->db->table_name = $this->db->db_tablepre.$this->model[$modelid2]['tablename']; $datas = $this->db->select($where,'id,title'); foreach($datas as $_v) { $_v['sid'] = 'v'.$_v['id']; if(strtolower(CHARSET)=='gbk') { $_v['title'] = iconv('gbk', 'utf-8', $_v['title']); } $infos[] = $_v; } echo json_encode($infos); } }
4.打开phpcms\modules\content\templates\relationlist.tpl.php文件
第一处:<input type="hidden" value="public_relationlist" name="a">
下方增加(感谢下方评论中1楼的“田诚诚 ”指出该问题)
<input type="hidden" value="<?php echo($modelname)?>" name="modelname">
第二处:直接搜索<tr onclick="select_list(this
然后把onclick里面这个事件改为如下
select_list(this,'<?php echo safe_replace($r['title']);?>',<?php echo $r['id'];?>,'<?php echo $modelname;?>')
第三处:把下面的js中的select_list函数改成:
function select_list(obj, title, id, modelname) { var relation_ids = window.top.$('#' + modelname).val(); var sid = 'v<?php echo $modelid;?>' + id; if ($(obj).attr('class') == 'line_ff9966' || $(obj).attr('class') == null) { $(obj).attr('class', 'line_fbffe4'); window.top.$('#' + sid).remove(); if (relation_ids != '') { var r_arr = relation_ids.split('|'); var newrelation_ids = ''; $.each(r_arr, function (i, n) { if (n != id) { if (i == 0) { newrelation_ids = n; } else { newrelation_ids = newrelation_ids + '|' + n; } } }); window.top.$('#' + modelname).val(newrelation_ids); } } else { $(obj).attr('class', 'line_ff9966'); var oldIds = Array.from(window.top.$('#' + modelname + '_text li')).map(function (item) { return $(item).attr('id') }) if (oldIds.indexOf(sid) != -1) return; var str = "<li id='" + sid + "'><span>" + title + "</span><a href='javascript:;' class='close' onclick=\"remove_relation('" + sid + "'," + id + ",'<?php echo $modelname;?>')\"></a></li>"; window.top.$('#' + modelname + '_text').append(str); if (relation_ids == '') { window.top.$('#' + modelname).val(id); } else { relation_ids = relation_ids + '|' + id; window.top.$('#' + modelname).val(relation_ids); } } }
好了,大功告成!保存好后,新增文章选择刚才的模型,试试吧!
5.前台调用
{if $relation!=''} {php $rel = explode('|',$relation);} {loop $rel $picture_id} {pc:get sql="select * from v9_picture where id=$picture_id"} {loop $data $r} <li><a href="{$r[url]}" title="{$r[title]}"><img src="{$r[thumb]}" alt="{$r[title]}"/><span>{$r[title]}</span></a></li> {/loop} {/pc} {/loop} {/if}
v9_picture是目标模型的数据表
调用附表数据使用(有缺陷,暂未解决):
{pc:get sql="select * from v9_team,v9_team_data where $picture_id=v9_team.id=v9_team_data.id"}
评论(16)
后台显示可以了。,但是前台调用不能正常显示,能帮我看看吗?
{if $relation!=''}
{php $rel = explode('|',$relation);}
{loop $rel $picture_id}
{pc:get sql="select * from v9_designer,v9_designer_data where $picture_id=v9_designer.id=v9_designer_data.id"}
<dl>
{loop $data $r}
<a href="{$v[url]}" target="_blank"><dd style="background-image: url({$v[thumb]});" title="{$v[title]}"></dd></a>
{/loop}
</dl>
{/pc}
{/loop}
{/if}
v9_designer是我的模型主表
@viswzwpy:低级错误 改成这样就应该可以了 前提是 模型表的名字没错
改成这样
<a href="{$r[url]}" target="_blank"><dd style="background-image: url({$r[thumb]});" title="{$r[title]}"></dd></a>
或者改这里 {loop $data $v}
@viswzwpy:你好像半懂不懂的样子 where $picture_id=v9_designer.id=v9_designer_data.id 这个地方肯定有问题 加我QQ或者微信 109457371
在public_relationlist action下增加
<input type="hidden" value="public_relationlist" name="a">/*在下面增加*/
<input type="hidden" value="<?php echo($modelname)?>" name="modelname">
有朋自远方来...评论一下呗O(∩_∩)O