您现在的位置是:首页 > cms教程 > 帝国CMS教程帝国CMS教程
帝国cms多个搜索多个模板(不同栏目不同搜索模板)原创教程
2025-08-09帝国CMS教程已有人查阅
导读今天下午有人问代码号搜索结果很乱,太多分不清是教程还是模板,能不能优化搜索教程结果就是教程,搜模板的结果就是模板,给不同的页面结果。答案是可以的。下面来具体操作思路分享

今天下午有人问代码号搜索结果很乱,太多分不清是教程还是模板,能不能优化搜索教程结果就是教程,搜模板的结果就是模板,给不同的页面结果。答案是可以的。下面来具体操作思路分享给大家。原创文章,转载请标明出处和本文连接地址;
步骤一,对搜索进行二次开发
下面是原来的头部搜索块
<form name="search" method="post" action="/e/search/index.php">
<input type="text" name="keyboard" size="11" placeholder="请输入关键字搜索">
<button class="sea-btn"><i class="iconfont icon-search"></i></button>
<input type="hidden" name="show" value="title,newstext">
<input type="hidden" name="tbname" value="news">
<input type="hidden" name="tempid" value="1">
</form>
知己知彼百战不殆,先对代码大概的给大家分析一下基础的搜索表单HTML代码,主要用于网站内容搜索功能
表单通过POST方式提交到/e/search/index.php处理,包含以下核心元素:
输入框
文本输入框设置size=11个字符宽度,显示"请输入关键字搜索"的占位提示。
使用name="keyboard"作为参数名传递搜索关键词
隐藏字段
show参数限定返回title和newstext字段
tbname=news指定搜索news数据表
tempid=1表示使用模板ID1为1的模板显示搜索页面
步骤二,多搜索开发
大概知道了上面的代码参数就不难进行二次开发了,下面我贴出我修改过的代码给大家参考。
<div class="search-form">
<form name="search" method="post" action="/e/search/index.php">
<input type="text" name="keyboard" size="11" placeholder="请输入搜索内容">
<select id="templateSelect" class="template-select">
<option value="1">全站搜索</option>
<option value="2">教程搜索</option>
<option value="3">源码搜索</option>
</select>
<button class="sea-btn"><i class="iconfont icon-search"></i></button>
<input type="hidden" name="show" value="title,newstext">
<input type="hidden" name="tbname" value="news">
<input type="hidden" id="tempid" name="tempid" value="1">
<input type="hidden" id="classid" name="classid" value="0">
<input type="hidden" id="searchtype" name="searchtype" value="full">
</form>
</div>
<script>
document.getElementById('templateSelect').addEventListener('change', function() {
const selectedValue = this.value;
const tempidField = document.getElementById('tempid');
const classidField = document.getElementById('classid');
const searchtypeField = document.getElementById('searchtype');
// 全站搜索配置
if(selectedValue === '1') {
tempidField.value = '1';
classidField.value = '0'; // 不限定栏目
searchtypeField.value = 'full';
}
// 教程搜索配置
else if(selectedValue === '2') {
tempidField.value = '1'; // 使用模板ID1
classidField.value = '2'; // 限定栏目ID2
searchtypeField.value = 'article';
}
// 源码搜索配置
else {
tempidField.value = '2'; // 使用模板ID2
classidField.value = '7'; // 限定栏目ID7
searchtypeField.value = 'source'; // 修改为source类型
}
});
</script>
多级搜索模板,模板1拆分为"全站搜索"和"文章搜索"两个选项,通过隐藏字段searchtype区分。模板2专用于文档搜索,保持独立功能。
JavaScript根据选择实时更新tempid、classid和searchtype三个隐藏字段全站搜索时classid=0表示不限定栏目。
说明:
1
这里分三个搜索可以筛选
value="1"全站搜索
value="2"教程搜索
value="3"模板搜索
2
全站搜索(classid=0)不限定栏目范围
文章/文档搜索分别限定特定栏目ID(2/7)
然后添加一个搜索模板
步骤三,添加搜索模板

搜索模板样式就就自己写了,然后调整一下上面搜索框的样式,可以看看代码号的搜索>搜索展示demo
到这里基本上就改造完成了,但是搜索结果时常有bug搜索的结果同一个词用不用的模板还是之前的模板,考虑到缓存的问题,对下面的页面进行开发,源码我贴在下面文件路径/e/search/result/index.php
<?php
require('../../class/connect.php');
require('../../class/db_sql.php');
require('../../class/functions.php');
require('../../class/t_functions.php');
require('../../data/dbcache/class.php');
require "../".LoadLang("pub/fun.php");
$editor=1;
eCheckCloseMods('search');//关闭模块
$link=db_connect();
$empire=new mysqlquery();
$getvar=$_GET['getvar'];
if(empty($getvar))
{
$getfrom="history.go(-1)";
}
else
{
$getfrom="../../../search/";
}
//搜索结果
$searchid=(int)$_GET['searchid'];
if(empty($searchid))
{
printerror("SearchNotRecord",$getfrom,1);
}
$search_r=$empire->fetch1("select searchid,keyboard,result_num,orderby,myorder,tbname,tempid,andsql,trueclassid from {$dbtbpre}enewssearch where searchid='$searchid'");
if(empty($search_r['searchid'])||InfoIsInTable($search_r[tbname]))
{
printerror("SearchNotRecord",$getfrom,1);
}
$page=(int)$_GET['page'];
$page=RepPIntvar($page);
$start=0;
$page_line=$public_r['search_pagenum'];//每页显示链接数
$line=$public_r['search_num'];//每页显示记录数
$offset=$page*$line;//总偏移量
$search="&searchid=".$searchid;
$myorder=$search_r[orderby];
if(empty($search_r[myorder]))
{
$myorder.=" desc";
}
$add=stripSlashes($search_r['andsql']);
$num=$search_r[result_num];
$query="select * from {$dbtbpre}ecms_".$search_r[tbname].($add?' where '.substr($add,5):'');
$query.=" order by ".$myorder." limit $offset,$line";
$sql=$empire->query($query);
$listpage=page1($num,$line,$page_line,$start,$page,$search);
//取得模板
if($search_r['tempid'])
{
$tempr=$empire->fetch1("select temptext,subnews,listvar,rownum,showdate,modid,subtitle,docode from ".GetTemptb("enewssearchtemp")." where tempid='".$search_r['tempid']."' limit 1");
}
elseif(empty($class_r[$search_r[trueclassid]][searchtempid]))
{
$tempr=$empire->fetch1("select temptext,subnews,listvar,rownum,showdate,modid,subtitle,docode from ".GetTemptb("enewssearchtemp")." where isdefault=1 limit 1");
}
else
{
$tempr=$empire->fetch1("select temptext,subnews,listvar,rownum,showdate,modid,subtitle,docode from ".GetTemptb("enewssearchtemp")." where tempid='".$class_r[$search_r[trueclassid]][searchtempid]."' limit 1");
}
$have_class=1;
//页面支持标签
$tempr[temptext]=DtNewsBq('list'.$search_r['tempid'],$tempr[temptext],0);
//替换公共模板变量
$listtemp=$tempr[temptext];
if($public_r['searchtempvar'])
{
$listtemp=ReplaceTempvar($listtemp);
}
$search_r[keyboard]=ehtmlspecialchars($search_r[keyboard]);
$listtemp=str_replace("[!--show.page--]",$listpage,$listtemp);
$listtemp=str_replace("[!--keyboard--]",$search_r[keyboard],$listtemp);
$listtemp=str_replace("[!--ecms.num--]",$num,$listtemp);
$url="<a href='".ReturnSiteIndexUrl()."'>".$fun_r['index']."</a> > ".$fun_r['adsearch'];
$pagetitle=$fun_r['adsearch']." ".$search_r[keyboard];
$listtemp=ReplaceSvars($listtemp,$url,0,$pagetitle,$pagetitle,$pagetitle,$add,0);
$rownum=$tempr[rownum];
if(empty($rownum))
{
$rownum=1;
}
$formatdate=$tempr[showdate];
$subnews=$tempr[subnews];
$subtitle=$tempr[subtitle];
$docode=$tempr[docode];
$modid=$tempr[modid];
$listvar=str_replace('[!--news.url--]',$public_r[newsurl],$tempr[listvar]);
//字段
$ret_r=ReturnReplaceListF($tempr[modid]);
//取得列表模板
$list_exp="[!--empirenews.listtemp--]";
$list_r=explode($list_exp,$listtemp);
$listtext=$list_r[1];
$no=$offset+1;
$changerow=1;
while($r=$empire->fetch($sql))
{
//替换列表变量
$repvar=ReplaceListVars($no,$listvar,$subnews,$subtitle,$formatdate,$url,$have_class,$r,$ret_r,$docode);
$listtext=str_replace("<!--list.var".$changerow."-->",$repvar,$listtext);
$changerow+=1;
//超过行数
if($changerow>$rownum)
{
$changerow=1;
$string.=$listtext;
$listtext=$list_r[1];
}
$no++;
}
db_close();
$empire=null;
//多余数据
if($changerow<=$rownum&&$listtext<>$list_r[1])
{
$string.=$listtext;
}
$string=$list_r[0].$string.$list_r[2];
echo stripSlashes($string);
?>
到这里恭喜你帝国cms多搜索多模板已经开发好了。
本文标签:帝国cms功能开发
很赞哦! ()
相关教程
图文教程
帝国CMS8.0新增内置JSON相关处理函数使用方法
为了更方便向外部APP或小程序输出对应格式的内容,帝国CMS8.0版新增内置JSON相关处理函数,这样给模板里的变量加该处理函数就能更方便的对接:
帝国CMS的ueditor百度编辑器路径加载错误分析和解决方法
如果你的帝国CMS使用了ueditor百度编辑器可以收藏下这篇文章,网站环境如果使用了CDN或者其他缓存服务,那么ueditor百度编辑器的初始化样式文件路径加载就会错误
帝国CMS前段投稿获取显示IP地址
有一些需求是前段投稿功能需要自动获取投稿人的IP地址并录入数据库,故有此教程!废话不多说直接步骤来。步骤如下:1、管理数据表 - ***数据表 - 管理字段 - 添加一个 IP字段2、
帝国CMS列表页置顶信息加置顶图标的方法
帝国CMS实现列表页信息列表置顶信息加上置顶小图标的教程。如赶集网、58同城等都有这样的效果:
相关源码
-
(自适应)餐具英文外贸生活用品带下载功能网站模板免费下载为餐具及生活用品外贸企业打造的响应式网站模板,基于PbootCMS内核开发。突出产品展示与多语言支持特性,通过自适应设计确保更好客户在手机、平板、电脑等设备上获得一致浏览体验。查看源码 -
自适应响应式绿色装修公司定制家居类pbootcms网站下载(自适应手机端)响应式全屋装修定制家居类网站pbootcms模板 绿色装修公司网站源码下载PbootCMS内核开发的网站模板,该模板适用于装修定制网站、装查看源码 -
(PC+WAP)聚氨酯粉末涂料防腐耐用材料粘合剂网站源码下载为化工涂料企业设计的展示系统,集成产品技术参数库、颜色样板展示器和配方查询模块。支持粉末涂料、环氧树脂等多类产品分类展示查看源码 -
(PC+WAP)五金机械设备营销型模板下载带在线留言为机械设备制造商设计的营销型模板,集成产品参数对比系统、产品展示模块和询价管理功能。采用PbootCMS开发内核,PHP7+运行环境。手工编写语义化HTML5结构,CSS3动画优化交互体验。查看源码 -
(PC+WAP)楼承板建筑工程合金钢铁材料pbootcms网站源码下载为楼承板生产商及建筑工程企业设计的响应式网站模板,采用PbootCMS内核开发,同步适配电脑与移动设备浏览。通过模块化结构展示工程案例、产品参数等专业内容,帮助建筑建材企业建立标准化数字展示平台,提升行业专业形象。查看源码 -
(自适应响应式)工业机床工程农业机械设备网站源码下载框架适用于工程机械、机床设备等工业领域。通过模块调整可快速转型为农业机械、物流设备展示系统。预留7种工业产品展示模板。查看源码
| 分享笔记 (共有 篇笔记) |

