2013-12-12 122 views
0

使用realestate script 3其中使用smarty 3Smarty php变量输出

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

<?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文件夹中,并在脚本和3个变量$ title,$ price,$ picture结尾处返回TEST echo。在模板{} my_plugin

使用命令,但我想有机会获得这些变量,所以我可以在* .tpl文件调用它们,例如:{$标题}

这样我可以把HTML部分英寸tpl文件,只需从该函数插入我需要的变量。

它应该环路10个结果模板文件;)

回答

0

理论上你的代码已经分配给naslov智者变量的最后冠军。 如果您可以访问所有人,您可以将其更改为追加。 这样理论上在拨打{my_plugin}后可以访问{$naslov}变量中的标题。

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

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