我正在尝试在<div>
smarty 模板上显示搜索结果。搜索结果来自点击。
锚是这样的:
<li class="price-list-item"><a href="javascript:void();" class="price" onclick="searchRange(0,1000)">0-1000</a></li>
searchRange 函数是这样的:
function searchRange(first, second) {
var first = first;
var second = (second != null) ? second : first;
$("#ad-display").load("index.php?first="+first+"&second="+second+" #ad-display");
}
所写的广告显示框adlistdisplay.tpl
如下所示:
<div id="ad-display">
<section id="add-list">
<ul id="ad-list">
{foreach from=$adverts key=k item=i name=ad_list}
<li class="ad-list-item-{cycle values="odd,even"}" id="ad-{$i.aid}">
<div class="ad-item-box">
<div class="ad-item-photo">
<a href="viewad.php?aid={$i.aid}"><img src="adimages/{$i.img}" width="80" height="80"></a>
</div>
<div class="ad-item-info">
<span class="ad-item-title"><a href="viewad.php?aid={$i.aid}">{$i.title}</a></span>
<span class="ad-item-desc">{$i.text|truncate:25}</span>
<span class="ad-item-price">Kr. {$i.price|number_format:2:",":"."}</span>
</div>
</div>
</li>
{/foreach}
</ul>
</section>
</div>
在我的 index.php 我有这个代码:
$adverts = new Adverts();
$smarty->assign('catlist', $adverts->getcategories());
SmartyPaginate::assign($smarty);
if(isset($_GET["first"])) {
$search = new SearchEngine();
$smarty->assign('adverts', $search->search_ads(null, "price", $_GET["first"], $_GET["second"]));
} else {
$smarty->assign('adverts', $adverts->listadverts());
}
$smarty->display('index.tpl');
index.tpl 包含以下内容:
{include file="adlistdisplay.tpl"}
当我单击顶部的锚点时,它只会在#ad-display 处显示一个空白点。不显示任何结果。我希望它显示 search_ads 函数的结果。它看起来像这样:
function search_ads($search_string = null, $type, $min_price = null, $max_price = null) {
global $db;
global $config;
switch($type) {
case "text":
$search = 'text LIKE "%'. $search_string .'%"';
break;
case "price":
$search = 'price > '. $min_price .' <= '. $max_price .'';
break;
case "headline":
$search = 'title LIKE "%'. $search_string .'%"';
break;
default:
$search = 'price BETWEEN '. $min_price .' AND '. $max_price .'';
break;
}
$row = $db->dbh->query('SELECT ad.*, (SELECT img.image FROM '.$config->db_prefix.'_images img WHERE img.aid = ad.aid LIMIT 1) AS img FROM '.$config->db_prefix.'_adverts ad WHERE '. $search .' AND ad.approved = 1');
$row->setFetchMode(PDO::FETCH_ASSOC);
return $row;
}
我创建了这个小提琴来展示我要实现的目标。这有点不同,但表明了我的意图
解决方案:
$search = 'price > '. $min_price .' AND price <= '. $max_price .'';