如何在脚本中将文件php添加到tpl

时间:2015-06-02 11:52:17

标签: php sql mysqli smarty

我想把php文件放到tpl 以下代码

<?php
$servername = "localhost";
$username = "1_1";
$password = "1=~Eh]V";
$dbname = "11_11";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT domain FROM insights_base WHERE domain_1 = current_date() ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table><tr><th>Domain Name</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr>

<td>" . $row["domain"]. " </td>

         </tr>";
     }
     echo "</table>";
} else {
     echo "0 results";
}`enter code here`
$conn->close();
?>  

文件路径

require_once $ current_dir。 &#39; /app.config.php' ;;

require_once $ current_dir。 &#39; /lib/smarty/Smarty.class.php' ;;

require_once $ current_dir。 &#39; /lib/classes/Database.singleton.php' ;;

require_once $ current_dir。 &#39; /function_core.php' ;;

1 个答案:

答案 0 :(得分:0)

的index.php:

<?php

require 'libs/Smarty.class.php';

$servername = "localhost";
$username = "1_1";
$password = "1=~Eh]V";
$dbname = "11_11";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT domain FROM insights_base WHERE domain_1 = current_date() ";
$result = $conn->query($sql);
$data = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
       $data[] = $row;
    }
}
$conn->close();

$smarty = new Smarty;
$smarty->assign('data', $data);
$smarty->display('index.tpl');

在index.tpl:

<table>
    <tr><th>Domain Name</th></tr>
    {foreach from=$data key=k item=i}
        <tr>
            <td>{$i.domain}</td>
        </tr>
    {/foreach}
</table>

您可以找到更多信息和示例here