ecshop數據調用分好幾種,第一種是js調用,利用后臺生成js代碼調用相應的產品.
js調用的好處就是方便實用,一句話就可以完成調用,而且可以跨站調用,有利于推廣.
但壞處也是多的不能夠在主站采用.
js調用一個是數據庫讀取問題,php在緩存技術上是相當成熟的,但是js調用就沒有這個功能,所以每次調用都要讀取數據庫,如果調用多了,那可能造成網站很卡.
還有一個壞處就是js調用不利于seo.
簡單來講就是用js調用后,查看源代碼,看到的只是一句調用代碼,而不是產品名稱啊,價格啊,等.所以.如果在主站seo優化上,js調用是完全可以拋棄的.
所以,我們要尋找一種,利于seo,而且能夠實現js調用這些功能的方法.
ecshop給我們提供了$smarty
所以我們可以做一個聲明,然后用$smarty來調用這些數據.
例如網上已經公布的文章調用方法
/**
* 獲得指定欄目最新的文章列表。
*
* @access private
* @return array
*/
function index_get_class_articles($cat_aid, $cat_num)
{
$sql = "Select article_id, title,open_type,cat_id,file_url FROM " .$GLOBALS['ecs']->table('article'). " Where cat_id = ".$cat_aid." and is_open = 1 LIMIT " . $cat_num;
$res = $GLOBALS['db']->getAll($sql);
$arr = array();
foreach ($res AS $idx => $row)
{
$arr[$idx]['id'] = $row['article_id'];
$arr[$idx]['title'] = $row['title'];
$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?
sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
$arr[$idx]['cat_name'] = $row['cat_name'];
$arr[$idx]['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
$arr[$idx]['url'] = $row['open_type'] != 1 ?
build_uri('article', array('aid' => $row['article_id']), $row['title']) : trim($row['file_url']);
$arr[$idx]['cat_url'] = build_uri('article_cat', array('acid' => $row['cat_id']));
}
return $arr;
}
聲明后再在需要調用的php內寫入
//調用方法
$smarty->assign('class_articles_4', index_get_class_articles(4,6)); // 分類調用文章
//調用多個就修改傳進去的參數,以及模板接收的變量,其中上面的4就是文章分類ID,其中6是調用數量
這樣就可以在模板dwt文件里開始調用了.
<!--{foreach from=$class_articles_8 item=article}-->
<li><a href="{$article.url}" title="{$article.title|escape:html}"><!--{$article.short_title|truncate:15:true}--></a></li>
<!--{/foreach}-->
這樣就可以展示出來了.
------------------------------------
同樣產品調用也是這個思路.這里我們調用指定分類下精品產品列表.
首先聲明
/**
* 獲得cat_id精品列表。
*
* @access private
* @param integer
* @return array
*/
function index_get_cat_id_goods_best_list($cat_id, $num)
{
$sql = 'Select g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price, g.promote_price, ' .
"promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, " .
"g.is_best, g.is_new, g.is_hot, g.is_promote " .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
"Where g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.is_best = 1 AND g.cat_id = '$cat_id'".
" LIMIT $num";
$res = $GLOBALS['db']->getAll($sql);
$goods = array();
foreach ($res AS $idx => $row)
{
$goods[$idx]['id'] = $row['article_id'];
$goods[$idx]['id'] = $row['goods_id'];
$goods[$idx]['name'] = $row['goods_name'];
$goods[$idx]['brief'] = $row['goods_brief'];
$goods[$idx]['brand_name'] = $row['brand_name'];
$goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
$goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
$goods[$idx]['market_price'] = price_format($row['market_price']);
$goods[$idx]['shop_price'] = price_format($row['shop_price']);
$goods[$idx]['thumb'] = empty($row['goods_thumb']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_thumb'];
$goods[$idx]['goods_img'] = empty($row['goods_img']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_img'];
$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
}
return $goods;
}
聲明后用$smarty調用
$smarty->assign('cat_id2_best_goods', index_get_cat_id_goods_best_list(2,4));
//2指分類id,4指循環次數
然后就可以在dwt模板文件里開始調用了
具體調用方法有兩種,因為產品調用要是先js調用的效果的話,就是有H和V兩種,圖片價格和文字標題兩種.
如果是橫向調用4個產品,帶圖片價格等.可以這樣寫
<!--{foreach from=$cat_id2_best_goods item=goods}-->
<div class="goodsItem" style="float:left ; margin-left:10px;" >
<a href="{$goods.url}"><img src="{$goods.thumb}" alt="{$goods.name|escape:html}" class="goodsimg" width="100" height="100" /></a><br />
<p><a href="{$goods.url}" title="{$goods.name|escape:html}">{$goods.short_name|truncate:7:true}</a></p>
<div class="shop_s" style="text-align:center; color:#CC0000 " >
<strong>
{$goods.shop_price}
</strong>
</div>
</div>
<!--{/foreach}-->
如果需要調用縱向標題列表的話只需要把item=goods修改成item=list,然后刪掉不需要的調用代碼即可
<!--{foreach from=$cat_id11_best_goods item=list}-->
<li>
<a href="{$list.url}" title="{$list.name|escape:html}">{$list.name|truncate:15:true}</a>
</li>
<!--{/foreach}-->
其實橫向縱向的排列方式在模板文件里修改也可以,但方法都一樣,就是在css里修改.加個float:left即可.
以上就是本文章的內容,希望對大家有所幫助
新聞熱點
疑難解答