您现在的位置是:首页 > 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功能开发 

很赞哦! ()

相关源码

  • (PC+WAP)蓝色电缆桥架五金钢结构机械PbootCMS模板下载采用PC与WAP双端适配设计,满足桌面设备和移动端访问需求。专注服务于电缆桥架、钢结构及五金机械制造领域,通过结构化布局展示产品特性与技术参数,后台数据一体化管理提升内容维护效率。查看源码
  • (自适应)绿色新闻生活百科资讯文章博客类网站pbootcms模板源码本模板基于PbootCMS开发,为生活百科、资讯文章和博客类网站设计。采用清新绿色系风格,提供舒适的阅读体验,同时适配PC和移动设备。适用于生活技巧分享、健康知识传播查看源码
  • (自适应响应式)WORDwps办公资源教程资讯网站模板下载基于PbootCMS内核开发的响应式网站模板,为办公教程、WPS技巧分享、职场技能培训等场景打造。模板内置标准化文档分类体系,支持图文/视频教程混合展示,满足现代办公知识传播需求。查看源码
  • (自适应)简繁双语机械矿山矿石五金设备pbootcms源码下载本模板基于PbootCMS开发,主要面向机械五金、矿山矿石设备等行业,支持简体中文和繁体中文双语切换。采用响应式布局技术,确保在各种设备上都能获得良好的浏览体验。模板设计注重展示工业设备的专业性和技术特点,帮助企业建立可靠的线上展示平台。查看源码
  • (自适应)pbootcms家政服务保洁保姆打扫卫生网站模板下载本模板基于PbootCMS内核开发,为家政服务企业量身定制。设计风格温馨亲切,突出家政行业的专业与贴心服务特性,多方位展示企业服务项目与优势。查看源码
  • (自适应)五金配件机械加工设备pbootcms模板免费下载这款基于PbootCMS开发的网站模板为五金配件和机械加工行业设计,采用简洁有力的设计风格,突出工业产品的专业性和可靠性。模板结构清晰,功能完善,能够有效展示各类工业产品的技术参数和应用场景。查看源码
分享笔记 (共有 篇笔记)
验证码: