此代码可以帮助我显示存储在评论数组中的数据,但每当我尝试单独调用变量时,我都会收到错误。那我怎么能单独显示变量呢。
<?php $reviews = Mage::getModel('review/review')->getResourceCollection();
$reviews->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes()
->load();
print_r($reviews->getData());
这是我收到错误的行:
echo $reviews->getTitle();
?>
错误是致命错误:调用未定义的函数。请帮忙。
答案 0 :(得分:0)
我从未使用过Magento,但我认为你必须遍历评论。试试这个。
<?php
$reviews = Mage::getModel('review/review')->getResourceCollection()
->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes();
if (count($reviews) > 0) {
foreach ($reviews->getItems() as $review) {
// Individual review
echo $review->getTitle();
}
}
?>
答案 1 :(得分:0)
$ReviewsCollection = $this->getReviewsCollection()->getItems();
if( count( $ReviewsCollection ) ) {
$x = 1;
foreach( $ReviewsCollection as $_review ){
// get review title
echo $_review->getTitle();
$_votes = $_review->getRatingVotes();
if (count($_votes)){
foreach ($_votes as $_vote){
echo $_vote->getRatingCode().' - '.$_vote->getPercent().'%<br />';
}
}
// get review date
echo $this->formatDate($_review->getCreatedAt()).'<br />';
// get review detail
echo $_review->getDetail().'<br />';
// get how wrote the review
echo $_review->getNickname().'<br />';
// because you cant setPageSize with this collection it's best to break the loop.
if($x == 4){ break; }
$x++;
}
}