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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里