首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >智能搜索功能

智能搜索功能
EN

Stack Overflow用户
提问于 2013-08-13 15:16:01
回答 1查看 1.4K关注 0票数 0

如何在smarty中包含.php文件,从Smarty中的搜索输入中处理$_POST数据,并在.tpl文件中显示结果?如何在智能控制器或配置文件中正确定义search.php?我现在是智能引擎的初学者,对这个引擎不了解很多东西和窍门

index.php智能核心

代码语言:javascript
复制
<?php
//ob_start('ob_gzhandler');
$t1 = microtime ( 1 );
session_start ();
header ( "Content-Type: text/html; charset=utf-8" );
require_once ("inc/initf.php");
//require_once("/verjani/public_html/inc/search.php");//how to include ?
$smarty = new Smarty ();
// $smarty->debugging = true;
// $smarty->error_reporting = E_ALL & ~E_NOTICE;
$smarty->cache_dir = THEM_PATH . "/cache";
$smarty->template_dir = THEM_PATH . "/template";
$smarty->compile_dir = THEM_PATH . "/template_c";
Helper::register($smarty);
$frontEnd = new frontEnd ();
$module = $frontEnd->getModule ();
$module->viewHeaders ();
if ($module->displayTpl !== false) {
    $smarty->assign ( 'COOKIE', $_COOKIE );
    $smarty->assign ( 'this', $module );
    $smarty->display ( $module->displayTpl, md5 ( $_SERVER ['REQUEST_URI'] ) );
}
$t = microtime();
echo '<!--'.$t.'-->';

search.php,来自http://www.smarty.net/docs/en/language.function.foreach.tpl#idp8696576

代码语言:javascript
复制
<?php 
  include('Smarty.class.php'); 

  $smarty = new Smarty; 

  $dsn = 'mysql:host=localhost;dbname=test'; 
  $login = 'test'; 
  $passwd = 'test'; 

  // setting PDO to use buffered queries in mysql is 
  // important if you plan on using multiple result cursors 
  // in the template. 

  $db = new PDO($dsn, $login, $passwd, array( 
     PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); 

  $res = $db->prepare("select * from users"); 
  $res->execute(); 
  $res->setFetchMode(PDO::FETCH_LAZY); 

  // assign to smarty 
  $smarty->assign('res',$res); 

  $smarty->display('index.tpl');?>
?>

header.tpl.html

代码语言:javascript
复制
<form method="post" action="../search.php" class="searchform cf">
              <input type="text" placeholder="">
              <button type="submit">Search</button>
</form>
EN

回答 1

Stack Overflow用户

发布于 2015-08-12 17:50:23

以下是一些提示:

  1. 您不必创建多个智能实例,唯一需要初始化的位置是在index.php文件中
  2. 不要从您的index.tpl文件中显示您的search.php文件,index.php文件应该包括它自己的TPL文件。相反,让您的search.php文件访问数据库,并将结果作为数组返回,并将其分配给一个智能变量,您可以在搜索中使用该变量来显示结果。

我就是这样做的。

Index.php

代码语言:javascript
复制
<?php
require('smarty-setup.php');

$smarty = new Smarty_Setup(); //must match class created in smarty setup file in order for it to work


require('search.php');

//uncomment these lines for debugging
// $smarty->debugging = true;
// $smarty->error_reporting = E_ALL & ~E_NOTICE;
$smarty->display('test.tpl');

smarty-setup.php

代码语言:javascript
复制
<?php

// load Smarty library
require('/smarty/Smarty.class.php');


// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');

class Smarty_Setup extends Smarty {

   function __construct()

   {

        // Class Constructor.
        // These automatically get set with each new instance.
        parent::__construct();


        $this->setTemplateDir('/templates/');
        $this->setCompileDir('/smarty/templates_c/');
        $this->setConfigDir('/smarty/configs/');
        $this->setCacheDir('/smarty/cache/');
   }
}
?>

Search.php

代码语言:javascript
复制
  <?php

  if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['search'])))
  {

      $ResultsArray = array();
      //conduct your search using $_POST['search'] to get the search
      //query and put the results in the $ResultsArray above.

      $smarty->assign('searchResults', $ResultsArray);
      $smarty->assign('displayForm', 'false');
  } else {

      $smarty->assign('displayForm', 'true');
  }
  ?>

Index.tpl

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
    <body>
        {include file='search.tpl'}
    </body>
</html>

search.tpl

代码语言:javascript
复制
{if $displayForm eq true}
    <form method="post" action="{$smarty.server.SCRIPT_NAME}" class="searchform cf">
          <input type="text" name="search" placeholder="Search" />
          <button type="submit">Search</button>
    </form>

{else}

    <h1>Search Results</h1>
    <ul>
        {foreach from=$searchResults item=result}
            <li>{$result}</li>
        {/foreach}
    </ul>
{/if}

这里有一些更多的信息,如果你需要的话:

Smarty if语句

聪明的预言

还有,快速笔记。您可以创建一个智能-setup.php文件,在该文件中可以像我一样声明您的配置、模板、templates_C和缓存目录。有关这方面的更详细信息,请参见这里(http://www.smarty.net/docs/en/installing.smarty.extended.tpl)。

重要注意事项此搜索表单在任何方面都不安全。它可以用来黑你的网站。为了防止这类攻击,请参阅下列条款:

保护PHP表单

SQL注入 (只有在查询数据库时才有必要)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18212919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档