簡體   English   中英

如何在Smarty中訪問PHP While循環變量

[英]How to access php while loop variables in smarty

我已經用不好的邏輯完成了一個php頁面,但是工作得很好...我想將該頁面的數據訪問到smarty頁面中。 網頁名稱ID為Dealers.php,主題網頁名稱為Dealers.html

我想在循環數據到smarty時訪問

$q1=mysql_query("select * from class_users where active=1 and store=1 ORDER BY rand() limit 8");

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-zds-]+~u', '',  strtolower($dname));
}

這意味着我正在將數據庫中的數據存儲在變量中...所以我想使用此變量到smarty模板文件中。 如何在Smarty中分配while循環以及如何獲取while循環變量?

Dealer.php $ smarty-> assign(dealer_name,$ dname);

Dealers.html {$ dealer_name}

它一次只顯示一個名字。 需要使用上述php邏輯從數據庫中獲取所有經銷商。 請告訴我如何在Dealers.html中訪問這些變量

最好的方法是將其分配到array ,然后遍歷數組

$data = array();
while ($ev= mysql_fetch_assoc($q1)){
    $data[] = $ev;
}
$smarty->assign('users', $data);

然后在您的Smarty模板中(請注意,此語法假定使用Smarty 3或更高版本)

{foreach $users as $user}
    <div>{$user.contact_name} - {$user.contact_name}</div>
{/foreach}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM