如何在ecshop实现文章随意调用功能

2016-07-07 15:01 来源:www.chinab4c.com 作者:ecshop专家

<?php

if(!function_exists("get_article_id")) {

function get_article_id($id, $num = 0) {

$wherestr = '';

$search = 'article_id=';


for( $i=0; $i<count($id); $i++ ) {

if( $i<count($id)-1 ) {

$wherestr = $wherestr . $search . $id[$i] . ' or ';

}

else {

$wherestr = $wherestr . $search . $id[$i];

}

}


$sql = 'SELECT * FROM ecs_article '.

' WHERE (' . $wherestr . ') AND ( is_open = 1 ) '.

' ORDER BY add_time DESC, article_type DESC, article_id DESC';

if ($num > 0) {

$sql .= ' LIMIT ' . $num;

}

$res = $GLOBALS['db']->getAll($sql);

$articles = array();

foreach ($res AS $id => $row) {

$articles[$id]['title'] = $row['title'];

$articles[$id]['url'] = 'article.php?id=' . $row['article_id'];

$articles[$id]['addtime'] = date($GLOBALS['_CFG']['date_format'], $row['add_time']);

}

return $articles;

}

}

$this->assign('company_msg',get_article_id(array(8,9))); // 这里指按ID号调用8,9号文章

?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<div class="right_frame_title">

<p>公司要文</p>

</div>

<div class="right_frame">

<!--{if $company_msg }-->

<ul>

<!--{foreach from=$company_msg item=cmsg}-->

<li><a href="{$cmsg.url}" title="{$cmsg.title|escape:html}" target="_blank">{$cmsg.title|truncate:16:"..."}</a></li>

<!--{/foreach}-->

</ul>

<!--{else}-->

<ul><li>暂无文章</li></ul>

<!--{/if}-->

</div>

<div><img src="../images/right_frame_bottom.gif" alt="" width="190" height="10" /></div>

从第4行到31行的代码是中心功能,主要用来实现按需调用文章,函数名:get_article_id()。但在这里将它一起写进了company_msg.lbi的库文件中,如果你觉得麻烦或者自己想在其他地方等又使用到该函数,那么可以直接将这函数写进EC 的includes/lib_arcicle.php文件末尾或其他需要使用的文件(但这种方式已经改了原代码了),为什么这么做相信有ecshop二次开发经验的都知道了这里不再多说。

33行的$this->assign('company_msg',get_article_id(array(8,9)));主要就是通过调用该函数来取得自己想要的文章。本函数包括两个参数 $id (数组型)和 $num (整型) ,$id 就是想要调用的文章ID,$num 是控制显示多少条默认的情况下就是全部显示。

在这个例子中是取ID号8和9的文章,当然也可以取8,6,120,11等自己想取的文章ID。

35-50行,这个不用多说了,其实也就是库文件的显示样式。懂HTML的话这个完全可以自己重新设计,我为了方便一起写出来了。

最后在想要显示的地方调用该库文件。例如在index.dwt 的左则等布局好的地方加入

<!-- #BeginLibraryItem "/library/company_msg.lbi" --><!-- #EndLibraryItem -->

就OK了。这样做的好处就是完全不用改动原文件,实现自己想要的功能。尽量避免升级等其他麻烦的地方。就算以后升级该库文件也直接使用就可以了


回答:
路过,看看。