首先列出使用pc:get标签的方法:
{pc:get sql=”select keyword from v9_search_keyword” orderby=”searchnums desc” num=”5″ } {loop $data $r} <a href=”{APP_PATH}index.php?m=search&c=index&a=init&typeid=1&siteid=1&q={$r[keyword]}” >{$r[keyword]}</a> {/loop} {/pc}将上面这段代码复制到头部的任意位置,即可显示5个关键词并按搜索次数最多的前5个显示。
下面是后台增加关键词管理的操作方法。我将管理菜单放在扩展的栏目下,其它朋友可以根据知道的需求,增加到各自的栏目下,先上图,有图有真相。
1.关键词管理页面,在扩展栏目下。
3.关键字编辑页面,可以把热门的关键字增加搜索次数,以引导用户获取更多信息
请按照以下方法增加:
1.增加语言包:
找到文件phpcms/languages/zh-cn/admin.lang.php,在此文件最下面增加以下内容:
/*热门关键词*/ $LANG['search_keyword'] = ‘关键字管理’; $LANG['search_word_add'] = ‘增加关键字’; $LANG['search_word_pinyin'] = ‘拼音’; $LANG['search_word_nums'] = ‘搜索次数’; $LANG['search_word_some'] = ‘相关分词’; $LANG['search_word_confirm_del'] = ‘是否删除该关键词’; $LANG['search_word_name'] = ‘关键字名称’; $LANG['search_word_error_input'] = ‘请输入完整的数据’;2.由于模型类本来已存在,所以不需要再创建模型类,只需要创建控制器:
在phpcms\modules\admin\下,新建文件名为:search_keyword.php,路径为:phpcms\modules\admin\search_keyword.php
<?php defined(‘IN_PHPCMS’) or exit(‘No permission resources.’); pc_base::load_app_class(‘admin’,'admin’,0); class search_keyword extends admin { function __construct() { $this->db = pc_base::load_model(‘search_keyword_model’); parent::__construct(); } function init () { $page = $_GET['page'] ? intval($_GET['page']) : ’1′; //根据搜索数量由大到小排序 $infos = $this->db->listinfo(”,’searchnums DESC’,$page ,’20′); $pages = $this->db->pages; $big_menu = array(‘javascript:window.top.art.dialog({id:\’add\’,iframe:\’?m=admin&c=search_keyword&a=add\’, title:\”.L(‘search_word_add’).’\', width:\’450\’, height:\’150\’}, function(){var d = window.top.art.dialog({id:\’add\’}).data.iframe;var form = d.document.getElementById(\’dosubmit\’);form.click();return false;}, function(){window.top.art.dialog({id:\’add\’}).close()});void(0);’, L(‘search_word_add’)); include $this->admin_tpl(‘search_keyword_list’); } /** * 关键词添加 */ function add() { if(isset($_POST['dosubmit'])){ if(empty($_POST['info']['keyword']) || empty($_POST['info']['pinyin']) || empty($_POST['info']['searchnums'])){ echo L(‘search_word_error_input’); return false; } $this->db->insert($_POST['info']); showmessage(L(‘operation_success’),’?m=admin&c=search_keyword&a=add’,”, ‘add’); }else{ $show_validator = $show_scroll = $show_header = true; include $this->admin_tpl(‘search_keyword_add’); } } /** * 关键词修改 */ function edit() { if(isset($_POST['dosubmit'])){ $keywordid = intval($_GET['keywordid']); if(empty($_POST['info']['keyword']) || empty($_POST['info']['pinyin']) || empty($_POST['info']['searchnums'])){ echo L(‘search_word_error_input’); return false; } $this->db->update($_POST['info'],array(‘keywordid’=>$keywordid)); showmessage(L(‘operation_success’),’?m=admin&c=search_keyword&a=edit’,”, ‘edit’); }else{ $show_validator = $show_scroll = $show_header = true; $info = $this->db->get_one(array(‘keywordid’=>$_GET['keywordid'])); if(!$info) showmessage(L(‘specified_word_not_exist’)); extract($info); include $this->admin_tpl(‘search_keyword_edit’); } } /** * 关键词删除 */ function delete() { if(is_array($_POST['keywordid'])){ foreach($_POST['keywordid'] as $keywordid_arr) { $this->db->delete(array(‘keywordid’=>$keywordid_arr)); } showmessage(L(‘operation_success’),’?m=admin&c=search_keyword’); } else { $keywordid = intval($_GET['keywordid']); if($keywordid < 1) return false; $result = $this->db->delete(array(‘keywordid’=>$keywordid)); if($result){ showmessage(L(‘operation_success’),’?m=admin&c=search_keyword’); }else { showmessage(L(“operation_failure”),’?m=admin&c=search_keyword’); } } } } ?>3.在模板里,增加三个模板页面,分别是:
phpcms\modules\admin\templates/search_keyword_add.tpl.php (增加关键词页面) phpcms\modules\admin\templates/search_keyword_edit.tpl.php (编辑关键词页面) phpcms\modules\admin\templates/search_keyword_list.tpl.php (编辑关键词列表)(1). search_keyword_add.tpl.php
<?php defined(‘IN_ADMIN’) or exit(‘No permission resources.’); include $this->admin_tpl(‘header’); ?> <script type=”text/javascript”> <!– $(function(){ $.formValidator.initConfig({formid:”myform”,autotip:true,onerror:function(msg,obj){window.top.art.dialog({content:msg,lock:true,width:’200′,height:’50′}, function(){this.close();$(obj).focus();})}}); }) //–> </script> <div> <table width=”100%” cellpadding=”2″ cellspacing=”1″> <form action=”?m=admin&c=search_keyword&a=add” method=”post” name=”myform” id=”myform”> <tr> <th width=”25%”><?php echo L(‘search_word_name’);?> :</th> <td><input type=”text” name=”info[keyword]” id=”word” size=”20″></td> </tr> <tr> <th><?php echo L(‘search_word_pinyin’);?> :</th> <td><input type=”text” name=”info[pinyin]” value=”" id=”pinyin”></td> </tr> <tr> <th><?php echo L(‘search_word_nums’);?> :</th> <td><input type=”text” name=”info[searchnums]” value=”" id=”searchnums”></td> </tr> <tr> <th><?php echo L(‘search_word_some’);?> :</th> <td><input type=”text” name=”info[data]” value=”" id=”data”></td> </tr> <input type=”submit” name=”dosubmit” id=”dosubmit” value=” <?php echo L(‘submit’)?> “> </form> </table> </div> </body> </html>(2).关键词列表
<?php defined(‘IN_ADMIN’) or exit(‘No permission resources.’); $show_dialog = 1; include $this->admin_tpl(‘header’,'admin’); ?> <div> <form name=”myform” id=”myform” action=”?m=admin&c=search_keyword&a=delete” method=”post” onsubmit=”checkuid();return false;”> <div> <table width=”100%” cellspacing=”0″> <thead> <tr> <th width=”35″ align=”center”><input type=”checkbox” value=”" id=”check_box” onclick=”selectall(‘keywordid[]‘);”></th> <th width=”30%”><?php echo L(‘keyword_name’)?></th> <th ><?php echo L(‘search_word_pinyin’)?></th> <th ><?php echo L(‘search_word_nums’)?></th> <th ><?php echo L(‘search_word_some’)?></th> <th width=”120″><?php echo L(‘operations_manage’)?></th> </tr> </thead> <tbody> <?php if(is_array($infos)){ foreach($infos as $info){ ?> <tr> <td align=”center”> <input type=”checkbox” name=”keywordid[]” value=”<?php echo $info['keywordid']?>”> </td> <td width=”30%” align=”left”><span ><?php echo $info['keyword']?></span> </td> <td align=”center”><?php echo $info['pinyin']?></td> <td align=”center”><?php echo $info['searchnums']?></td> <td align=”center”><?php echo $info['data']?></td> <td align=”center”><a href=”javascript:edit(<?php echo $info['keywordid']?>, ‘<?php echo new_addslashes($info['keyword'])?>’)”><?php echo L(‘edit’)?></a> | <a href=”javascript:confirmurl(‘?m=admin&c=search_keyword&a=delete&keywordid=<?php echo $info['keywordid']?>’, ‘<?php echo L(‘search_word_confirm_del’)?>’)”><?php echo L(‘delete’)?></a> </td> </tr> <?php } } ?></tbody> </table> <div> <a href=”#” onClick=”javascript:$(‘input[type=checkbox]‘).attr(‘checked’, true)”><?php echo L(‘selected_all’)?></a>/<a href=”#” onClick=”javascript:$(‘input[type=checkbox]‘).attr(‘checked’, false)”><?php echo L(‘cancel’)?></a> <input type=”submit” name=”submit” value=”<?php echo L(‘remove_all_selected’)?>” onClick=”return confirm(‘<?php echo L(‘badword_confom_del’)?>’)” /> </div> <div id=”pages”><?php echo $pages?></div> </div> </form> </div> </body> </html> <script type=”text/javascript”> function edit(id, name) { window.top.art.dialog({id:’edit’}).close(); window.top.art.dialog({title:’<?php echo L(‘edit’)?> ‘+name+’ ‘,id:’edit’,iframe:’?m=admin&c=search_keyword&a=edit&keywordid=’+id,width:’450′,height:’150′}, function(){var d = window.top.art.dialog({id:’edit’}).data.iframe;var form = d.document.getElementById(‘dosubmit’);form.click();return false;}, function(){window.top.art.dialog({id:’edit’}).close()}); } function checkuid() { var ids=”; $(“input[name='keywordid[]‘]:checked”).each(function(i, n){ ids += $(n).val() + ‘,’; }); if(ids==”) { window.top.art.dialog({content:’<?php echo L(‘badword_pleasechose’)?>’,lock:true,width:’200′,height:’50′,time:1.5},function(){}); return false; } else { myform.submit(); } } </script>(3). 编辑关键词
<?php defined(‘IN_ADMIN’) or exit(‘No permission resources.’); include $this->admin_tpl(‘header’); ?> <script type=”text/javascript”> $(function(){ $.formValidator.initConfig({formid:”myform”,autotip:true,onerror:function(msg,obj){window.top.art.dialog({content:msg,lock:true,width:’200′,height:’50′}, function(){this.close();$(obj).focus();})}}); }) </script> <div> <table cellpadding=”2″ cellspacing=”1″ width=”100%”> <form action=”?m=admin&c=search_keyword&a=edit&keywordid=<?php echo $keywordid?>” method=”post” name=”myform” id=”myform”> <tr> <th width=”25%”><?php echo L(‘search_word_name’);?> :</th> <td><input type=”text” name=”info[keyword]“ size=”20″ value=”<?php echo $keyword?>”></td> </tr> <tr> <th><?php echo L(‘search_word_pinyin’);?> :</th> <td><input type=”text” name=”info[pinyin]” value=”<?php echo $pinyin?>” ></td> </tr> <tr> <th><?php echo L(‘search_word_nums’);?> :</th> <td><input type=”text” name=”info[searchnums]” value=”<?php echo $searchnums?>” ></td> </tr> <tr> <th><?php echo L(‘search_word_some’);?> :</th> <td><input type=”text” name=”info[data]” value=”<?php echo $data?>”></td> </tr> <input type=”submit” name=”dosubmit” id=”dosubmit” value=” <?php echo L(‘submit’)?> “> </form> </table> </div> </body> </html>4.增加导航菜单:
请使用phpmyadmin或其它mysql数据库管理软件执行下面此语句。
1.为关键词表增加一个可自动增加的id,keywordid
alter table v9_search_keyword add keywordid int(16) unsigned NOT NULL auto_increment PRIMARY KEY ;2.以下是插入菜单链接:
INSERT INTO v9_menu VALUES (null, ‘search_keyword’, ’977′, ‘admin’, ‘search_keyword’, ‘init’, ”, ’13′, ’1′); INSERT INTO v9_menu VALUES (null, ‘add_search_keyword’, ’1571′, ‘admin’, ‘search_keyword’, ‘add’, ”, ’0′, ’0′); INSERT INTO v9_menu VALUES (null, ‘edit_search_keyword’, ’1571′, ‘admin’, ‘search_keyword’, ‘edit’, ”, ’0′, ’0′); INSERT INTO v9_menu VALUES (null, ‘del_search_keyword’, ’1571′, ‘admin’, ‘search_keyword’, ‘del’, ”, ’0′, ’0′);通过以上步骤,即可完成后台管理热门管理词。
但现在还有一个问题没有解决,
相关分词正常来说是通过程序生成的,因为不是程序生成的话,在搜索页面相关搜索并不会出现数据,我的想法是把输入框禁用,然后旁边增加一个获取相关分词的按钮,点击按钮,通过程序生成,并把生成的值赋给相关分词的输入框,但由于时间关系,并没有做这一步处理,有兴趣的朋友可以进行增加。这个功能一定要做的,但不知道有没有时间把文章补完,我尽量把此功能完成后,再把方法告诉大家。以下是我所增加的文件,提供大家进行下载,有不明白的地方,可以留言进行交流。
以下压缩包4个文件:
phpcms/modules/admin/search_keyword.php phpcms\modules\admin\templates/search_keyword_add.tpl.php phpcms\modules\admin\templates/search_keyword_edit.tpl.php phpcms\modules\admin\templates/search_keyword_list.tpl.php