聪明的搜索功能

时间:2013-08-13 15:16:01

标签: php smarty

如何在smarty中包含.php文件,从Smarty中的搜索输入处理$ _POST数据并在.tpl文件中显示结果? 如何在smarty控制器或配置文件中正确定义search.php?我现在是聪明的引擎的初学者,并不知道有关这个引擎的许多事情和技巧 index.php smarty core

<?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.'-->';
来自http://www.smarty.net/docs/en/language.function.foreach.tpl#idp8696576

search.php

<?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

<form method="post" action="../search.php" class="searchform cf">
              <input type="text" placeholder="">
              <button type="submit">Search</button>
</form>

1 个答案:

答案 0 :(得分:0)

以下是一些指示:

  1. 您不必创建多个smarty实例,应该初始化smarty的唯一地方是index.php文件
  2. 不要从search.php文件中显示index.tpl文件,index.php文件应该包含自己的TPL文件。相反,让您的search.php文件访问数据库并将结果作为数组返回,并将其分配给一个smarty变量,您可以在搜索到TPL文件时使用该变量来显示结果。
  3. 我就是这样做的。

    <强>的index.php

    <?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

    <?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

      <?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

    <!DOCTYPE html>
    <html lang="en">
        <body>
            {include file='search.tpl'}
        </body>
    </html>
    

    <强> search.tpl

    {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 statements
    Smarty foreach statements

    另外,快速说明。您可以创建一个smarty-setup.php文件,您可以像我一样声明您的config,templates,templates_C和caches目录。有关详细信息,请参见此处(http://www.smarty.net/docs/en/installing.smarty.extended.tpl)。

    重要提示 此搜索表单不以任何方式保密。它可以用来破解你的网站。为了防止此类攻击,请参阅以下文章:

    Securing PHP forms
    SQL injection(仅在查询数据库时才有必要)。