user3055272
user3055272

Reputation: 3

How to access php while loop variables in smarty

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

Upvotes: 0

Views: 544

Answers (1)

Machavity
Machavity

Reputation: 31624

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}

Upvotes: 1

Related Questions