您现在的位置是:首页 > 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功能开发
很赞哦! ()
相关教程
图文教程
帝国CMS自动把内容图片的alt和title替换为标题的方法
1.把下面的代码放到/e/class/userfun.php (放到最后?>的前面)2.在模型里正文字段一般是newstext(也可以是其它字段),在字段处理函数文本框填上user_imgalt:
帝国CMS实现列表点击数排序下载量排序的方法
利用帝国CMS结合项功能可实现列表页多样化,默认是按最新信息排序的,比如按点击数降序、升序,下载量,价格排序等等。
帝国cms调用指定条件信息的使用方法
帝国cms调用指定条件的信息,我们就得用到sql语句中的“LIKE”"LIKE"为通配符。在帝国CMS中,我们有时需要调用某一指定条件的信息,案列如下:
帝国CMS栏目ID结合项伪静态支持分页的方法
跟帝国官方的差不多,只是伪静态了。没有官方支持的筛选项多,只支持:栏目ID,筛选项,分页。需要其他比如:专题ID模型id模版ID排序等等,可以自己修改。
相关源码
-
(PC+WAP)房屋建造建筑工程房地产建材行pbootcms网站模板下载本模板基于PbootCMS内核开发,为房屋建造、建筑工程及房地产建材类企业设计。通过本模板可快速搭建具有行业特色的企业官网,只需替换文字与图片内容即可适配其他行业使用。查看源码 -
(响应式)企业管理人力资源服务类pbootcms模板源码下载为人力资源服务及企业管理设计的响应式网站模板,基于PbootCMS内核开发。通过宽屏布局优化岗位展示效果,简洁界面聚焦人才服务核心业务,自适应技术确保在PC端与手机端查看源码 -
响应式帝国cms7.5NBA黑色体育资讯模板下载本模板为体育新闻媒体、报道机构设计,采用帝国CMS7.5内核开发,具备完整的资讯发布、体育日历、数据展示功能。响应式布局确保在手机端呈现实时资讯和图文内容查看源码 -
(自适应响应式)HTML5电脑手机电子数码产品配件pbootcms模板下载本模板为电脑配件、手机配件及数码周边产品企业设计,基于PbootCMS内核开发。采用现代化响应式布局,适配各类移动设备,能够专业展示各类电子产品配件参数、应用场景和技术特点。模板内置多种产品展示模块,满足不同类型配件企业的展示需求。查看源码 -
(PC+WAP)生活资讯百科新闻门户类pbootcms网站模板为生活资讯、百科门户类企业打造的高性能网站模板,基于PbootCMS开源内核开发,采用HTML5响应式架构,PC与手机端实时数据同步,覆盖全终端用户浏览场景。查看源码 -
(PC+WAP)货物运输快递物流汽车贸易pbootcms模板下载为货运代理、汽车贸易及快递企业设计的全终端适配网站系统,整合运单追踪与车辆展示核心功能模块原生开发的DIV+CSS架构,支持WebP图像压缩技术。查看源码
| 分享笔记 (共有 篇笔记) |

