Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:25:40+00:00 2026-06-10T01:25:40+00:00

Im using the component K2, and it’s voting/rating system. Currently it displays the rating

  • 0

Im using the component K2, and it’s voting/rating system. Currently it displays the rating as a percentage, with some css to view stars. But instead of showing the stars, I want it to say for example, 4.5/5

This is the code for viewing it:

<?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; ?>

And this is the code that found in ‘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;
}

What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:25:42+00:00Added an answer on June 10, 2026 at 1:25 am

    In summary, take the percentage shown in: $this->item->votingPercentage ( 0 to 100) and convert it to a number in the range of 0 to 5 by dividing it by 20. You’ll probably want to keep 1 decimal place of accuracy when displaying the number as x out of 5.

    Since you don’t know whether votingPercentage is a string or a numeric, I would test it to make sure it has a valid numeric value before doing any calculations:

    <?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)
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on an color-picker component using HTML, CSS and Javascript, one major
I have built a custom component using some containers and a TileList. Now when
I am using IcePDF component to view pdf in JApplet on my web page.
I am using auth component and it works ok. But in my default layout
I'm using ADOdataset component to view an Excel Spreadsheet on a Delphi form. The
Hey I am currently using a component in which I need to load a
I am using JQGrid component for displaying ajax tree grid. With some quantity of
I'm using a component written in JavaScript. The component is exposing some events. The
I am using Auth Component. I am setting $this->Auth->loginError in the beforeFilter. But i
I am currently using jquery-ui-1.8.10 I am using the autocomplete component and cannot find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.