任何人都可以帮我在smarty中显示这个PHP代码吗?

时间:2014-03-05 14:08:39

标签: php smarty

我已经坐了3天试图找出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');

index2.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

1 个答案:

答案 0 :(得分:0)

我认为问题在于,出于某种原因,你认为当你做

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

他们的关键'文章'包括在内,不是,只有它的内容。所以没有这样的$ articles ['articles']传递给模板,同样没有$ id ['id'],只有$ id。尝试用$ articles文章来做foreach:

{foreach $articles as $article}