广州传奇网络

地址:广州市天河区东圃大马路富华楼C座

电话:13808825895

邮箱:gz020wbs@163.com

QQ:1564443073

网址:http://www.020wbs.com/

首页 > 二次开发Ecshop二次开发 > ECSHOP首页调用不同分类文章

二次开发Ecshop二次开发

二次开发Ecshop二次开发

ECSHOP首页调用不同分类文章

一、新建立lib_getdata.php 内容如下:
<?php
if (!defined('IN_ECS'))
{
die('Hacking attempt');
}

/**
* ==================================
* 文章自定义数据调用函数
* ====================================
*/
//取得文章里面的图片
function GetImageSrc($body) {
if( !isset($body) ) {
return '';
}
else {
preg_match_all ("/<(img|IMG)(.*)(src|SRC)=[\"|'|]{0,}([h|\/].*(jpg|JPG|gif|GIF|png|PNG))[\"|'|\s]{0,}/isU",$body,$out);
return $out[4];
}
}

//提取文里面的URL
function GetArticleUrl($body) {
if( !isset($body) ) {
return '';
}
else {
preg_match_all("/<(a|A)(.*)(href|HREF)=[\"|'|](.*)[\"|'|\s]{0,}>(.*)<\/(a|A)>/isU",$body,$out);
return $out;
}
}

function get_article_children_new ($cat = 0)
{
return db_create_in(array_unique(array_merge(array($cat), array_keys(article_cat_list($cat, 0, false)))), 'a.cat_id');
}

/**
* 按文章ID号/分类ID/商品ID号/商品分类ID号取得文章
* @param array $id 文章ID或文章分类ID
* @param string $getwhat 以何种方式取文章其中可选参数有:
[1]art_cat(以文章分类ID获取) [2]art_id(以文章ID获取)
[3]goods_cat(以商品分类ID获取) [4]goods_id(以商品ID获取)
其中的[3]和[4]必须有商品关联文章或文章关联商品
* @param integer $num 控制显示多少条文章.当参数为0时则全部显示
* @param integer $start 从第几条数据开始取
* @param boolean $isrand 是否随机显示文章(默认为不显示)
* @param boolean $showall 是否显示隐藏的文章(黑认为不显示隐藏文章)
* @return array
*/
function get_article_new( $id = array(0), $getwhat = 'art_id', $num = 0, $isrand = false, $showall = false, $start = 0 ) {
$sql = '';
$findkey = '';
$search = '';
$wherestr = '';

for( $i=0; $i<count($id); $i++ ) {
if( $i<count($id)-1 ) {
$findkey .= $id[$i] .',';
}
else {
$findkey .= $id[$i];
}
}

if( $getwhat == 'art_cat' ){
for( $i=0; $i<count($id); $i++ ) {
if( $i<count($id)-1 ) {
$search .= get_article_children_new($id[$i]) . ' OR ';
}
else {
$search .= get_article_children_new($id[$i]);
}
}
}
elseif($getwhat == 'goods_cat') {
for( $i=0; $i<count($id); $i++) {
if( $i<count($id)-1 ) {
$search .= get_children($id[$i]) . ' OR ';
}
else {
$search .= get_children($id[$i]);
}
}
}
elseif( $getwhat == 'art_id' ) {
$search = 'a.article_id IN' . '(' . $findkey . ')';
}
elseif( $getwhat == 'goods_id' ) {
$search = 'g.goods_id IN' . '(' . $findkey . ')';
}
$wherestr = '(' . $search . ')';

if( $getwhat == 'art_cat' || $getwhat == 'art_id' ) {
$sql = 'SELECT a.*,ac.cat_id,ac.cat_name,ac.keywords as cat_keywords,ac.cat_desc
FROM ' . $GLOBALS['ecs']->table('article') . ' AS a, ' .
$GLOBALS['ecs']->table('article_cat') . ' AS ac' .
' WHERE (a.cat_id = ac.cat_id) AND ' . $wherestr;
}
elseif( $getwhat == 'goods_cat' || $getwhat == 'goods_id' ) {
$sql = 'SELECT DISTINCT a.*,ac.cat_id,ac.cat_name,ac.keywords as cat_keywords,ac.cat_desc FROM ' .
$GLOBALS['ecs']->table('goods') .' AS g ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('goods_article') . ' AS ga ON g.goods_id=ga.goods_id INNER JOIN ' .
$GLOBALS['ecs']->table('article') . ' AS a ON ga.article_id = a.article_id, ' .
$GLOBALS['ecs']->table('article_cat') . 'AS ac ' .
'WHERE (a.cat_id = ac.cat_id) AND ' . $wherestr;
}


if( ($showall == false) && ( $getwhat == 'art_cat' || $getwhat == 'art_id' ) ) {
$sql .= ' AND a.is_open=1';
}
elseif( ($showall == false) && ( $getwhat == 'goods_cat' || $getwhat == 'goods_id' ) ) {
$sql .= ' AND a.is_open=1';
}

if( $isrand == true ) {

$sql .= ' ORDER BY rand()';
}
elseif( ($isrand == false) && ( $getwhat == 'art_cat' || $getwhat == 'art_id' ) ) {
$sql .= ' ORDER BY a.add_time DESC, a.article_id DESC';
}
elseif( ($isrand == false) && ( $getwhat == 'goods_cat' || $getwhat == 'goods_id' ) ) {
$sql .= ' ORDER BY a.add_time DESC, a.article_id DESC';
}

if( $start == 0 && $num>0 ) {
$sql .= ' LIMIT ' . $num;
}
elseif( $start>0 && $num>0 ) {
$sql .= ' LIMIT ' . $start . ',' . $num;
}

//开始查询
$arr = $GLOBALS['db']->getAll($sql);
$articles = array();
foreach ($arr AS $id => $row) {
$articles[$id]['cat_id'] = $row['cat_id'];
$articles[$id]['cat_name'] = $row['cat_name'];
$articles[$id]['cat_url'] = build_uri('article_cat', array('acid' => $row['cat_id']));
$articles[$id]['cat_keywords'] = $row['cat_keywords'];
$articles[$id]['cat_desc'] = $row['cat_desc'];
$articles[$id]['title'] = $row['title'];
$articles[$id]['url'] = build_uri('article', array('aid'=>$row['article_id']), $row['title']);
$articles[$id]['author'] = $row['author'];
$articles[$id]['content'] = $row['content'];
$articles[$id]['keywords'] = $row['keywords'];
$articles[$id]['file_url'] = $row['file_url'];
$articles[$id]['link'] = $row['link'];
$articles[$id]['addtime'] = date($GLOBALS['_CFG']['date_format'], $row['add_time']);
$articles[$id]['content'] = $row['content'];
$imgsrc = GetImageSrc($row['content']);
$articles[$id]['img'] = $imgsrc; //提取文章中所有的图片
$link = GetArticleUrl($row['content']);
$articles[$id]['link_url'] = $link[4]; //提取文章中所有的链接地址
$articles[$id]['link_title'] = $link[5]; //提取文章中所有的链接名称
}
return $articles;
}
?>


