我已經坐在3天試圖弄清楚Smarty的,但我並不感到很smartyer :) 這裏是我的PHP代碼:任何人都可以幫我在smarty中顯示這個php代碼嗎?
somefile.php
include('libs/Smarty.class.php');
require("configs/config.php");
// create object
$smarty = new Smarty;
function homepage($params, $smarty, $numRows=1000000, $order="publicationDate DESC") {
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows";
$st = $conn->prepare($sql);
$st->bindValue(":numRows", $numRows, PDO::PARAM_INT);
$st->execute();
$list = array();
while ($row = $st->fetch()) {
$article = new Article($row);
$list[] = $article;
}
// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query($sql)->fetch();
$conn = null;
return (array ("results" => $list, "totalRows" => $totalRows[0]));
if (isset($data['id'])) $this->id = (int) $data['id'];
if (isset($data['publicationDate'])) $this->publicationDate = (int) $data['publicationDate'];
if (isset($data['title'])) $this->title = preg_replace ("/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title']);
if (isset($data['summary'])) $this->summary = preg_replace ("/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['summary']);
if (isset($data['content'])) $this->content = $data['content'];
$results = array();
$results['articles'] = $data['articles'];
$results['totalRows'] = $data['totalRows'];
$smarty->assign("articles", $data['articles']);
$smarty->assign("totalRows", $data['totalRows']);
$smarty->assign("id", $data['id']);
$smarty->assign("publicationDate", $data['publicationDate']);
$smarty->assign("title", 'kkkkkkkkkkkkkkkkkkkkk');
$smarty->assign("summary", 'lllllllllllllllllllllll');
$smarty->assign("content", 'øøøøøøøøøøøøøøøøøøøøøøøøøøøøøø');
}
// display it
$smarty->display('index2.tpl');
索引2。 TPL
{include file="header.tpl"}
{debug}
<h1>Article Archive</h1>
{assign var=results value={cycle values="totalRows,articles,title,publicationDate,id"}}
<ul id="headlines" class="archive">
{foreach $articles['articles'] as $article}
<li>
<h2>
<span class="pubDate">{$publicationDate}</span><a href=".?action=viewArticle&articleId={$id}">{$title}</a>
</h2>
<p class="summary">{$summary}</p>
</li>
{/foreach}
</ul>
<p><a href="./archive.php">Articles Archive</a></p>
{include file="footer.tpl"}
我得到的錯誤是:
Notice: Undefined index: articles in /var/www/www.mypage.com/htdocs/smarty/templates_c/cd9ff0835daf703adc0ed3991c1d26021d9fcc09.file.index2.tpl.php on line 42
Notice: Trying to get property of non-object in /var/www/www.mypage.com/htdocs/smarty/templates_c/cd9ff0835daf703adc0ed3991c1d26021d9fcc09.file.index2.tpl.php on line 42
'$ data ['articles']'你的代碼中不存在,但是你使用它。你有id,publicationDate,標題,摘要和內容。沒有其他的。 –
我有:$ results ['articles'] = $ data ['articles'];在結果數組中,它來自sql代碼中的「while」命令。我對PHP和smarty都是全新的,所以希望你能和我一起裸露。 :) – user3383761
我也試圖改變第文章,數據結果,但還是同樣的錯誤 – user3383761