|
|
|
ขอถามเรื่อง rating jquery ครับคือดูโค๊ดนะครับว่าผมจะทําไงถึงจะถูกจากโค๊ดสําเร็จรูป |
|
|
|
|
|
|
|
คือ จากโค๊ดจะเพิ่มและเขียนยังไงให้ลง เก้บข้อมูลโหวต ครับ จากฟอร์มโหวต ผมไม่เคยทําโปรแกรมโหวตเลยไม่รู้ว่าฐานข้อมูลการโหวตนี้ต้องเก็บอะไรไว้บ้างในฐานข้อมูลครับ
form
Code (PHP)
<?php require('./demo6.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>Star Rating widget Demo6 Page</title>
<!-- demo page css -->
<link rel="stylesheet" type="text/css" media="screen" href="css/demos.css?b38"/>
<style type="text/css">
#container { position:relative; height:30px; }
#container > * { position:absolute; height:30px; left:0; top:0; }
#loader {display:none;padding-left:20px; background:url(css/crystal-arrows.gif) no-repeat center left;}
</style>
<!-- demo page js -->
<script type="text/javascript" src="js/jquery.min.js?v=1.4.2"></script>
<script type="text/javascript" src="js/jquery-ui.custom.min.js?v=1.8"></script>
<!-- Star Rating widget stuff here... -->
<script type="text/javascript" src="js/jquery.ui.stars.js?v=3.0.0b38"></script>
<link rel="stylesheet" type="text/css" href="css/crystal-stars.css?b38"/>
<script type="text/javascript">
$(function(){
$("#rat").children().not(":radio").hide();
// Create stars
$("#rat").stars({
// starWidth: 28, // only needed in "split" mode
cancelShow: false,
callback: function(ui, type, value)
{
// Hide Stars while AJAX connection is active
$("#rat").hide();
$("#loader").show();
// Send request to the server using POST method
/* NOTE:
The same PHP script is used for the FORM submission when Javascript is not available.
The only difference in script execution is the returned value.
For AJAX call we expect an JSON object to be returned.
The JSON object contains additional data we can use to update other elements on the page.
To distinguish the AJAX request in PHP script, check if the $_SERVER['HTTP_X_REQUESTED_WITH'] header variable is set.
(see: demo6.php)
*/
$.post("demo6.php", {rate: value}, function(db)
{
// Select stars to match "Average" value
ui.select(Math.round(db.avg));
// Update other text controls...
$("#avg").text(db.avg);
$("#votes").text(db.votes);
// Show Stars
$("#loader").hide();
$("#rat").show();
}, "json");
}
});
});
</script>
</head>
<body>
<div class="pageBody">
<div id="container">
<form id="rat" action="" method="post">
<?php foreach ($options as $id => $rb): ?>
<input type="radio" name="rate" value="<?php echo $id ?>" title="<?php echo $rb['title'] ?>" <?php echo $rb['checked'].' '.$rb['disabled'] ?> />
<?php endforeach; ?>
<?php if (!$rb['disabled']): ?>
<input type="submit" value="Rate it!" />
<?php endif; ?>
</form>
<div id="loader"><div style="padding-top: 5px;">please wait...</div></div>
</div>
<?php $db = get_votes() ?>
<div>
Item Popularity: <span id="avg"><?php echo $db['avg'] ?></span>/<strong><?php echo count($options) ?></strong>
(<span id="votes"><?php echo $db['votes'] ?></span> votes cast)
</div>
</div>
</body>
</html>
php
Code (PHP)
<?php
sleep(2);
// Fill [$options] with radiobutton properties now
$options = get_options();
// Check, if we need to proccess the FORM submission (or AJAX call that pretends POST method)
if($_SERVER["REQUEST_METHOD"] == 'POST')
{
// veriffy user input!
$vote = in_range($_POST['rate'], 1, 5);
// update statistic and save to file
$db = save_vote($vote);
// For AJAX requests we'll return JSON object with current vote statistics
if($_SERVER['HTTP_X_REQUESTED_WITH'])
{
header('Cache-Control: no-cache');
echo json_encode($db); // requires: PHP >= 5.2.0, PECL json >= 1.2.0
}
// For non-AJAX requests we are going to echo {$post_message} variable in main script
else
{
$avg = round($db['avg']);
foreach($options as $id => $val) {
$options[$id]['disabled'] = 'disabled="disabled"';
$options[$id]['checked'] = $id==$avg ? 'checked="checked"' : '';
}
}
}
// ========================
// Functions
// ========================
function get_options() {
return array(
1 => array('title' => 'Not so great'),
2 => array('title' => 'Quite good'),
3 => array('title' => 'Good'),
4 => array('title' => 'Great!'),
5 => array('title' => 'Excellent!'),
);
}
function in_range($val, $from=0, $to=100) {
return min($to, max($from, (int)$val));
}
function get_dbfile() {
return preg_replace('#\.php$#', '.dat', __FILE__);
}
function get_votes() {
$dbfile = get_dbfile();
return is_file($dbfile) ? unserialize(file_get_contents($dbfile)) : array('votes' => 0, 'sum' => 0, 'avg' => 0);
}
function save_vote($vote) {
$db = get_votes();
$db['votes']++;
$db['sum'] += $vote;
$db['avg'] = sprintf('%01.1f', $db['sum'] / $db['votes']);
file_put_contents(get_dbfile(), serialize($db));
return $db;
}
Tag : PHP
|
|
|
|
|
|
Date :
2010-12-21 12:48:46 |
By :
kenghockey |
View :
1418 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code ตรงไหนครับ
|
|
|
|
|
Date :
2010-12-22 07:50:54 |
By :
อ๊อด |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตรงนี้ครับ ว่าจะให้โหวตมันเก็บค่ายังไง แอตทิบิ้วมันคืออะไรครับ
Code (PHP)
<form id="rat" action="" method="post">
074.
075. <?php foreach ($options as $id => $rb): ?>
076. <input type="radio" name="rate" value="<?php echo $id ?>" title="<?php echo $rb['title'] ?>" <?php echo $rb['checked'].' '.$rb['disabled'] ?> />
077. <?php endforeach; ?>
078.
079. <?php if (!$rb['disabled']): ?>
080. <input type="submit" value="Rate it!" />
081. <?php endif; ?>
082.
083. </form>
|
|
|
|
|
Date :
2010-12-22 10:09:02 |
By :
kenghockey |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|