您现在的位置是:首页 > 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>&nbsp;>&nbsp;".$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功能开发 

很赞哦! ()

相关源码

  • 帝国cms7.5模板生活常识美食女性时尚综合资讯门户源码本模板为帝国cms7.5生活资讯类网站设计,以天奇生活网为参考原型,涵盖生活常识、美食烹饪、女性时尚等多元化内容板块。采用响应式布局设计,适配各种终端设备,为访客提供优质的阅读体验。模板结构清晰合理,内容展示层次分明,符合生活类网站的用户需求。查看源码
  • (PC+WAP)红色驾校培训学车在线预约源码下载基于PbootCMS内核开发的驾校培训专用网站模板,深度适配驾驶培训行业展示需求。采用PC与移动端同步响应设计,单一后台统一管理内容数据,更换图文素材后可快速转变为其他行业网站。查看源码
  • (PC+WAP)绿色产品环保设备垃圾桶厂家公司网站pbootcms模板为垃圾桶生产商、环保设备企业打造的高端响应式门户模板,基于PbootCMS开源内核深度开发。采用HTML5自适应架构,无缝实现PC与手机端数据实时同步与交互优化查看源码
  • (自适应)变压器电子元器件电器配件pbootcms网站模板源码为电子元器件企业打造的响应式网站模板,基于PbootCMS内核开发,助力企业快速构建专业级线上展示平台。支持页面独立设置标题、关键词和描述,内置SEO友好结构。PHP程序确保运行安全稳定,有助于提升搜索引擎收录效果。查看源码
  • (自适应)蓝色建材亚克力板材装饰材料pbootcms网站模板这款基于PbootCMS开发的响应式模板为建材板材行业优化设计,适用于亚克力板材、建筑装饰材料等企业展示需求。采用HTML5技术实现手机/PC跨终端适配,管理员通过统一后台即可同步查看源码
  • (PC+WAP)铝合金门窗定制pbootcms网站模板源码为铝合金门窗、定制门窗企业打造的高性能网站模板,基于PbootCMS开源内核开发,采用HTML5自适应架构,PC与手机端数据实时同步,覆盖全终端用户交互场景。查看源码
分享笔记 (共有 篇笔记)
验证码: