Smarty + PHP + mySQL评论系统不显示数据

时间:2018-02-19 01:10:02

标签: php mysql smarty

我正在尝试从我的评论表中的所有提交的评论中检索“姓名,电子邮件和消息”列,并将其显示在页面上,但不显示任何内容。这很奇怪,因为我的博客帖子几乎完全相同,而且工作正常。

php code

$sql = "SELECT name, email, message FROM comments"; 
$result = mysqli_query($db_server, $sql); 
while($row = mysqli_fetch_assoc($result)) 
{ 
$data[] = $row;
} 
$tpl->assign('data', $data); 
$tpl->display('comments.tpl'); 

html

<table>
    {if $signedin} 
        <tr> 
            <td>Username:</td> 
        </tr> 
        {foreach from=$data item=item key=key} 
        <tr> 
            <td>{$item.name}</td> 
            <td>{$item.email}</td>
            <td>{$item.message}</td>
        <tr> 
        {/foreach} 
    {else}
    Sign in to view comments.
    {/if}
    </table>

我做错了什么?

编辑:从recipes.php添加工作代码

require_once 'config.php';

session_start();

include 'is-signed-in.php';

$data = array(); //initalize a container
    $query = "SELECT * from recipes";
    $result = mysqli_query($db_server, $query);

    if ($result->num_rows > 0) {
        //fetch rows
        while($row = $result->fetch_assoc()) {
        $data[] = $row; //push the array inside the row
        }
    }
    $tpl->assign('values', $data);

// Display template
$tpl->display('myrecipes.tpl');

recipes.tpl

{if $signedin}


{foreach from=$values key=k item=value}
    <p>{$value.user_id_fk}</p>
    <a href="<?php echo url_for('recipes-page.php'); ?>"><h2>{$value.title}</h2></a>
    <p>Date: {$value.submission_date}</p>
    <p>{$value.instructions}</p>
    <p>Category: {$value.category}</p>
    <form action="" method="post">
    <a href="{$delete}">Delete</a>
{/foreach}
{else}
 You are currently not signed in.
{/if}

1 个答案:

答案 0 :(得分:1)

显然我的问题出在我的.tpl中,没有任何内容显示在表格中。它真的应该,但我确实在h1元素中有一个迷路锚标签。所以这解决了这个问题。

 <h1>Comments</h1>
            {foreach from=$data item=item key=key} 
            <div> 
                <p>Name: {$item.name}</p> 
                <p>Email: {$item.email}</p>
                <p>Message: {$item.message}</p>
            </div> 
            {/foreach}