2014-03-05 52 views
-2

我已經坐在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&amp;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 
+4

'$ data ['articles']'你的代碼中不存在,但是你使用它。你有id,publicationDate,標題,摘要和內容。沒有其他的。 –

+0

我有:$ results ['articles'] = $ data ['articles'];在結果數組中,它來自sql代碼中的「while」命令。我對PHP和smarty都是全新的,所以希望你能和我一起裸露。 :) – user3383761

+0

我也試圖改變第文章,數據結果,但還是同樣的錯誤 – user3383761

回答

0

我覺得現在的問題是,由於某種原因,你認爲當你做

$smarty->assign("articles", $data['articles']); 

他們重點「的文章」被包括在內,至極不是,只是它的內容。所以沒有這樣的$ articles ['articles']傳遞給模板,同樣沒有$ id ['id'],只有$ id。嘗試使用$ articles來做foreach:

{foreach $articles as $article} 
+0

是的,我也認爲這是問題。代碼看起來像這樣在原始的工作PHP文件中:'<?php foreach ($ results ['articles'] as $ article){?>這裏的原始文章中的SQL,WHILE,IF ISSET和RESULTS ARRAY -snippets也來自正在運行的phpcode。 – user3383761

相關問題