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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据