DEV Community

Cover image for A Lightweight 5-Star Rating System for PHP
Em leons
Em leons

Posted on

A Lightweight 5-Star Rating System for PHP

Want to add 5-star ratings to your website or app without bloated dependencies or framework lock-in? Meet sim-rating — a simple, customizable PHP library for calculating and rendering 5-star ratings in multiple formats.

Why Choose sim-rating?

  • Works with any php Framework – Use it with plain PHP, Laravel, CodeIgniter, Symfony, or any PHP setup.
  • Lightweight – Just clean, efficient PHP.
  • Fully Customization – Adjust colors, styles, formats, and more.
  • Built-in Smart Logic – Auto-calculate totals, averages, and percentage distributions.

Getting Started

Installation

composer require emleons/sim-rating
Enter fullscreen mode Exit fullscreen mode

Basic Example

require('vendor/autoload.php');
use Emleons\SimRating\Rating;

$ratings = [
    'one_star' => 10,
    'two_star' => 20,
    'three_star' => 30,
    'four_star' => 40,
    'five_star' => 50
];

$rating = new Rating($ratings);

echo $rating->render('html');  // Outputs HTML star rating
echo $rating->render('json');  // Outputs JSON
Enter fullscreen mode Exit fullscreen mode

🎨 Customization Options

echo $rating->render('html', [
    'type' => 'bars',              // 'stars' or 'bars'
    'color' => '#4a90e2',          // Custom color
    'bar_height' => '25px',
    'show_summary' => true
]);
Enter fullscreen mode Exit fullscreen mode

✨ Features at a Glance

1. Multiple Display Modes

  • Stars (default)
  • Bars (horizontal progress bars)
  • JSON (great for APIs)

2. 🧠 Built-In Rating Calculations

$rating->getAverage();       // 3.67
$rating->getTotal();         // 150
$rating->getDistribution();  // ['five_star' => 33.33, ...]
Enter fullscreen mode Exit fullscreen mode

3. 🧩 Framework-Friendly

Laravel Blade

@php
    $rating = new \Emleons\SimRating\Rating($product->ratings);
@endphp

{!! $rating->render() !!}
Enter fullscreen mode Exit fullscreen mode

CodeIgniter 4

// Controller
$rating = new \Emleons\SimRating\Rating($product['ratings']);
return view('product_view', ['rating' => $rating->render()]);

// View
<?= $rating ?>
Enter fullscreen mode Exit fullscreen mode

⚙️ Advanced Usage

🛠️ Custom Template Support

echo $rating->render('html', [
    'template' => 'views/custom-rating-template.php'
]);
Enter fullscreen mode Exit fullscreen mode

🌟 Enable Interactive Ratings

echo $rating->render('html', [
    'interactive' => true  // Adds clickable stars (requires JS)
]);
Enter fullscreen mode Exit fullscreen mode

🔗 Useful Links


🎁 Tip: Whether you’re building an e-commerce store, course platform, or review website, sim-rating helps you show user feedback with clean, minimal code.

Top comments (3)

Collapse
 
xwero profile image
david duymelinck • Edited

While I think it is a good effort, there are a few things that could change.

  • Remove the vendor folder from the library and add the dependencies in the composer.json file. This will make it easier to prevent version conflicts.

  • Move html and css out of the HtmlRenderer this will make it easier to overwrite those assets.

  • The SVGRenderer doesn't render an svg, it returns json. I would remove that renderer.

  • Use data objects and enums to move data around and identify parts of the functionality. It will give the library a better foundation.

Collapse
 
emleons profile image
Em leons

Thanks I will update.

Collapse
 
database0001 profile image
Database

Good work but javascript invented im sorry.