二、将lib_getdata.php文件放入你的网站目录/includes 文件夹下面.

三、用DW等相关软件打开 你的网站目录/includes/init.php 文件,并找到这一行:
require(ROOT_PATH . 'includes/lib_article.php');

在该行下面增加以下代码:
require(ROOT_PATH . 'includes/lib_getdata.php');


四、8点说明
①希望新老手都可以认真看完本函数的参数使用说明和本贴里面的例子。 /**

* 按文章ID号/分类ID/商品ID号/商品分类ID号取得文章

* @param array $id 文章ID或文章分类ID

* @param string $getwhat 以何种方式取文章其中可选参数有:

[1]art_cat(以文章分类ID获取) [2]art_id(以文章ID获取)

[3]goods_cat(以商品分类ID获取) [4]goods_id(以商品ID获取)

其中的[3]和[4]必须有商品关联文章或文章关联商品

* @param integer $num 控制显示多少条文章.当参数为0时则全部显示

* @param integer $start 从第几条数据开始取

* @param boolean $isrand 是否随机显示文章(默认为不显示)

* @param boolean $showall 是否显示隐藏的文章(黑认为不显示隐藏文章)

* @return array

*

* 函数原型:get_article_new( $id = array(0), $getwhat = 'art_id', $num = 0, $isrand = false, $showall = false, $start = 0 )

*/
②该函数的第一个参数即ID参数,是数组型并不只是调用一个的,例如可以array(5,12,7,21,9)
③我的例子里面库代码是UTF-8的.你可以使用DW等软件变成GBK编码的,但不要使用记事本等.当然这里也要改成<meta http-equiv="Content-Type" content="text/html; charset=GBK">
④我提供的显示库例子,只是跟据我的样例网站布局而来的,请参照上面的方法而不是完全COPY!
⑤函数本身经过各种测试没有问题,请注意自己在调用函数时候的参数和模板代码.
⑥推荐在你的库中先不使用任何模板代码,而先使用print_r()检查调用出来的结果是否正确.例如:
<?php 

$test = get_article_new(array(2),'art_cat',6);

print_r($test);

?>
⑦请在贴出错误的时候不要只贴错误内容和行号.请一起贴出包括本行的部份代码.
⑧自己定义广告轮换,我只提供的是方法.如果直接使用我上面的例子,是肯定不行的.网上有很多FLASH的广告轮换.请自己跟据函数返回的结果来调用.但如果你不了解PHP\JS\XML等...则请略过~


