2016-07-01 5 views
-1

im Smarty newbee。データベースから一部のデータを表示しようとしましたが、ブラウザのページに何も表示されません。私はPHPファイルでクエリを表示しようとしたが、すべて正常に動作しますが、私はSmartyで表示できません。ここに私のコード: New.phpDBからのデータを表示、スマート

<?php 
include 'Smarty/libs/Smarty.class.php'; 
$link = mysqli_connect ('localhost','xxxx','xxxx','xxxx'); 

$sql = "SELECT * FROM `s_news`"; 
$result = mysqli_query($link,$sql); 

while($row = mysqli_fetch_assoc($result)) { 
$data[] = $row; # $data is the array created for use in the Smarty template. 
$smarty->assign('data', $data); 
$smarty->display('text.tpl'); 
} 
?> 

Text.tplファイル:

{foreach from=$cat1 item=item} 
<table cellpadding="2" cellspacing="0"> 
<tr> 
<td>Username:</td> 
</tr> 
{foreach from=$data item=item key=key} 
<tr> 
<td>{$item.date}</td> 
<tr> 
{/foreach} 
</table> 
{/foreach} 

ありがとう!

+0

ループ外に割り当てる必要があります。 –

+0

@ YourCommonSense pls answer。私はあなたが言ったことを想像することはできません。 –

答えて

1

あなたはwhileループの中でテンプレートを割り当てて表示しようとしています。すべてのデータが収集された後に、外部に行ってください。

while($row = mysqli_fetch_assoc($result)){ 
$data[] = $row; # $data is the array created for use in the Smarty template. 
} 
$smarty->assign('data', $data); 
$smarty->display('text.tpl'); 
+0

、動作していない、mb私はいくつかの他のライブラリを含める必要がありますか?あなたはどのように思いますか? –

+0

それがあなたのコードであれば、$ smarty = new Smarty()で$ smartyも初期化する必要があります。マニュアルを見てください:http://www.smarty.net/docs/en/installing.smarty.basic.tpl – Borgtex

+0

私はあなたの指示に従います。 mysqliクエリのvar_dumpを試して、私は情報を得るが、私はtext.tplファイルで間違いだと思う。 text.tplが正しいことを確認してください。表示する列の名前は 'date'と 'news'です –

関連する問題