码迷,mamicode.com
首页 > Web开发 > 详细

PHPCMS V9 relation 后台添加文章 选择“相关文章” 可调用其它模型文章 的解决办法

时间:2018-06-02 16:33:09      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:解决办法   for   ref   value   TE   lower   each   ons   cli   

问题:在添加文章时候选择相关文章只能是本模型下的栏目文章,如果想选用其他模型的文章该如何做?

思路:添加一个自己的相关文章字段

实现:

 1.修改系统默认的relation字段表单代码如下:

1 <input type=‘hidden‘ name=‘info[你的字段名]‘ id=‘你的字段名‘ value=‘{FIELD_VALUE}‘ style=‘50‘ >
2 <ul class="list-dot" id="你的字段名_text"></ul>
3 <div>
4 <input type=‘button‘ value="添加相关" onclick="omnipotent(‘selectid‘,‘?m=content&c=content&a=public_relationlist&modelid=目标模型ID&modelname=你的字段名‘,‘添加相关文章‘,1)" class="button" style="width:66px;">
5 <span class="edit_content">
6 <input type=‘button‘ value="显示已有" onclick="show_myrelation(当前模型ID,目标模型ID,{ID},‘你的字段名‘)" class="button" style="width:66px;">
7 </span>
8 </div>

 2.打开网站根目录文件statics\js\content_addtop.js

 修改remove_relation方法为

 1 function remove_relation(sid, id, modelname) {
 2     var relation_ids = $(‘#‘ + modelname).val();
 3     if (relation_ids != ‘‘) {
 4         $(‘#‘ + sid).remove();
 5         var r_arr = relation_ids.split(‘|‘);
 6         var newrelation_ids = ‘‘;
 7         $.each(r_arr, function(i, n) {
 8             if (n != id) {
 9                 if (i == 0) {
10                     newrelation_ids = n;
11                 } else {
12                     newrelation_ids = newrelation_ids + ‘|‘ + n;
13                 }
14             }
15         });
16         $(‘#‘ + modelname).val(newrelation_ids);
17     }
18 }

修改show_relation函数为:

 1 function show_relation(modelid, id, fieldname) {
 2     $.getJSON("?m=content&c=content&a=public_getjson_ids&modelid=" + modelid + "&id=" + id, function(json) {
 3         var newrelation_ids = ‘‘;
 4         if (json == null) {
 5             alert(‘没有添加相关文章‘);
 6             return false;
 7         }
 8         $.each(json, function(i, n) {
 9             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>";
10         });
11         $(‘#relation_text‘).html(newrelation_ids);
12     });
13 }

新增show_myrelation方法:

 1 function show_myrelation(modelid, modelid2, id, fieldname) {
 2     $.getJSON("?m=content&c=content&a=public_getjson_ids2&modelid=" + modelid + "&modelid2=" + modelid2 + "&id=" + id + "&fieldname=" + fieldname, function(json) {
 3         var newrelation_ids = ‘‘;
 4         if (json == null) {
 5             alert(‘没有添加相关文章‘);
 6             return false;
 7         }
 8         $.each(json, function(i, n) {
 9             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>";
10         });
11         $(‘#‘ + fieldname + ‘_text‘).html(newrelation_ids);
12     });
13 }

3.打开phpcms\modules\content\content.php文件

修改public_relationlist方法为

 1 public function public_relationlist() {
 2         $modelname=$_GET[‘modelname‘];
 3         pc_base::load_sys_class(‘format‘,‘‘,0);
 4         $show_header = ‘‘;
 5         $model_cache = getcache(‘model‘,‘commons‘);
 6         if(!isset($_GET[‘modelid‘])) {
 7             showmessage(L(‘please_select_modelid‘));
 8         } else {
 9             $page = intval($_GET[‘page‘]);
10             
11             $modelid = intval($_GET[‘modelid‘]);
12             $this->db->set_model($modelid);
13             $where = ‘‘;
14             if($_GET[‘catid‘]) {
15                 $catid = intval($_GET[‘catid‘]);
16                 $where .= "catid=‘$catid‘";
17             }
18             $where .= $where ?  ‘ AND status=99‘ : ‘status=99‘;
19             
20             if(isset($_GET[‘keywords‘])) {
21                 $keywords = trim($_GET[‘keywords‘]);
22                 $field = $_GET[‘field‘];
23                 if(in_array($field, array(‘id‘,‘title‘,‘keywords‘,‘description‘))) {
24                     if($field==‘id‘) {
25                         $where .= " AND `id` =‘$keywords‘";
26                     } else {
27                         $where .= " AND `$field` like ‘%$keywords%‘";
28                     }
29                 }
30             }
31 
32             $infos = $this->db->listinfo($where,‘‘,$page,12);
33             $pages = $this->db->pages;
34             include $this->admin_tpl(‘relationlist‘);
35         }
36     }

新增方法public_getjson_ids2: 

 1 public function public_getjson_ids2() {
 2     $modelid = intval($_GET[‘modelid‘]);
 3     $modelid2 = intval($_GET[‘modelid2‘]);
 4     $fieldname = $_GET[‘fieldname‘];
 5     $id = intval($_GET[‘id‘]); 
 6     $this->db->set_model($modelid); 
 7     $tablename = $this->db->table_name; 
 8     $this->db->table_name = $tablename.‘_data‘; 
 9     $r = $this->db->get_one(array(‘id‘=>$id),$fieldname); 
10     if($r["{$fieldname}"]) { $myrelation = str_replace(‘|‘, ‘,‘, $r["{$fieldname}"]); 
11     $myrelation = trim($myrelation,‘,‘); $where = "id IN($myrelation)"; $infos = array();
12     $this->db->set_model($modelid2);
13     $this->model = getcache(‘model‘, ‘commons‘);
14     $this->db->table_name = $this->db->db_tablepre.$this->model[$modelid2][‘tablename‘];
15     $datas = $this->db->select($where,‘id,title‘);
16     foreach($datas as $_v) { $_v[‘sid‘] = ‘v‘.$_v[‘id‘];
17     if(strtolower(CHARSET)==‘gbk‘) $_v[‘title‘] = iconv(‘gbk‘, ‘utf-8‘, $_v[‘title‘]); $infos[] = $_v;
18     }
19     echo json_encode($infos);
20     }
21 }

4.打开phpcms\modules\content\templates\relationlist.tpl.php文件,在public_relationlist action下增加一个参数传递用于修复搜索后不能添加的问题

<input type="hidden" value="public_relationlist" name="a">/*在下面增加*/
<input type="hidden" value="<?php echo($modelname)?>" name="modelname">

后修改

<?php foreach($infos as $r) {?>
<tr onclick="select_list(this,‘<?php echo safe_replace($r[‘title‘]);?>‘,<?php echo $r[‘id‘];?>,‘<?php echo $Mname;?>‘)" class="cu" title="<?php echo L(‘click_to_select‘);?>">
<td align=‘left‘ ><?php echo $r[‘title‘];?></td>
<td align=‘center‘><?php echo $this->categorys[$r[‘catid‘]][‘catname‘];?></td>
<td align=‘center‘><?php echo format::date($r[‘inputtime‘]);?></td>
</tr>
<?php }?>

以及最后的JavaScript

 1 <SCRIPT LANGUAGE = "JavaScript" >
 2     < !--
 3         function select_list(obj, title, id, modelname) {
 4             var relation_ids = window.top.$(‘#‘ + modelname).val();
 5             var sid = ‘v<?php echo $modelid;?>‘ + id;
 6             if ($(obj).attr(‘class‘) == ‘line_ff9966‘ || $(obj).attr(‘class‘) == null) {
 7                 $(obj).attr(‘class‘, ‘line_fbffe4‘);
 8                 window.top.$(‘#‘ + sid).remove();
 9                 if (relation_ids != ‘‘) {
10                     var r_arr = relation_ids.split(‘|‘);
11                     var newrelation_ids = ‘‘;
12                     $.each(r_arr, function (i, n) {
13                         if (n != id) {
14                             if (i == 0) {
15                                 newrelation_ids = n;
16                             } else {
17                                 newrelation_ids = newrelation_ids + ‘|‘ + n;
18                             }
19                         }
20                     });
21                     window.top.$(‘#‘ + modelname).val(newrelation_ids);
22                 }
23             } else {
24                 $(obj).attr(‘class‘, ‘line_ff9966‘);
25                 var str = "<li id=‘" + sid + "‘><span>" + title + "</span><a href=‘javascript:;‘ class=‘close‘ onclick=\"remove_relation(‘" + sid + "‘," + id + ",‘<?php echo $modelname;?>‘)\"></a></li>";
26                 window.top.$(‘#‘ + modelname + ‘_text‘).append(str);
27                 if (relation_ids == ‘‘) {
28                     window.top.$(‘#‘ + modelname).val(id);
29                 } else {
30                     relation_ids = relation_ids + ‘|‘ + id;
31                     window.top.$(‘#‘ + modelname).val(relation_ids);
32                 }
33             }
34         }
35     //-->
36     < /SCRIPT>

功能修改完毕

 5.前台调用

添加了相关文章后

 1 <div class="card-body">
 2                 {if $relationC!=‘‘}
 3                 {php $rel = explode(‘|‘,$relationC);}
 4                 {loop $rel $picture_id}
 5                 {pc:get sql="select * from lvv9_news where id=$picture_id"} 
 6                 {loop $data $r}
 7                 <div>
 8                         <a href="{$r[url]}" title="{$r[alt]}"><img class="img-fluid" src="{thumb($r[thumb],200,150,0)}" alt="{$r[title]}"/></a>
 9                         <p><a href="{$r[url]}" title="{$r[title]}">{$r[title]}</a></p>
10                 </div>
11 
12                 {/loop}
13                 {/pc}
14                 {/loop}
15                 {/if}
16             </div>

其中亮黄的部分为我的字段名称和数据库文章模型的表名,替换即可!

6.感谢

感谢https://www.eqifei.net/post-268.html#comments 这个地址的文章 

 


 

PHPCMS V9 relation 后台添加文章 选择“相关文章” 可调用其它模型文章 的解决办法

标签:解决办法   for   ref   value   TE   lower   each   ons   cli   

原文地址:https://www.cnblogs.com/tianchengcheng/p/9125661.html

(2)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!