五、现在开始利用本函数制作左图右列文章展示

在模板目录的库文件目录中增加:msg_img.lbi库文件
代码如下:

<?php

$this->assign( 'artimg1', get_article_new(array(2),'art_cat',6,false) );

$this->assign( 'artimg2', get_article_new(array(1),'goods_cat',6,false,false) );

?>

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

<style type="text/css">

<!--

.msg_img{background: #EDEDED;width: 546px; height:150px; text-align: left;margin: 10px 0 0 0; padding-top:10px;}

.msg_img h2{ background-color:#3D3D3D;width: 516px; height: 25px;color: #fff; line-height: 25px; text-indent: 20px; font-size: 12px;}

.msg_img_cont{padding:5px;text-align:left;}

.cont_left { width:100px; height:110px; overflow:hidden; padding:2px; text-align:center; float:left;}

.cont_left img{width:100px; height:100px; border: #999 solid 1px;}

.cont_right{ width:150px; height:110px; overflow:hidden; text-align:left; float:left; padding:0px 10px 0px 0px;}

.cont_right ul{list-style: none; padding:5px 0px 0px 10px;}

.cont_right ul li{ padding-bottom:5px;}

-->

</style>

<div class="msg_img">

<h2>最新动态</h2>

<div class="msg_img_cont">

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

<div class="cont_left"><!--显示文章图片-->

<!--{foreach from=$artimg1 item=img1 key=index1}-->

{if $index1 eq 0}

{if $img1.img.0 neq ''}

<a href="{$img1.url}" target="_blank"><img src="{$img1.img.0}" alt="{$img1.title|escape:html}" /></a>

{else}

暂无图片

{/if}

{/if}

<!--{/foreach}-->

</div><!--/显示文章图片-->

<div class="cont_right"><!--显示文章列表-->

<ul>

<!--{foreach from=$artimg1 item=art1 key=index1}-->

{if $index1 neq 0}

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

{/if}

<!--{/foreach}-->

</ul>

</div><!--/显示文章列表-->

<!-- {else} -->

<div class="cont_left">暂无图片</div>

<div class="cont_right">暂无文章</div>

<!-- {/if} -->

 

 

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

<div class="cont_left"><!--显示文章图片2-->

<!--{foreach from=$artimg2 item=img2 key=index2}-->

{if $index2 eq 0}

{if $img2.img.0 neq ''}

<a href="{$img2.url}" target="_blank"><img src="{$img2.img.0}" alt="{$img2.title|escape:html}" /></a>

{else}

暂无图片

{/if}

{/if}

<!--{/foreach}-->

</div><!--/显示文章图片-->

<div class="cont_right"><!--显示文章列表2-->

<ul>

<!--{foreach from=$artimg2 item=art2 key=index2}-->

{if $index2 neq 0}

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

{/if}

<!--{/foreach}-->

</ul>

</div><!--/显示文章列表-->

<!-- {else} -->

<div class="cont_left">暂无图片</div>

<div class="cont_right">暂无文章</div>

<!-- {/if} -->

</div>

</div>

 

六、补充说明
但要提一下的就是在本次新库中的第4行即:$this->assign( 'artimg2', get_article_new(array(1),'goods_cat',6,false,false) );
这里的参数使用了goods_cat,即以商品分类ID号为1 调用文章.但须要注意的就是如果你在实际使用中,该分类下的商品没有关联文章或者文章没有关联该分类的商品,就不会有数据出来!

还有就是所有的抽取出的内容图不再以默认的第一张了.完全可以自己定义要显示内容的第几张,当然前提是你的文章内容必须要这么多的图.

{if $img1.img.0 neq ''}

<a href="{$img1.url}" target="_blank"><img src="{$img1.img.0}" alt="{$img1.title|escape:html}" /></a>
以上代码默认情况下$img1.img.0就是取内容中的第一张图,以此类推

(1)要更新库里面调用的函数参数.$this->assign( 'artimg1', get_article_new(array(2),'art_cat',6,false) );
如果你以前是用cat参数,则本次更新为:art_cat,如果你以前是没用使用参数直接取文章ID,那么本次更新为art_id.
以前该函数的第二个参数是cat或者随意写但这次更新的函数固定了四种,具体可以参看get_article_new()的函数说明,在第一楼的代码24行有说明.

(2)以前库文件里调用涉及图片的模板代码要修改
以前的代码:

<a href="{$aimg1.url}" target="_blank"><img src="{$aimg1.img}" alt="{$aimg1.title|escape:html}" /></a>
现在必须要改成:<a href="{$aimg1.url}" target="_blank"><img src="{$aimg1.img.0}" alt="{$aimg1.title|escape:html}" /></a>
即抽取出的图片成为了数组的方式.如果你想调用文章内容里面的第二张图则可以写成