phpcms 移植【添加相關文章】功能

添加相關文章功能相當有用,移植一個過來基本上可以實現比較複雜的頁面內包含分類功能,做二次開發時可以省下不少力氣。

用例:如果一個產品,屬於一個廠家,而這個廠家是動態添加的,既不是一個分類,而是一個廠家的模型,這二者關聯的時候使用這個添加相關的功能可以輕易實現。

 

學會使用phpcms中的類別管理和推薦位管理基本上可以滿足文章管理的多數場景,如果能夠理解mysql表的設計,可以使用模型管理這個大殺器,

基本上能夠想到的功能都能實現,最後再對【添加相關文章】功能進行設計和移植,模型功能能夠再次爆發威力,進行各種關係映射。前臺配合ajax和json,以及數據表的query函數可以整合實現相當複雜的功能和頁面。

 

 

1,模型管理中添加一個萬能字段。

然後在表單欄中填寫:

<input type='hidden' name='info[rt]' id='rt' value='{FIELD_VALUE}' style='50' >
<ul class="list-dot" id="rt_text"></ul>
<div>
<input type='button' value="添加相關" onclick="omnipotent('selectid','?m=content&c=content&a=public_rtlist&modelid=29','添加相關文章',1)" class="button" style="width:66px;">
<span class="edit_content">
<input type='button' value="顯示已有" onclick="show_rt({MODELID},{ID})" class="button" style="width:66px;">
</span>
</div>

注意上面的標紅字體,需要和相關參數關聯

 

2,在文章修改頁面添加如下函數

function show_rt(modelid,id) {
$.getJSON("?<?php echo "token=".$_SESSION['token'];?>&m=content&c=content&a=rt_getjson_ids&modelid="+modelid+"&id="+id, function(json){
var newrt_ids = '';
if(json==null) {
alert('沒有添加相關文章');
return false;
}

$.each(json, function(i, n){
newrt_ids += "<li id='"+n.sid+"'>·<span>"+n.title+"</span><a href='javascript:;' class='close' onclick=\"remove_rt('"+n.sid+"',"+n.id+")\"></a></li>";
});

$('#rt_text').html(newrt_ids);
});
}

//移除相關文章
function remove_rt(sid,id) {
var rt_ids = $('#rt').val();
if(rt_ids !='' ) {
$('#'+sid).remove();
var r_arr = rt_ids.split('|');
var newrt_ids = '';
$.each(r_arr, function(i, n){
if(n!=id) {
if(i==0) {
newrt_ids = n;
} else {
newrt_ids = newrt_ids+'|'+n;
}
}
});
$('#rt').val(newrt_ids);
}
}

 

3,在content.php中填寫如下函數

public function public_rtlist() {
ec_main::get_lib_c('format','',0);
$show_header = '';
$model_cache = getcache('model','commons');
if(!isset($_GET['modelid'])) {
showmessage(L('please_select_modelid'));
} else {
$page = intval($_GET['page']);

$modelid = intval($_GET['modelid']);
$this->db->set_model($modelid);
$where = '';
if($_GET['catid']) {
$catid = intval($_GET['catid']);
$where .= "catid='$catid'";
}
$where .= $where ? ' AND status=99' : 'status=99';

if(isset($_GET['keywords'])) {
$keywords = trim($_GET['keywords']);
$field = $_GET['field'];
if(in_array($field, array('id','title','keywords','description'))) {
if($field=='id') {
$where .= " AND `id` ='$keywords'";
} else {
$where .= " AND `$field` like '%$keywords%'";
}
}
}
$infos = $this->db->listinfo($where,'',$page,12);
$pages = $this->db->pages;
include $this->admin_tpl('rtlist');
}
}



public function rt_getjson_ids() {
$modelid = intval($_GET['modelid']);
$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),'rt');

if($r['rt']) {
$rt = str_replace('|', ',', $r['rt']);
$rt = trim($rt,',');
$where = "id IN($rt)";
$infos = array();
$this->db->table_name = 'ec_chang';
$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);

}

}

5增加模板rtlist.tpl.php[content/tpl]

<?php
defined('IN_ADMIN') or exit('No permission resources.');
include $this->admin_tpl('header','admin');
?>
<div class="pad-10">
<form name="searchform" action="" method="get" >
<input type="hidden" value="content" name="m">
<input type="hidden" value="content" name="c">
<input type="hidden" value="public_rtlist" name="a">
<input type="hidden" value="<?php echo $modelid;?>" name="modelid">
<table width="100%" cellspacing="0" class="search-form">
    <tbody>
		<tr>
		<td align="center">
		<div class="explain-col">
				<select name="field">
					<option value='title' <?php if($_GET['field']=='title') echo 'selected';?>><?php echo L('title');?></option>
					<option value='keywords' <?php if($_GET['field']=='keywords') echo 'selected';?> ><?php echo L('keywords');?></option>
					<option value='description' <?php if($_GET['field']=='description') echo 'selected';?>><?php echo L('description');?></option>
					<option value='id' <?php if($_GET['field']=='id') echo 'selected';?>>ID</option>
				</select>
				<?php echo form::select_category('',$catid,'name="catid"',L('please_select_category'),$modelid,0,1);?>
				<input name="keywords" type="text" value="<?php echo stripslashes($_GET['keywords'])?>" style="width:330px;" class="input-text" />
				<input type="submit" name="dosubmit" class="button" value="<?php echo L('search');?>" />
	</div>
		</td>
		</tr>
    </tbody>
</table>
</form>
<div class="table-list">
    <table width="100%" cellspacing="0" >
        <thead>
            <tr>
            <th ><?php echo L('title');?></th>
			<th width="100"><?php echo L('belong_category');?></th>
            <th width="100"><?php echo L('addtime');?></th>
            </tr>
        </thead>
    <tbody>
	<?php foreach($infos as $r) { ?>
	<tr onclick="select_list(this,'<?php echo safe_replace($r['title']);?>',<?php echo $r['id'];?>)" 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 }?>
	    </tbody>
    </table>
   <div id="pages"><?php echo $pages;?></div>
</div>
</div>
<style type="text/css">
 .line_ff9966,.line_ff9966:hover td{
	background-color:#FF9966;
}
 .line_fbffe4,.line_fbffe4:hover td {
	background-color:#fbffe4;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
	function select_list(obj,title,id) {
		var rt_ids = window.top.$('#rt').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(rt_ids !='' ) {
				var r_arr = rt_ids.split('|');
				var newrt_ids = '';
				$.each(r_arr, function(i, n){
					if(n!=id) {
						if(i==0) {
							newrt_ids = n;
						} else {
						 newrt_ids = newrt_ids+'|'+n;
						}
					}
				});
				window.top.$('#rt').val(newrt_ids);
			}
		} else {
			$(obj).attr('class','line_ff9966');
			var str = "<li id='"+sid+"'>·<span>"+title+"</span><a href='javascript:;' class='close' onclick=\"remove_rt('"+sid+"',"+id+")\"></a></li>";
			window.top.$('#rt_text').append(str);
			if(rt_ids =='' ) {
				window.top.$('#rt').val(id);
			} else {
				rt_ids = rt_ids+'|'+id;
				window.top.$('#rt').val(rt_ids);
			}
		}
}
//-->
</SCRIPT>
</body>
</html>

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章