Search code examples
phpsqlmysqlismarty

how to add file php to tpl in scripts


I want to put php file to tpl The following code

<?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();
?>  

Files path

require_once $current_dir . '/app.config.php';

require_once $current_dir . '/lib/smarty/Smarty.class.php';

require_once $current_dir . '/lib/classes/Database.singleton.php';

require_once $current_dir . '/function_core.php';


Solution

  • 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>
    

    More information and examples you can find here.