我正在为我的网站创建一个评论系统,我正在将评论链接到文章ID。每个注释在增量顺序中都有自己的ID,但不同的注释可以具有相同的文章ID。
以下是我的模板表单上抛出错误的区域:
<h1>Comments</h1>
<ul id="headlines" class="archive">
<?php foreach ($craps['comments'] as $comment ) { ?>
<li>
<h2>
<span class="pubDate"><?php echo date('j F Y', $comment->publicationDate)?></span>
</h2>
<p class="summary">
<?php echo htmlspecialchars( $comment->content )?>
</p>
</li>
<?php } ?>
</ul>
这是我的索引页面的一部分,其中包含变量的设置,例如php for循环使用的数组和数据:
function viewArticle() {
if ( !isset($_GET["articleId"]) || !$_GET["articleId"] ) {
homepage();
return;
}
$results = array();
$results['article'] = Article::getById( (int)$_GET["articleId"] );
$results['pageTitle'] = $results['article']->title . " | Gaming News";
$craps = array();
$data = Comment::getList( (int)$_GET["articleId"]);
$craps['comments'] = $data['craps'];
require( TEMPLATE_PATH . "/viewArticle.php" );
}
这是系统从数据库中提取数据的位置(它在我的Comment.php中):
public static function getList( $art, $order="publicationDate DESC", $numRows=10000 ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM comments WHERE articleid = :art
ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows";
$st = $conn->prepare( $sql );
$st->bindValue( ":art", $art, PDO::PARAM_INT );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$comment = new Comment( $row );
$list[] = $comment;
}
}
我真的很感激帮助。这是显示测试系统中的注释之前的最后一个错误。
这是新的获取列表代码:
public static function getList( $art, $order="publicationDate DESC", $numRows=10000 ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM comments WHERE articleid = :art ORDER BY :order LIMIT :numRows";
$st = $conn->prepare( $sql );
$st->bindValue( ":art", $art, PDO::PARAM_INT );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->bindValue( ":order", $order, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$comment = new Comment( $row );
$list[] = $comment;
}
return $list;
}