【问题标题】:Joomla Rating, intead of showing % i want it to view '8.2/10'?Joomla 评级,而不是显示 % 我希望它查看“8.2/10”?
【发布时间】:2012-08-03 09:35:35
【问题描述】:

我正在使用组件 K2,它是投票/评级系统。目前它以百分比形式显示评分,并带有一些 CSS 来查看星星。但我不想显示星星,而是希望它说,例如,4.5/5

查看代码如下:

<?php if($this->item->params->get('catItemRating')): ?>
<div id="catItemRatingBlock">
 <div class="itemRatingForm">
   <ul class="itemRatingList">
     <li class="itemCurrentRating" id="itemCurrentRating<?php echo $this->item->id; ?>" style="width:<?php echo $this->item->votingPercentage; ?>%;"></li>
     <li><a href="#" rel="<?php echo $this->item->id;  ?>" class="one-star">1</a></li>
     <li><a href="#" rel="<?php echo $this->item->id;  ?>" class="two-stars">2</a></li>
     <li><a href="#" rel="<?php echo $this->item->id;  ?>" class="three-stars">3</a></li>
     <li><a href="#" rel="<?php echo $this->item->id;  ?>" class="four-stars">4</a></li>
     <li><a href="#" rel="<?php echo $this->item->id;  ?>" class="five-stars">5</a></li>
   </ul>
 </div>
</div>
<?php endif; ?>

这是在 'com_k2/models/item.php' 中找到的代码:

function getVotesPercentage($itemID = NULL)
{

    $mainframe = &JFactory::getApplication();
    $user = JFactory::getUser();
    $db = &JFactory::getDBO();
    $xhr = false;
    $result = 0;
    if (is_null($itemID))
    {

        $itemID = JRequest::getInt('itemID');
        $xhr = true;
    }

    $vote = K2ModelItem::getRating($itemID);

    if (!is_null($vote) && $vote->rating_count != 0)
    {
        $result = number_format(intval($vote->rating_sum) / intval($vote->rating_count), 2) * 20;
    }
    if ($xhr)
    {
        echo $result;
        $mainframe->close();
    }
    else
        return $result;
}

我该怎么办?

【问题讨论】:

  • 当您在 K2 中编辑文章时,它们会显示您在后端描述的文章评分。右侧边栏显示为(average rating: 5.00/5.00)。所以看看他们在那里使用的代码。

标签: php joomla rating-system joomla-k2


【解决方案1】:

总而言之,取 $this->item->votingPercentage(0 到 100)中显示的百分比,然后除以 20 将其转换为 0 到 5 范围内的数字. 将数字显示为 5 中的 x 时,您可能希望保持 1 位小数的准确性。

由于您不知道votingPercentage 是字符串还是数字,我会在进行任何计算之前对其进行测试以确保它具有有效的数值:

<?php

if (!isnumeric($this->item->votingPercentage) {

   $numericRating = 0;

// If the value cannot be interpreted as a numeric, we assume a value of 0.

}

else {

   $numericRating = round(($this->item->votingPercentage / 2), 0);  

// The statement above is equivalent to dividing the percentage by 20 (to get a number from 0 to 5); and then multiplying by 10 to get a number from 0 to 50.  Since this final number could have a decimal component (for example 42.55 out of 50), we round to 0 decimal places to get rid of the decimal remainder - in this example, resulting in the number 43

   $numericRating = round(($numericRating / 10), 1);

// The statement above divides the number from the previous step by 10 (to shift the decimal point), and just for good measure, applies the rounding function one more time in case the division yields a number like 4.300000001 or 4.29999999

}

echo $numericRating . "/5";

// Finally, the resulting numeric value is rendered as a number from 0 to 5 with 1 decimal place, followed by the characters "/5"  (i.e. out of 5)
?>

【讨论】:

  • 快点打败我吧。打得好,这是一个非常彻底的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多