dqr3883 2013-12-01 20:27
浏览 54
已采纳

如何在smarty中访问php while循环变量

I have completed a php page with my poor logic but working fine... i want to access data of that page into smarty page. Page name id dealers.php and theme page name is dealers.html

I want to access while loop data into 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));
}

it mean i am storing data from database in variables... so i want to use this variables into smarty template file. How to assign while loop and how to get while loop variables in smarty ...?

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

dealers.html {$dealer_name}

It showing only one name one time. Need to get all dealers from database using above php logic. Just tell me how to assccess these variables in dealers.html

  • 写回答

1条回答 默认 最新

  • dongruan6001 2013-12-01 21:56
    关注

    The best way to do this is to assign it into an array and then loop through your array

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

    And then in your Smarty template (note, this syntax assumes Smarty 3 or later)

    {foreach $users as $user}
        <div>{$user.contact_name} - {$user.contact_name}</div>
    {/foreach}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line