Zdravim, no zadany link mi vyhadzuje 403 error tak ze neviem co vlastne potrebujes, lebo nevidim finalny produkt. Ked som si presiel kod vyzera celkom pouzitelne cize neviem povedat, kde presne by mohol byt problem. Postujem tu vsak svoj jednoduchy rating ktory splna to co potrebujes, avsak udaje sa ukladaju do TXT suboru, nie do SQL databazy. Dufam ze pomoze.
ratings.php
Code:
<?php
error_reporting(0);
$rating = new ratings($_POST['widget_id']);
isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote();
class ratings {
public $data_file = 'ratings.data.txt';
private $widget_id;
private $data = array();
public function __construct($wid)
{
$this->widget_id = $wid;
$all = file_get_contents($this->data_file);
if($all)
{
$this->data = unserialize($all);
}
}
public function get_ratings()
{
if($this->data[$this->widget_id])
{
echo json_encode($this->data[$this->widget_id]);
}
else
{
$data['widget_id'] = $this->widget_id;
$data['number_votes'] = 0;
$data['total_points'] = 0;
$data['dec_avg'] = 0;
$data['whole_avg'] = 0;
echo json_encode($data);
}
}
public function vote()
{
# Get the value of the vote
preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
$vote = $match[1];
$ID = $this->widget_id;
# Update the record if it exists
if($this->data[$ID])
{
$this->data[$ID]['number_votes'] += 1;
$this->data[$ID]['total_points'] += $vote;
}
# Create a new one if it doesn't
else
{
$this->data[$ID]['number_votes'] = 1;
$this->data[$ID]['total_points'] = $vote;
}
$this->data[$ID]['dec_avg'] = round( $this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1 );
$this->data[$ID]['whole_avg'] = round( $this->data[$ID]['dec_avg'] );
file_put_contents($this->data_file, serialize($this->data));
$this->get_ratings();
}
}
?>
rating.js
Code:
// This is the first thing we add ------------------------------------------
$(document).ready(function() {
$('.rate_widget').each(function(i) {
var widget = this;
var out_data = {
widget_id : $(widget).attr('id'),
fetch: 1
};
$.post(
'class/ratings.php',
out_data,
function(INFO) {
$(widget).data( 'fsr', INFO );
set_votes(widget);
},
'json'
);
});
$('.ratings_stars').hover(
// Handles the mouseover
function() {
$(this).prevAll().andSelf().addClass('ratings_over');
$(this).nextAll().removeClass('ratings_vote');
},
// Handles the mouseout
function() {
$(this).prevAll().andSelf().removeClass('ratings_over');
// can't use 'this' because it wont contain the updated data
set_votes($(this).parent());
}
);
// This actually records the vote
$('.ratings_stars').bind('click', function() {
var star = this;
var widget = $(this).parent();
var clicked_data = {
clicked_on : $(star).attr('class'),
widget_id : $(star).parent().attr('id')
};
$.post(
'class/ratings.php',
clicked_data,
function(INFO) {
widget.data( 'fsr', INFO );
set_votes(widget);
},
'json'
);
});
});
function set_votes(widget) {
var avg = $(widget).data('fsr').whole_avg;
var votes = $(widget).data('fsr').number_votes;
var exact = $(widget).data('fsr').dec_avg;
window.console && console.log('and now in set_votes, it thinks the fsr is ' + $(widget).data('fsr').number_votes);
$(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote');
$(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote');
$(widget).find('.total_votes').text( votes + ' votes recorded (' + exact + ' rating)' );
}
// END FIRST THING
A ak to chces pouzit staci includovat js a php file a urobit nieco taketo
Code:
<div id='r".$row['id_zlavy']."' class='rate_widget'>
<div class='star_1 ratings_stars'></div>
<div class='star_2 ratings_stars'></div>
<div class='star_3 ratings_stars'></div>
<div class='star_4 ratings_stars'></div>
<div class='star_5 ratings_stars'></div>
</div>
A samozrejme nejake to CSS
Code:
.rate_widget
{
width: 100px;
height: 15px;
margin: 0;
padding: 0;
float: left;
border: 0;
}
.ratings_stars
{
background: url('images/star_gray.png') no-repeat;
float: left;
height: 15px;
padding: 2px;
padding-top: 3px;
width: 14px;
}
.ratings_vote
{
background: url('images/star.png') no-repeat;
}
.ratings_over
{
background: url('images/star.png') no-repeat;
}
Neviem ci to pomoze, ale dufam ze ano.