2013-06-02 32 views
-2

我想運行智者模板文件MySQL查詢.......如何智者可變輸出存儲到PHP變量,完成MySQL查詢

文件擴展名是

每一件事運行的.html罰款只是我想運行一個查詢是不完整的,沒有smarty變量

例如 $ q1 = mysql_query(「select * from class_users where class = {$ v.user_id}」);不工作

我想將{$ v.user_id}存儲到php變量中,例如$ var =「{$ v.user_id}」;

<?php 
$connect = mysql_connect("localhost","***","****") or die('error connecting: ' . mysql_error()); 
mysql_select_db("****") or die('error selecting db: ' . mysql_error()); //select database 
?> 

<div class="classified{if $v.highlited && $ads_settings.enable_highlited} highlited{/if} {if $v.featured && $ads_settings.enable_featured} featured{/if}" {if $v.highlited && $ads_settings.enable_highlited}style='background: {$ads_settings.highlited_color};'{/if}> 

{if $v.featured && $ads_settings.enable_featured}<div class="featured-icon"></div>{/if} 

{capture name=some_content assign=details_url} 
{if $seo_settings.enable_mod_rewrite}{seo->makeDetailsLink p1=`$v.id` p2=`$v.title`}{else}{$live_site}/details.php?id={$v.id}{/if} 
{/capture} 
<div style="margin-left: {$ads_settings.thmb_width+10}px;"> 
<div> 
     <div class="classified_photo" style="width: {$ads_settings.thmb_width+10}px; margin-left: -{$ads_settings.thmb_width+10}px;"> 
    <a href="{$details_url}" name="listing{$v.id}"> 
    <img src="{$live_site}/{$v.image}" class="pic" id="pic{$v.id}" onmouseover="this.className='pic_over'" onmouseout="this.className='pic'" alt="{if $v.image_id}{$v.title|strip_tags:false|substr:0:100}{/if}" /> 
    </a> 
    {if $v.rented && $ads_settings.enable_rented}<div class="rented" id="rented{$v.id}" style="display: none;"></div>{elseif $v.sold && $ads_settings.enable_sold}<div class="sold" id="sold{$v.id}" style="display: none;"></div>{/if} 
     </div> 

     <div class="classified_content"> 
<h3><a href="{$details_url}" id="title{$v.id}">{$v.title|wordwrap:70:" ":true}</a></h3> 
{if $data_set=="cars"}<div class="listing_make">{$v.make} {$v.model}</div>{/if} 
{if $ads_settings.enable_price && $v.price>=0}<span class="price">{$v.price_curr}&nbsp;</span>{/if} 
{if $v.location_str}<span class="location">{if $v.location_str && $ads_settings.enable_price && $v.price>=0} | {/if}{$v.location_str}</span>{/if}{if $v.location_str || ($ads_settings.enable_price && $v.price>=0)}<br/>{/if} 
<div class="small">{if $ads_settings.show_ad_date_for_everybody==1}{$lng.listings.added_on}&nbsp;{$v.date_nice}{else}{$lng.listings.posted}{/if}&nbsp;{$lng.general.to}&nbsp;<u>{$v.category}</u></div> 
<p>{$v.description|truncate:200:"...":false|wordwrap:70:" ":true}</p> 

<span class="classified_links"> 

{* if listings_compare *} 
{if in_array("listings_compare", $modules_array)} 

<a href="javascript:;" class="addcmpwin" id="cmp{$v.id}" {if in_array($v.id, $cmp)}style="display: none;"{/if}>{$lng_compare.add_to_compare}</a>&nbsp; 

<a href="javascript:;" class="remcmpwin" id="remcmp{$v.id}" {if !in_array($v.id, $cmp)}style="display: none;"{/if}>{$lng_compare.rem_compare}</a>&nbsp; 

{/if} 
{* end if listings_compare *} 

<a href="javascript:;" class="fshare" id="fshare{$v.id}">{$lng.listings.recommend_this}</a> 

&nbsp;<a href="javascript:;" {if $self_noext=="favorites" || in_array($v.id, $fav_array)}style="display: none;"{/if} class="addtofav" id="fav{$v.id}">{$lng.listings.add_to_favourites}</a> 
<a href="javascript:;" {if $self_noext!="favorites" && !in_array($v.id, $fav_array)}style="display: none;"{/if} class="remfav" id="remfav{$v.id}">{$lng.listings.remove_favourite}</a> 

&nbsp;<a href="{$details_url}">{$lng.listings.details}</a>&nbsp; 

{if ($logged_in || !$ads_settings.hide_contact_when_not_logged) && (!$v.sold || !$ads_settings.hide_contact_when_sold) && (!$v.rented || !$ads_settings.hide_contact_when_rented)}&nbsp;<a href="javascript:;" class="fmailto" id="mailto{$v.user_id}_{$v.id}">{$lng.general.contact}</a>{/if} 
</span> 

{if ($v.priority_name && $ads_settings.enable_priorities) || ($v.video && $v.enable_video && $ads_settings.enable_video)} 
    <table class="extra" cellpadding="0" cellspacing="0"> 
    <tr> 
    {if $v.priority_name && $ads_settings.enable_priorities} 
    <td><div class="buttonwrapper"><div class="priority-left"><div class="priority-right">{$v.priority_name}</div></div></div></td><td>&nbsp;</td> 
    {/if} 

    {if $v.video && $v.enable_video && $ads_settings.enable_video} 
    <td><div class="buttonwrapper"><div class="video-left"><div class="video-right">{$lng.listings.short_video}</div></div></div></td> 
    {/if} 
    </tr> 
    </table> 
{/if} 
<div> 
**<?php 


$q1=mysql_query("select * from class_users where id={$v.user_id}"); 


while ($ev= mysql_fetch_object($q1)){ 
$did = "$ev->id"; 
$dname = "$ev->contact_name"; 
$drating  = "$ev->rating"; 
$rating = round($drating); 
$dnorating = "$ev->no_ratings"; 
$dphoto = "$ev->photo"; 
$dc = "$ev->company_name"; 
$store_banner = "$ev->store_banner"; 
$durl = preg_replace('~[^A-Za-z\d\s-]+~u', '', strtolower($dname)); 
$durl = str_replace(" ", "-", $durl); 
$durls = "http://dealerspk.com/$did-$durl/store.html"; 
$dname = substr($dname, 0, 25); 
$dcompany = substr($dc, 0, 30); 
if ($photo){ 
$pict = "http://tatabata.com/uploads/photo/$photo"; 
}else{ 
$pict = "http://dealerspk.com/images/noimage.jpg"; 
} 
} 
?>** 
</div> 
    </div><!-- end: classified_content --> 
     </div> 
    </div> 
</div> 
+2

爲什麼要在模板文件中運行邏輯?是不是Smarty的目的是擺脫HTML行之間的邏輯(條件語句和循環語句除外)? – Fons

回答

0

您可以在Smarty模板使用{php}標籤embed PHP code。請注意,這是棄用功能,雖然(請參閱鏈接),並可能從未來的版本中刪除。它也破壞了Smarty作爲旨在將演示文稿與應用程序邏輯分離的模板語言的實際目的。

話雖這麼說,如果你仍然想做到這一點,下面可能工作(未經測試,雖然):

{php} 
$v = $this->getVariable('v'); 
print_r($v['user_id']); 
{/php} 
0

$ V可能是從渲染模板 使用數據庫的PHP代碼中設置在PHP文件中查詢不在模板文件 中,它們到底是模板文件!