ecshop細究php
<!-- #BeginLibraryItem "/library/page_header.lbi" --><!-- #EndLibraryItem -->
便可重用的搜索模塊代碼,拼接在body下面,咱們能夠進入這個文件看看,在50行左右html
<div class="serach-box"> <form id="searchForm" name="searchForm" method="get" action="search.php" onSubmit="return checkSearchForm()" class="f_r"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="135"><input name="keywords" type="text" id="keyword" value="{$search_keywords|escape}" class="B_input" /></td> <td><input name="imageField" type="submit" value="搜索" class="go" style="cursor:pointer;" /></td> </tr> </table> </form> </div>
這樣就很清楚了,url對應的是根目錄下php,文本框裏面設置mysql
<span style="font-size:18px;">value="{$search_keywords|escape}"</span>
即保留搜索值,form表單裏面的action指向php,可是在檢索以前,也即表單提交的同時,經過checkSearchForm()驗證輸入合法,而後咱們到根目錄下search.php中看看,代碼很是多,須要檢索的東西有點複雜,但能夠找到經過表單提交過來的keywordssql
/* 初始化搜索條件 */ $keywords = ''; $tag_where = ''; if (!empty($_REQUEST['keywords'])) { $arr = array(); if (stristr($_REQUEST['keywords'], ' AND ') !== false) { /* 檢查關鍵字中是否有AND,若是存在就是並 */ $arr = explode('AND', $_REQUEST['keywords']); $operator = " AND "; } elseif (stristr($_REQUEST['keywords'], ' OR ') !== false) { /* 檢查關鍵字中是否有OR,若是存在就是或 */ $arr = explode('OR', $_REQUEST['keywords']); $operator = " OR "; } elseif (stristr($_REQUEST['keywords'], ' + ') !== false) { /* 檢查關鍵字中是否有加號,若是存在就是或 */ $arr = explode('+', $_REQUEST['keywords']); $operator = " OR "; } else { /* 檢查關鍵字中是否有空格,若是存在就是並 */ $arr = explode(' ', $_REQUEST['keywords']); $operator = " AND "; } $keywords = 'AND ('; $goods_ids = array(); foreach ($arr AS $key => $val) { if ($key > 0 && $key < count($arr) && count($arr) > 1) { $keywords .= $operator; } $val = mysql_like_quote(trim($val)); $sc_dsad = $_REQUEST['sc_ds'] ? " OR goods_desc LIKE '%$val%'" : ''; $keywords .= "(goods_name LIKE '%$val%' OR goods_sn LIKE '%$val%' OR keywords LIKE '%$val%' $sc_dsad)"; $sql = 'SELECT DISTINCT goods_id FROM ' . $ecs->table('tag') . " WHERE tag_words LIKE '%$val%' "; $res = $db->query($sql); while ($row = $db->FetchRow($res)) { $goods_ids[] = $row['goods_id']; } $db->autoReplace($ecs->table('keywords'), array('date' => local_date('Y-m-d'), 'searchengine' => 'ecshop', 'keyword' => addslashes(str_replace('%', '', $val)), 'count' => 1), array('count' => 1)); } $keywords .= ')'; $goods_ids = array_unique($goods_ids); $tag_where = implode(',', $goods_ids); if (!empty($tag_where)) { $tag_where = 'OR g.goods_id ' . db_create_in($tag_where); } }
在上面的代碼中經過模塊化
<span style="font-size:18px;">$_REQUEST['keywords']</span>
來獲取關鍵值,以後進行一些其餘處理,而後拼接sql語句,查詢返回的結果經過url
$smarty->assign('goods_list', $arr);注入值,最後在search.dwt文件中還原整個頁面
<span style="font-size:18px;"><!-- {if $action eq "form"} --></span>
這裏我也不是太清楚,網上搜了下,沒有相關的回答,大致上就是這樣注入數據,仔細看看會發現嵌套使用蠻多的,search.dwt文件裏面表單也會調用search.php文件,而後不斷循環,我就感受這樣很複雜,模塊化並很差。spa