Software development, photography, jokes, ....

noyb-logo

Sites by me

 
sds82-logo
tas-logoTransportation Administration System
snoezelkussen-logo-kleinstSnoezelen Pillows for Dementia
ikzoekeenbegeleider-logoBegeleiders voor gehandicapten
Laat uw hond het jaarlijkse vuurwerk overwinnen
Betuweroute en Kunst
logo 50x50Hey Vos! Je eigen naam@vos.net emailadres?
Kunst in huis? Nicole Karrèr maakt echt bijzonder mooie dingen
nettylogo2Kunst in huis? Netty Franssen maakt ook bijzonder mooie dingen

Hosting Favorites

 
ANU Internet Services
Netim
GoDaddy
XEL Media

Blogroll

 
Bomenstichting
Google Translate
PHP
MySQL
jQuery
jQuery UI
YourHead Stacks API
Favicon Generator.
Password Generator.
Check HTTPS problems



Marc's Place


 

The simplest code for Star ratings




Buy me a coffee-2

What you need:
  • PHP with MySQL/MariaDB
  • jQuery
  • FontAwesome

Rating

<?php
/*
Author : M.H.E. Vos
Copyright : (c) 2024, SDS82
Creation date : 2024-01-01
*/

print('<style>
.reviewing
{
cursor: pointer;
}
.reviewing .fa-star.checked
{
color: orange;
}
</style>');

$theorder = 1234;

if ($iresult = mysqli_query($db_connection, "select * from orderitems where orderid = '$theorder'"))
{
while ($thisitem = $iresult->fetch_object())
{
$rating = 0;
$alreadyrated = 0;
if ($rresult = mysqli_query($db_connection, "select rating from ratings where orderid = '$theorder' and productid = '" . addslashes(thisitem->itemid) . "'"))
{
while ($obj = $rresult->fetch_object())
{
$rating = intval($obj->rating);
$alreadyrated = 1;
}
mysqli_free_result($rresult);
}

// Display rating
print('<div class="reviewing">
<span class="fa fa-star ' . ($alreadyrated ? ($rating >= 1 ? 'checked' : '') : 'notrated') . '" aria-hidden="true" data-value="1" data-sku="' . $thisitem->itemid . '"></span>
<span class="fa fa-star ' . ($alreadyrated ? ($rating >= 2 ? 'checked' : '') : 'notrated') . '" aria-hidden="true" data-value="2" data-sku="' . $thisitem->itemid . '"></span>
<span class="fa fa-star ' . ($alreadyrated ? ($rating >= 3 ? 'checked' : '') : 'notrated') . '" aria-hidden="true" data-value="3" data-sku="' . $thisitem->itemid . '"></span>
<span class="fa fa-star ' . ($alreadyrated ? ($rating >= 4 ? 'checked' : '') : 'notrated') . '" aria-hidden="true" data-value="4" data-sku="' . $thisitem->itemid . '"></span>
<span class="fa fa-star ' . ($alreadyrated ? ($rating >= 5 ? 'checked' : '') : 'notrated') . '" aria-hidden="true" data-value="5" data-sku="' . $thisitem->itemid . '"></span>
' . (!$alreadyrated ? '<input form="yourform" id="R' . $thisitem->itemid . '" name="R' . $thisitem->itemid . '" type="hidden" value="' . $rating . '">' : '') . '
</div>');


}
mysqli_free_result($oresult);
}
print('<script>
$(document).ready(function() {
$(".reviewing .fa-star.notrated").hover(function() {
$(this).parent().children(".fa-star").removeClass("checked");
$(this).addClass("checked").prevAll(".fa-star").addClass("checked");
$("#R" + $(this).data("sku")).val($(this).data("value"));
});
});
</script>');
?>


You can add AJAX code to store the rating or add a submit button to submit the 'yourform' form and store the rating that way.
You can also change 'hover' into 'clicked' when you want your customers to actually click on a star.

That's it! Happy coding!


buy me something-2
© 1997- Marc Vos (and others)   -   Privacy Statement   -    Contact Me

On this website, Google Analytics is used to track visitor statistics. These are anonymised data about the number of visitors, which pages they visit on this site, from which regions they visit, which web browsers they use, etc.. You will also see non-personalised ads via Google AdSense. Cookies from Paddle or Paypal are placed when you click on a 'Buy now!' or 'Donate!' button, and possible cookies from Disqus when you use that system to comment on one or more blogposts.
Privacy Statement