-2

我想在 smarty 模板文件中运行 mysql 查询.......

文件扩展名为 .html

每件事都运行良好只是我想运行一个不完整的查询,如果没有 smarty 变量

例如 $q1 = mysql_query("select *from class_users where id={$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>
4

2 回答 2

0

您可以使用{php}标签在 Smarty 模板中嵌入 PHP 代码。请注意,这是一个已弃用的功能(请参阅链接),并且可能会从未来的版本中删除。它还破坏了 Smarty 作为旨在将表示与应用程序逻辑分离的模板语言的实际目的。

话虽这么说,如果您仍然想这样做,以下可能有效(但未经测试):

{php}
$v = $this->getVariable('v');
print_r($v['user_id']);
{/php}
于 2013-06-02T14:49:04.340 回答
0

$v 可能是从呈现模板的 PHP 代码中设置的,在 PHP 文件中使用您的数据库查询,而不是在模板文件中,它们最终是 TEMPLATE 文件!

于 2013-10-15T14:17:13.250 回答