ecshop 添加自定义动态库项目详解
来源:互联网 发布:国家税务总局网络培训 编辑:程序博客网 时间:2024/06/08 06:37
对于如何添加静态库项目,这个有很多人写过,我用的是ecshop 2.7.2版,这里不多说了,我这里要讲的是如何 添加自定义动态库项目.
实现在后台 模板设置页里能看到最下边红色框内多出的 动态项目库
需要修改的地方有:(csdn的源码贴出挺麻烦的, 不知道怎么用不同颜色标出,只好在添加修改的地方用 后加的字样来标出了)
1、编制好你需要添加的动态库项目文件,我在这里添加的是 cat_promotion.lbi ,放置的位置是所在模版(我这里用的模版名称是 “redbaby ” ) 的 library目录中,对应完整目录是: \themes\redbaby\library ;
我这里的该库文件内容代码如下:(注意下调用的列表对象变量这里是 $cat_promotion)
<div class="index_hotOuter"><div class="index_hotTitle">精选热销</div><div class="index_hotBody"><!--{foreach from=$cat_promotion item=goods name="promotion_foreach"}--> <div class="index_hotBodyBox" ><div class="hotPic"><a href="{$goods.url}"><img src="{$goods.thumb}" border="0" alt="{$goods.name|escape:html}" /></a></div><div class="hotName"><a href="{$goods.url}" title="{$goods.name|escape:html}">{$goods.short_name|escape:html}</a></div><div class="hotInfo"><span class="price02" style="color:#CC0000">{$goods.promote_price}</span></div> </div> <!--{/foreach}--></div> <div class="index_hotMore"><a href="brand.php" target=_blank></a></div></div>
2 、添加语言项
找到 \languages\zh_cn\admin\template.php
打开后,找到这个位置
在最后添加
$_LANG['template_libs']['cat_promotion'] = '分类促销商品';
这样完成了语言项添加。
3、修改 \admin\includes\lib_template.php
/* 动态库项目 */$dyna_libs = array( 'cat_goods', 'brand_goods', 'suppliers_goods', 'cat_articles', 'ad_position', //我这里加上下边的预定义的动态库项目文件名
'cat_promotion');
4、上边都做完了,然后做什么呢,关键的地方到了,我们找到前台显示页面的主php文件中,这里找到的 \index.php
里面有:
/* 页面中的动态内容 */ assign_dynamic('index');
这里就是在前台页面中加载动态项目库的函数了,这个函数在哪里呢?
在这个地方: \includes\lib_main.php
找到这个函数,做修改,加入你自己的东东:
/** * 获得指定页面的动态内容 * * @access public * @param string $tmp 模板名称 * @return void */function assign_dynamic($tmp){ $sql = 'SELECT id, number, type FROM ' . $GLOBALS['ecs']->table('template') . " WHERE filename = '$tmp' AND type > 0 AND remarks ='' AND theme='" . $GLOBALS['_CFG']['template'] . "'"; $res = $GLOBALS['db']->getAll($sql); foreach ($res AS $row) { switch ($row['type']) { case 1: /* 分类下的商品 */ $GLOBALS['smarty']->assign('goods_cat_' . $row['id'], assign_cat_goods($row['id'], $row['number'])); break; case 2: /* 品牌的商品 */ $brand_goods = assign_brand_goods($row['id'], $row['number']); $GLOBALS['smarty']->assign('brand_goods_' . $row['id'], $brand_goods['goods']); $GLOBALS['smarty']->assign('goods_brand_' . $row['id'], $brand_goods['brand']); break; case 3: /* 文章列表 */ $cat_articles = assign_articles($row['id'], $row['number']); $GLOBALS['smarty']->assign('articles_cat_' . $row['id'], $cat_articles['cat']); $GLOBALS['smarty']->assign('articles_' . $row['id'], $cat_articles['arr']); break; case 5: /* 分类下的促销商品 后加---*/ $GLOBALS['smarty']->assign('promotion_cat_' . $row['id'], assign_cat_promotion($row['id'], $row['number'])); break; } }}
上边的这个 函数 assign_cat_promotion($row['id'], $row['number'])); 你要自己定义,用来获取后台模板设置的时候指定一些参数,我这边定义在 \include\lib_goods.php里,函数头如下:
/** * 获得指定分类下的促销商品 * * @access public * @param integer $cat_id 分类ID * @param integer $num 数量 * @param string $from 来自web/wap的调用 * @param string $order_rule 指定商品排序规则 * @return array */function assign_cat_promotion($cat_id, $num = 0, $from = 'web', $order_rule = ''){
另外,在\include\lib_main.php中还要找到这个函数,做如下添加:
/** * 替换动态模块 * * @access public * @param string $matches 匹配内容 * * @return string 结果 */function dyna_libs_replace($matches){ $key = '/' . $matches[1]; if ($row = array_shift($GLOBALS['libs'][$key])) { $str = ''; switch($row['type']) { case 1: // 分类的商品 $str = '{assign var="cat_goods" value=$cat_goods_' .$row['id']. '}{assign var="goods_cat" value=$goods_cat_' .$row['id']. '}'; break; case 2: // 品牌的商品 $str = '{assign var="brand_goods" value=$brand_goods_' .$row['id']. '}{assign var="goods_brand" value=$goods_brand_' .$row['id']. '}'; break; case 3: // 文章列表 $str = '{assign var="articles" value=$articles_' .$row['id']. '}{assign var="articles_cat" value=$articles_cat_' .$row['id']. '}'; break; case 4: //广告位 $str = '{assign var="ads_id" value=' . $row['id'] . '}{assign var="ads_num" value=' . $row['number'] . '}'; break; case 5: // 分类的促销商品 -------后加的 $str = '{assign var="cat_promotion" value=$cat_promotion_' .$row['id']. '}{assign var="promotion_cat" value=$promotion_cat_' .$row['id']. '}'; break; } return $str . $matches[0]; } else { return $matches[0]; }}
这样结束lib_main.php的修改。
5、上边数据准备好,那要在后台的模板设置里能看到动态项目库,如这样格式:(注意下边红色框内是动态库项目调用的地方,我添加的就是 红色椭圆框内的)需要修改2个文件,一个是 \admin\template.php具体需要修改的地方见最后一行示例:
(1)
/* 动态内容 */ $cate_goods = array(); $brand_goods = array(); $cat_articles = array(); $ad_positions = array(); $cate_promotion = array(); //后加
(2)
foreach ($temp_libs AS $val) { /* 对动态内容赋值 */ if ($val['lib'] == 'cat_goods') { /* 分类下的商品 */ if (isset($db_dyna_libs[$val['region']][$val['library']]) && ($row = array_shift($db_dyna_libs[$val['region']][$val['library']]))) { $cate_goods[] = array('region' => $val['region'], 'sort_order' => $val['sort_order'], 'number' => $row['number'], 'cats'=>cat_list(0, $row['id'])); } else { $cate_goods[] = array('region' => $val['region'], 'sort_order' => $val['sort_order'], 'number'=>0, 'cats'=>cat_list(0)); } }/* 分类促销产品 后加的*/ elseif ($val['lib'] == 'cat_promotion') { /* 分类下的商品 */ if (isset($db_dyna_libs[$val['region']][$val['library']]) && ($row = array_shift($db_dyna_libs[$val['region']][$val['library']]))) { $cate_promotion[] = array('region' => $val['region'], 'sort_order' => $val['sort_order'], 'number' => $row['number'], 'cats'=>cat_list(0, $row['id'])); } else { $cate_promotion[] = array('region' => $val['region'], 'sort_order' => $val['sort_order'], 'number'=>0, 'cats'=>cat_list(0)); } }
(3)
assign_query_info(); $smarty->assign('ur_here', $_LANG['03_template_setup']); $smarty->assign('curr_template_file', $curr_template); $smarty->assign('temp_options', $temp_options); $smarty->assign('temp_regions', $temp_regions); $smarty->assign('cate_goods', $cate_goods); $smarty->assign('brand_goods', $brand_goods); $smarty->assign('cat_articles', $cat_articles); $smarty->assign('ad_positions', $ad_positions);$smarty->assign('cate_promotion', $cate_promotion);//后加 $smarty->assign('arr_cates', cat_list(0, 0, true)); $smarty->assign('arr_brands', get_brand_list()); $smarty->assign('arr_article_cats', article_cat_list(0, 0, true)); $smarty->assign('arr_ad_positions', get_position_list());$smarty->assign('arr_promotion', cat_list(0, 0, true));//后加 $smarty->display('template_setup.htm');
(4)
/*------------------------------------------------------ *///-- 提交模板内容设置/*------------------------------------------------------ */if ($_REQUEST['act'] == 'setting'){ admin_priv('template_setup'); $curr_template = $_CFG['template']; $db->query("DELETE FROM " .$ecs->table('template'). " WHERE remarks = '' AND filename = '$_POST[template_file]' AND theme = '$curr_template'"); /* 先处理固定内容 */ foreach ($_POST['regions'] AS $key => $val) { $number = isset($_POST['number'][$key]) ? intval($_POST['number'][$key]) : 0; if (!in_array($key, $GLOBALS['dyna_libs']) AND (isset($_POST['display'][$key]) AND $_POST['display'][$key] == 1 OR $number > 0)) { $sql = "INSERT INTO " .$ecs->table('template'). "(theme, filename, region, library, sort_order, number)". " VALUES ". "('$curr_template', '$_POST[template_file]', '$val', '".$_POST['map'][$key]."', '" . @$_POST['sort_order'][$key] . "', '$number')"; $db->query($sql); } } /* 分类的商品 */ if (isset($_POST['regions']['cat_goods'])) { foreach ($_POST['regions']['cat_goods'] AS $key => $val) { if ($_POST['categories']['cat_goods'][$key] != '' && intval($_POST['categories']['cat_goods'][$key]) > 0) { $sql = "INSERT INTO " .$ecs->table('template'). " (". "theme, filename, region, library, sort_order, type, id, number". ") VALUES (". "'$curr_template', ". "'$_POST[template_file]', '" .$val. "', '/library/cat_goods.lbi', ". "'" .$_POST['sort_order']['cat_goods'][$key]. "', 1, '" .$_POST['categories']['cat_goods'][$key]. "', '" .$_POST['number']['cat_goods'][$key]. "'". ")"; $db->query($sql); } } } /* 分类促销的商品 后加的*/ if (isset($_POST['regions']['cat_promotion'])) { foreach ($_POST['regions']['cat_promotion'] AS $key => $val) { if ($_POST['promotion']['cat_promotion'][$key] != '' && intval($_POST['promotion']['cat_promotion'][$key]) > 0) { $sql = "INSERT INTO " .$ecs->table('template'). " (". "theme, filename, region, library, sort_order, type, id, number". ") VALUES (". "'$curr_template', ". "'$_POST[template_file]', '" .$val. "', '/library/cat_promotion.lbi', ". "'" .$_POST['sort_order']['cat_promotion'][$key]. "', 5, '" .$_POST['promotion']['cat_promotion'][$key]. "', '" .$_POST['number']['cat_promotion'][$key]. "'". ")"; $db->query($sql); } } }
(5)
/* 对提交内容进行处理 */ $post_regions = array(); foreach ($_POST['regions'] AS $key => $val) { switch ($key) { case 'cat_goods': foreach ($val AS $k => $v) { if (intval($_POST['categories']['cat_goods'][$k]) > 0) { $post_regions[] = array('region' => $v, 'type' => 1, 'number' => $_POST['number']['cat_goods'][$k], 'library' => '/library/' .$key. '.lbi', 'sort_order' => $_POST['sort_order']['cat_goods'][$k], 'id' => $_POST['categories']['cat_goods'][$k]); } } break; case 'cat_promotion': //后加 foreach ($val AS $k => $v) { if (intval($_POST['promotion']['cat_promotion'][$k]) > 0) { $post_regions[] = array('region' => $v, 'type' => 5, 'number' => $_POST['number']['cat_promotion'][$k], 'library' => '/library/' .$key. '.lbi', 'sort_order' => $_POST['sort_order']['cat_promotion'][$k], 'id' => $_POST['categories']['cat_promotion'][$k]); } } break;
到此,修改完template.php文件
6、修改 \admin\templates\template_setup.htm 这个是模板设置页面的模板文件。
(1)
<!-- cateogry goods list --> <tr> <td colspan="6" style="background-color: #F4FBFB; font-weight: bold" align="left"><a href="javascript:;" onclick="addCatGoods(this)">[+]</a> {$lang.template_libs.cat_goods} </td> </tr> {foreach from=$cate_goods item=library key=lib_name} <tr> <td class="first-cell" align="right"><a href="javascript:;" onclick="removeRow(this)">[-]</a></td> <td><select name="regions[cat_goods][]"><option value="">{$lang.select_plz}</option>{html_options values=$temp_regions output=$temp_regions selected=$library.region}</select></td> <td><input type="text" name="sort_order[cat_goods][]" value="{$library.sort_order}" size="4" /></td> <td><select name="categories[cat_goods][]"><option value="">{$lang.select_plz}</option>{$library.cats}</select></td> <td><input type="text" name="number[cat_goods][]" value="{$library.number}" size="4" /></td> <td></td> </tr> {/foreach} <!-- cateogry promotion goods list 后加的 --> <tr> <td colspan="6" style="background-color: #F4FBFB; font-weight: bold" align="left"><a href="javascript:;" onclick="addCatPromotion(this)">[+]</a> {$lang.template_libs.cat_promotion} </td> </tr> {foreach from=$cate_promotion item=library key=lib_name} <tr> <td class="first-cell" align="right"><a href="javascript:;" onclick="removeRow(this)">[-]</a></td> <td><select name="regions[cat_promotion][]"><option value="">{$lang.select_plz}</option>{html_options values=$temp_regions output=$temp_regions selected=$library.region}</select></td> <td><input type="text" name="sort_order[cat_promotion][]" value="{$library.sort_order}" size="4" /></td> <td><select name="promotion[cat_promotion][]"><option value="">{$lang.select_plz}</option>{$library.cats}</select></td> <td><input type="text" name="number[cat_promotion][]" value="{$library.number}" size="4" /></td> <td></td> </tr> {/foreach}
(2)在 \admin\templates\template_setup.htm 的下边的js脚本里,要添加如下红色的(后加的)代码
<script language="JavaScript"><!--var currTemplateFile = '{$curr_template_file}';var selCategories = '{$arr_cates}';var selRegions = new Array();var selBrands = new Array();var selArticleCats = new Array();var selAdPositions = new Array();var selPromotion = '{$arr_promotion}'; //后加的{foreach from=$temp_regions item=region key=id}selRegions[{$id}] = '{$region|escape:quotes}';{/foreach}{foreach from=$arr_brands item=brand key=id}selBrands[{$id}] = '{$brand|escape:quotes}';{/foreach}{foreach from=$arr_article_cats item=cat key=id}selArticleCats[{$id}] = '{$cat}';{/foreach}{foreach from=$arr_ad_positions item=ad_position key=id}selAdPositions[{$id}] = '{$ad_position|escape}';{/foreach}{literal}
......
/** * 增加一个分类的商品 */function addCatGoods(obj){ var rowId = rowindex(obj.parentNode.parentNode); var table = obj.parentNode.parentNode.parentNode.parentNode; var row = table.insertRow(rowId + 1); var cell = row.insertCell(-1); cell.innerHTML = '<a href="javascript:;" onclick="removeRow(this)">[-]</a>'; cell.className = 'first-cell'; cell.align = 'right'; cell = row.insertCell(-1); cell.innerHTML = buildRegionSelect('regions[cat_goods][]'); cell = row.insertCell(-1); cell.innerHTML = '<input type="text" name="sort_order[cat_goods][]" value="0" size="4" />'; cell = row.insertCell(-1); cell.innerHTML = '<select name="categories[cat_goods][]"><option value="">' + selectPlease + '</option>' + selCategories + '</select>'; cell = row.insertCell(-1); cell.innerHTML = '<input type="text" name="number[cat_goods][]" value="5" size="4" />'; cell = row.insertCell(-1);}/** * 增加一个分类的促销商品,------后加 */function addCatPromotion(obj){ var rowId = rowindex(obj.parentNode.parentNode); var table = obj.parentNode.parentNode.parentNode.parentNode; var row = table.insertRow(rowId + 1); var cell = row.insertCell(-1); cell.innerHTML = '<a href="javascript:;" onclick="removeRow(this)">[-]</a>'; cell.className = 'first-cell'; cell.align = 'right'; cell = row.insertCell(-1); cell.innerHTML = buildRegionSelect('regions[cat_promotion][]'); cell = row.insertCell(-1); cell.innerHTML = '<input type="text" name="sort_order[cat_promotion][]" value="0" size="4" />'; cell = row.insertCell(-1); cell.innerHTML = '<select name="promotion[cat_promotion][]"><option value="">' + selectPlease + '</option>' + selPromotion + '</select>'; cell = row.insertCell(-1); cell.innerHTML = '<input type="text" name="number[cat_promotion][]" value="5" size="4" />'; cell = row.insertCell(-1);}......
{/literal}//--></script>
到此结束,就可以实现在后台操作模板实现动态项目库的调用了。
- ecshop 添加自定义动态库项目详解
- ecshop 添加自定义动态库项目详解
- ECSHOP 添加自定义动态库项目详解
- ECSHOP自定义action添加
- ECShop 模板库项目功能详解
- ECShop 模板库项目功能详解
- 自定义ecshop模板库
- ecshop模板制作1-添加自定义模板
- ecshop二次开发 给商品添加自定义字段
- ECShop 模板库项目功能详解(www.xiaophper.com)
- Ecshop二次开发:给商品添加自定义字段(三)
- Ecshop二次开发:给商品添加自定义字段(三)
- Ecshop后台增加|添加商店设置选项和使用方法详解
- Ecshop后台增加|添加商店设置选项和使用方法详解
- ToolStrip动态添加自定义控件
- android 动态添加自定义TextView
- 自定义动态添加字段内容的库类
- ecshop后台商品列表添加自定义方法 例如添加商品提醒信息
- MySQL中文乱码综述
- Xfontserver
- WebService文件上传,下载
- 英语常用口语1000句
- 获取当前进程的IAT
- ecshop 添加自定义动态库项目详解
- VB語法
- 开博首篇
- Fabrice Bellard
- Fabrice Bellard
- android 源码下载DownloadError: android.googlesource.com: <urlopen error [Errno 110] Connection timed out
- linux上面python升级
- ArcEngine开发中关于获取IFeature对象Pfeature坐标的说明
- 自己写的代码