Smarty php变量输出

时间:2013-12-12 15:50:36

标签: php smarty

使用realestate script 3使用smarty 3

我设法创建了从数据库中获取信息的循环。

<?php
function smarty_function_my_plugin($params,&$smarty)
{
    $con=mysqli_connect("localhost","root","","res3");

    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    } else {
       $result = mysqli_query($con,"SELECT * FROM res3_listings WHERE listing_type_id=5 ORDER BY views DESC");
       while($row = mysqli_fetch_array($result)) {
           $title=$row['title_1'];
           $price=$row['price'] ;
           $id=$row['listing_id'];
           $result = mysqli_query($con,"SELECT listing_photo_file FROM res3_listing_photos WHERE listing_photo_id=1");
           while($row = mysqli_fetch_array($result)) {
              $picture=$row['listing_photo_file'];
           }
           echo $title. "<br/>".$price."<br/>".$picture;
           $smarty->assign('naslov', $title);
           echo "<br>";
       }
    }

    mysqli_close($con);
    echo '<h1>Test</h1>';
}
?>

所以我将脚本放在plugins文件夹中并在脚本结束时返回TEST echo和3个变量$ title,$ price,$ picture。使用模板{my_plugin}

中的命令

但是我想访问这些变量,所以我可以在* .tpl文件中调用它们,例如:{$ title}

这样我可以将HTML部分放在.tpl文件中,只需从该函数中插入我需要的变量。

它应该在模板文件上循环10个结果;)

1 个答案:

答案 0 :(得分:0)

理论上,您的代码已将最后一个标题分配给naslov smarty变量。 如果要访问所有这些,可以将其更改为追加。 这种方式理论上可以在{my_plugin}调用后访问{$naslov}变量中的标题。

或者您可以使用$smarty->fetch('othertemplate.tpl')在模板中返回。您可以在othertemplate.tpl中使用这些变量。

要将结果限制为最多10,可能是将limit 10附加到数据库查询的最简单方法。