0

と呼ばれるphpファイルがありますtestfun.php

<?php
$conn=mysql_connect("localhost","root","") or die("unabke to connect");
$db=mysql_select_db("smartyform",$conn) or die("databse error");

require 'Smarty/libs/Smarty.class.php';

$smarty = new Smarty;

$sel=mysql_query("select * from form"); 
while($row=mysql_fetch_array($sel))
{
$id=$row[0];
$name=$row[1];
}
$smarty->assign('id',$id);
$smarty->assign('name',$name);

$smarty->display('testfunction.tpl');
    ?>

と呼ばれるtplファイルがありますtestfunction.tpl

<body>

<ul>
{$id}
 :
 {$name}
</ul>

</body>
</html>

を実行するtestfun.phpと、次の出力が得られました。

16 : dg 

しかし、次のような出力が必要です。

1:d
d:g

私は何をすべきか ?

4

1 に答える 1

0

データを配列として smarty に渡す必要があります。したがって、PHP では次のようにする必要があります。

while($row=mysql_fetch_array($sel))
{
  $data[] = array(
    "id" => $row[0],
    "name" => $row[1]
  );
}

$smarty->assign("data", $data);

次に、Smarty テンプレートでforeachを使用してアイテムをリストする必要があります。

{foreach $data as $item}
  {$item.id}:{$item.name}
{/foreach}
于 2013-05-15T11:25:55.673 に答える