Buonasera a tutti,
ci ho sbattuto la testa una giornata intera ma non sono ancora arrivato alla radice del mistero.
Ho incorporato questo plugin jQuery nel mio sito web, l'inidirizzo al plugin è il seguente: https://github.com/dreyescat/bootstrap-rating
nel tutorial del plugin non vi è la spiegazione per modificare ciò che sto cercando di fare, vorrei quindi chiedere a voi.
Si tratta di un sistema di votazione e il problema che riscontro è che:
1) quando l'oggetto (l'immagine ad esempio) non ha ricevuto alcun voto, le stelline (che sono 5) sono tutte vuote eccetto la prima
2) quando come primo voto dell'immagine assegno il massimo (quindi 5) le stelline (se ricarico la pagina) sono tutte vuote (invece di essere piene - colorate) ma per ovviare a questo problema ho inserito questa riga -> //var rate = rate - 1; (nel codice che vi posterò qua sotto la troverete commentata, perchè penso che risolvendo il problema numero 1) si risolva automaticamente anche il secondo!)
Ed ecco a voi il codice javascript che ho adattato io alle mie esigenze (qundi è diverso da quello che troverrete presso il sito che vi ho linkato), di seguito vi posto anche il file rating.php che viene richiamato tramite il javascript.
codice:
(function ($, undefined) { 'use strict';
var OFFSET = 5;
$.fn.rating = function (options) {
this.each(function () {
var $input = $(this);
// Merge data and parameter options.
// Those provided as parameter prevail over the data ones.
var opts = $.extend({}, $input.data(), options);
// Sanitize start, stop, and step.
// All of them start, stop, and step must be integers.
// In case we don't have a valid stop rate try to get a reasonable
// one based on the existence of a valid start rate.
opts.start = parseInt(opts.start, 10) || undefined;
opts.stop = parseInt(opts.stop, 10) ||
opts.start + OFFSET ||
undefined;
opts.step = parseInt(opts.step, 10) || undefined;
// Extend/Override the default options with those provided either as
// data attributes or function parameters.
opts = $.extend({}, $.fn.rating.defaults, opts);
// Fill rating symbols until index.
var fillUntil = function (index) {
var $rates = $rating.children();
// Empty all just in case index is NaN.
$rates.removeClass(opts.filled).addClass(opts.empty);
// Fill all the symbols up to the selected one.
$rates.eq(index).prevAll('.rating-symbol').addBack()
.removeClass(opts.empty).addClass(opts.filled);
};
// Calculate the rate of an index according the the start and step.
var indexToRate = function (index) {
return opts.start + index * opts.step;
};
// Get the corresponding index of a rate or NaN if rate is not a number.
var rateToIndex = function (rate) {
return Math.max(Math.ceil((rate - opts.start) / opts.step), 0);
};
// Check the rate is in the proper range [start..stop) and with
// the proper step.
var contains = function (rate) {
var start = opts.step > 0 ? opts.start : opts.stop;
var stop = opts.step > 0 ? opts.stop - 1 : opts.start + 1;
return start <= rate && rate <= stop && (opts.start + rate) % opts.step === 0;
};
// Update empty and filled rating symbols according to a rate.
var updateRate = function (rate) {
var value = parseInt(rate, 10);
if (contains(value)) {
fillUntil(rateToIndex(value));
}
};
// Call f only if the input is enabled.
var ifEnabled = function (f) {
return function () {
if (!$input.prop('disabled') && !$input.prop('readonly')) {
f.call(this);
}
}
};
var ifDisabled = function (d) {
return function () {
if ($input.prop('disabled') || $input.prop('readonly')) {
d.call(this);
}
}
};
// Build the rating control.
var $rating = $('<div></div>').insertBefore($input);
for (var i = 0; i < rateToIndex(opts.stop); i++) {
$rating.append('<div class="rating-symbol ' + opts.empty + '"></div>');
}
// Initialize the rating control with the associated input value rate.
updateRate($input.val());
// Keep rating control and its associated input in sync.
$input
.on('change', function () {
updateRate($(this).val());
});
$rating
.on('click', '.rating-symbol', ifEnabled(function () {
// Set input to the current value and 'trigger' the change handler.
$input.val(indexToRate($(this).index())).change();
var id_img = $input.attr('id');
var rate = rateToIndex(parseInt($input.val(), 10) + 1);
//var rate = rate - 1;
$.post("rating.php",{
idimg: id_img,
idutente: id_utente,
voto: rate
});
}))
.on('mouseenter', '.rating-symbol', ifEnabled(function () {
// Emphasize on hover in.
fillUntil($(this).index());
$(this).css('cursor','pointer');
}))
.on('mouseenter','.rating-symbol', ifDisabled(function () {
$(this).css('cursor','not-allowed');
}))
.on('mouseleave', '.rating-symbol', ifEnabled(function () {
// Restore on hover out.
fillUntil(rateToIndex(parseInt($input.val(), 10)));
}));
});
};
// Plugin defaults.
$.fn.rating.defaults = {
filled: 'glyphicon glyphicon-star',
empty: 'glyphicon glyphicon-star-empty',
start: 0,
stop: OFFSET,
step: 1
};
$(function () {
$('input.rating').rating();
});
}(jQuery));
il PHP di riferimento è il seguente:
codice:
$sql = $db->prepare("INSERT INTO votazioni (idimmagine,idutente,voto) VALUES (:idimmagine,:idutente,:voto)"); $sql->execute(array(':idimmagine'=>$_POST['idimg'],':idutente'=>$_POST['idutente'],':voto'=>$_POST['voto']));
//$inner_join = $db->prepare("SELECT * FROM votazioni V INNER JOIN immagini I ON I.id = V.idimmagine");
$inner_join = $db->prepare("SELECT * FROM votazioni WHERE idimmagine = :idimmagine");
$inner_join->execute(array(':idimmagine'=>$_POST['idimg']));
$res_inner=$inner_join->fetchAll();
$tot_voto;
foreach($res_inner as $voto){
$tot_voto += $voto['voto'];
}
$media = $tot_voto/count($res_inner);
$ins_media = $db->prepare("UPDATE immagini SET media=? WHERE id=?");
$ins_media->execute(array($media,$_POST['idimg']));
?>
aiutatemi vi preg mi sembra un rompicapo! 
grazieeeee