- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有下面给出的星级评级脚本。它工作正常,但是当我想在处理文件中使用 $_GET 变量时,它没有使用它。
我还想在这个脚本中使用注释,但我不能在 tuto-star-rating.php
中使用 $_POST 或 $_GET| .
我可以得到 $_GET['sid']
在index.php
但我无法在 tuto-start-rating.php
中获取 sid .这tuto-start-rating.php
通过JS调用。
在 index.php 中,url 是 index.php?sid=1
在tuto-star-rating.php
我想使用 $_GET 保存餐厅 ID 但无法这样做。我尝试如下,但它不接受它只接受直接输入的数字,正如您在下面的文件代码中看到的那样:
$getRest = mysql_real_escape_string($_GET['sid']);
$query = $bdd->execute('INSERT INTO rest_rating (sr_id, media, rate, ip, user)
VALUES ('.$getRest.', '.$mediaId.', "'.$rate.'", "'.$ipaddress.'", "'.$user.'")'); // We insert the new rate
我需要帮助来使用不同的形式或通过集成来将评论系统与此代码集成。
index.php
<?php
include('comment/dbClass.php');
$bdd = new db();
?>
<style>
.no_star { display: inline-block; background: url("comment/star.png") no-repeat; width: 16px; height: 16px }
.star { display: inline-block; background: url("comment/star.png") no-repeat; background-position: 0 -16px; width: 16px; height: 16px }
.star_hover { display: inline-block; background: url("comment/star.png") no-repeat; background-position: 0 -32px; width: 16px; height: 16px }
.star_selected { display: inline-block; background: url("comment/star.png") no-repeat; background-position: 0 -48px; width: 16px; height: 16px }
</style>
<?php
function starBar($numStar, $mediaId, $starWidth) { // function with arguments: number of stars, media ID, width of the star image
global $bdd;
$getRest = mysql_real_escape_string($_GET['sid']);
$cookie_name = 'tcRatingSystem'.$mediaId; // Set up the cookie name
// We get the rate average and number of rate from the database
$query = $bdd->getOne('SELECT round(avg(rate), 2) AS average, count(rate) AS nbrRate, sr_id AS sr_id FROM rest_rating WHERE media='.$mediaId.' and sr_id = "'.$getRest.'"');
$avgCeil = round($query['average'], 0); // round above or below to show how many selected stars we display
$getJSON = array('numStar' => $numStar, 'mediaId' => $mediaId); // We create a JSON with the number of stars and the media ID
$getJSON = json_encode($getJSON);
// We create the DIV block with selected stars and unselected stars depending of the rate
$starBar = '<div id="'.$mediaId.'">';
$starBar .= '<div class="';
if( !isset($_COOKIE[$cookie_name]) ) $starBar .= 'star_bar';
$starBar .= '" rel='.$getJSON.' style="width:'.($numStar*$starWidth).'px">';
for ($i=1; $i<=$numStar; $i++) {
$starBar .= '<div class="';
if ($i <= $avgCeil) $starBar .= 'star_selected'; else $starBar .= 'star';
$starBar .= '"></div>';
}
$starBar .= '</div>';
$starBar .= '<div class="resultMedia'.$mediaId.'" style="font-size: small; color: grey">'; // We show the rate score and number of rates
if ($query['nbrRate'] == 0) $starBar .= 'Not rated yet';
else $starBar .= 'Rating: ' . $query['average'] . '/' . $numStar . ' (' . $query['nbrRate'] . ' votes)';
$starBar .= '</div>';
$starBar .= '<div class="box'.$mediaId.'"></div>'; // Return the text "Thank you for rating" when someone rate
$starBar .= '</div>';
return $starBar;
}
echo starBar(5, 59, 16); // We create star bar
?>
tuto-start-rating.php
<?php
session_start();
include('dbClass.php');
$bdd = new db();
//$getRest = mysql_real_escape_string($_GET['sid']);
$ipaddress = $_SERVER["REMOTE_ADDR"];
$user = session_id();
if($_POST) {
$mediaId = $_POST['mediaId']; // Media ID
$rate = $_POST['rate']; // Your rate
$expire = 24*3600; // 1 day
setcookie('tcRatingSystem'.$mediaId, 'voted', time() + $expire, '/'); // Place a cookie
$query = $bdd->execute('INSERT INTO rest_rating (sr_id, media, rate, ip, user)
VALUES (1, '.$mediaId.', "'.$rate.'", "'.$ipaddress.'", "'.$user.'")
'); // We insert the new rate
// We calculate the new average and new number of rate
$result = $bdd->getOne('SELECT round(avg(rate), 2) AS average, count(rate) AS nbrRate FROM rest_rating WHERE media='.$mediaId.'');
$avgCeil = round($result['average'], 0); // Round the average
// Send JSON back with the new average, the number of rate and rounded average
$dataBack = array('avg' => $result['average'], 'nbrRate' => $result['nbrRate'], 'avgCeil' => $avgCeil);
$dataBack = json_encode($dataBack);
echo $dataBack;
}
?>
tuto-star-rating.js
function rateMedia(mediaId, rate, numStar) {
$('.box' + mediaId).html('<img src="comment/loader-small.gif" alt="" />'); // Display a processing icon
var data = {mediaId: mediaId, rate: rate}; // Create JSON which will be send via Ajax
$.ajax({ // JQuery Ajax
type: 'POST',
url: 'comment/tuto-star-rating.php', // URL to the PHP file which will insert new value in the database
data: data, // We send the data string
dataType: 'json',
timeout: 3000,
success: function(data) {
$('.box' + mediaId).html('<div style="font-size: small; color: green">Thank you for rating</div>'); // Return "Thank you for rating"
// We update the rating score and number of rates
$('.resultMedia' + mediaId).html('<div style="font-size: small; color: grey">Rating: ' + data.avg + '/' + numStar + ' (' + data.nbrRate + ' votes)</div>');
// We recalculate the star bar with new selected stars and unselected stars
var ratingBar = '';
for ( var i = 1; i <= numStar; i++ ) {
ratingBar += '<div class="';
if (i <= data.avgCeil) ratingBar += 'star_selected'; else ratingBar += 'star';
ratingBar += '"></div>';
}
$('#' + mediaId + ' .star_bar').html(ratingBar).off('mouseenter');
},
error: function() {
$('#box').text('Problem');
}
});
}
$(function () {
$('.star_bar').on('mouseenter', function overBar(event) { // Mouse enter the star bar
var relData = $.parseJSON($(this).attr('rel')); // Get JSON values: number of stars and media ID
$(this).css('cursor','pointer');
// We create a new star bar OVER the previous one with transparent stars
var newStarBar = '';
for ( var i = 1; i <= relData.numStar; i++ ) {
newStarBar += '<div class="no_star" id="' + i + '" title="' + i + '/' + relData.numStar + '" onclick="rateMedia(' + relData.mediaId + ', ' + i + ', ' + relData.numStar + '); return false;"></div>';
}
$(this).css('position', 'relative').append('<div id="over' + relData.mediaId + '" style="position:absolute; top:0; left:0;">' + newStarBar + '</div>');
// When we move the mouse over the new transparent star bar they become blue
$('#over' + relData.mediaId + ' > div').mouseover(function() {
var myRate = $(this).attr('id');
for ( var i = 1; i <= relData.numStar; i++ ) {
if (i <= myRate) $('#over' + relData.mediaId + ' #' + i).attr('class', 'star_hover');
else $('#over' + relData.mediaId + ' #' + i).attr('class', 'no_star');
}
});
});
// Mouse leaves the star bar, we remove the rating bar
$('.star_bar').on('mouseleave', function overBar(event) {
var relData = $.parseJSON($(this).attr('rel'));
$('#over' + relData.mediaId).remove();
});
});
**tuto-star-rating.php**
<?php
session_start();
include('dbClass.php');
$bdd = new db();
//$getRest = mysql_real_escape_string($_GET['sid']);
$ipaddress = $_SERVER["REMOTE_ADDR"];
$user = session_id();
if($_POST) {
$mediaId = $_POST['mediaId']; // Media ID
$rate = $_POST['rate']; // Your rate
$expire = 24*3600; // 1 day
setcookie('tcRatingSystem'.$mediaId, 'voted', time() + $expire, '/'); // Place a cookie
$query = $bdd->execute('INSERT INTO rest_rating (sr_id, media, rate, ip, user)
VALUES (1, '.$mediaId.', "'.$rate.'", "'.$ipaddress.'", "'.$user.'")
'); // We insert the new rate
// We calculate the new average and new number of rate
$result = $bdd->getOne('SELECT round(avg(rate), 2) AS average, count(rate) AS nbrRate FROM rest_rating WHERE media='.$mediaId.'');
$avgCeil = round($result['average'], 0); // Round the average
// Send JSON back with the new average, the number of rate and rounded average
$dataBack = array('avg' => $result['average'], 'nbrRate' => $result['nbrRate'], 'avgCeil' => $avgCeil);
$dataBack = json_encode($dataBack);
echo $dataBack;
}
?>
dbClass.php
<?php
class db {
private $conn;
private $host;
private $user;
private $password;
private $baseName;
private $port;
private $Debug;
function __construct($params=array()) {
$this->conn = false;
$this->host = 'localhost'; //hostname
$this->user = 'root'; //username
$this->password = ''; //password
$this->baseName = 'lepetit'; //name of your database
$this->port = '3306';
$this->debug = true;
$this->connect();
}
function __destruct() {
$this->disconnect();
}
function connect() {
if (!$this->conn) {
$this->conn = mysql_connect($this->host, $this->user, $this->password);
mysql_select_db($this->baseName, $this->conn);
mysql_set_charset('utf8',$this->conn);
if (!$this->conn) {
$this->status_fatal = true;
echo 'Connection BDD failed';
die();
}
else {
$this->status_fatal = false;
}
}
return $this->conn;
}
function disconnect() {
if ($this->conn) {
@pg_close($this->conn);
}
}
function getOne($query) { // getOne function: when you need to select only 1 line in the database
$cnx = $this->conn;
if (!$cnx || $this->status_fatal) {
echo 'GetOne -> Connection BDD failed';
die();
}
$cur = @mysql_query($query, $cnx);
if ($cur == FALSE) {
$errorMessage = @pg_last_error($cnx);
$this->handleError($query, $errorMessage);
}
else {
$this->Error=FALSE;
$this->BadQuery="";
$tmp = mysql_fetch_array($cur, MYSQL_ASSOC);
$return = $tmp;
}
@mysql_free_result($cur);
return $return;
}
function getAll($query) { // getAll function: when you need to select more than 1 line in the database
$cnx = $this->conn;
if (!$cnx || $this->status_fatal) {
echo 'GetAll -> Connection BDD failed';
die();
}
mysql_query("SET NAMES 'utf8'");
$cur = mysql_query($query);
$return = array();
while($data = mysql_fetch_assoc($cur)) {
array_push($return, $data);
}
return $return;
}
function execute($query,$use_slave=false) { // execute function: to use INSERT or UPDATE
$cnx = $this->conn;
if (!$cnx||$this->status_fatal) {
return null;
}
$cur = @mysql_query($query, $cnx);
if ($cur == FALSE) {
$ErrorMessage = @mysql_last_error($cnx);
$this->handleError($query, $ErrorMessage);
}
else {
$this->Error=FALSE;
$this->BadQuery="";
$this->NumRows = mysql_affected_rows();
return;
}
@mysql_free_result($cur);
}
function handleError($query, $str_erreur) {
$this->Error = TRUE;
$this->BadQuery = $query;
if ($this->Debug) {
echo "Query : ".$query."<br>";
echo "Error : ".$str_erreur."<br>";
}
}
}
?>
最佳答案
来自 your comment ,
I can get sid in index.php but i can not get sid in tuto-start-rating.php. This tuto-start-rating.php is called through JS
由于您将 JavaScript 作为外部文件包含在内,因此您不能在 tuto-star-rating.js< 中使用/访问像 $_GET['sid']
这样的 PHP 变量/em> 文件。您需要按以下方式更改您的 index.php 和 tuto-star-rating.js 文件,
index.php
在 index.php 页面中包含 tuto-star-rating.js 文件之前,添加以下行,
<script>var sid = "<?php echo $_GET['sid']; ?>";</script>
// include your tuto-star-rating.js file
tuto-star-rating.js
您需要按以下方式更改您的 AJAX 请求,
function rateMedia(mediaId, rate, numStar) {
// your code
$.ajax({
type: 'POST',
url: 'comment/tuto-star-rating.php?sid=' + sid,
// your code
});
}
这样,您就可以使用$_GET
访问tuto-star-rating.php页面中的sid超全局,像这样:
$getRest = mysql_real_escape_string($_GET['sid']);
旁注: 不要使用 mysql_*
函数,它们从 PHP 5.5 开始被弃用,并在 PHP 7.0 中被完全删除。使用 mysqli
或 pdo
反而。 And this is why you shouldn't use mysql_*
functions .
关于javascript - php mysql 用户评分和评论系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39537771/
我正在使用评论系统,现在,我想重写 url 评论的片段并附加一个符号#,我想将页面部分移动到评论列表,正好是最后一个评论用户,带有 username 我在发表评论时使用 next 重定向用户: {
这个问题在这里已经有了答案: "Rate This App"-link in Google Play store app on the phone (21 个回答) 关闭2年前。 有没有一种方法可以要
长期潜伏者第一次海报... 我们正在使用 Facebook 的 API 将其集成到我们的网络应用程序中,并且我们能够通过 {page-id}/ratings 部分中的 {open_graph_stor
我正在尝试让 Visual Studio 2012 自动格式化我的评论 block ,就像它对我的 C# block 所做的那样。我希望我的评论看起来像这样: /* * Here is my C#
在 MySQl 中创建表时对每个字段进行注释是否会影响性能?我正在处理一个包含 1000 多个表的数据库,几乎每个表中的每个字段都有注释。我只是想知道这是否会以任何方式影响 MySQL 的性能? 最佳
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
这个问题在这里已经有了答案: SQL select only rows with max value on a column [duplicate] (27 个答案) 关闭 5 年前。 我这里有 2
如何在评论中正确编写 --> 或 -->? 我正在维护一个包含许多小程序代码条目的大型 html 文件。说: a --> b. 我在 HTML 中将其编码为 -->: a --> b. 但是,我
这是一个简单的问题。有没有办法允许用户直接在我的应用程序中输入评论和/或评级,并将这些数据发回 Android Market?如果是这样,如果我使用 EditText View 允许用户输入,代码会是
注释是否表示代码中带有//或/* */的注释? 最佳答案 不,注释不是评论。使用语法 @Annotation 将注释添加到字段、类或方法。最著名的注解之一是@Override,用于表示方法正在覆盖父类
我有一个包含两个模型的 Django 应用程序:第一个是 django.contrib.auth.User,第二个是我创建的 Product。 我会为每个产品添加评论,因此每个注册用户都可以为每个产品
有没有办法评论多行......其中已经有一些评论? 即 ... Hello world! Multi-line comment end --> 看来连
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: obj.nil? vs. obj == nil 现在通过 ruby koans 工作,发现这个评论嵌入在
这是一个基本问题 .gemrc 文件中是否允许注释? 如果是,你会怎么做? 我这里查了没用 docs.rubygems.org/read/chapter/11 最佳答案 文档说:The config
有没有办法在 SASS 中添加 sass-only 注释?你知道,所以输出 .css 文件没有那些注释 例如, /* global variables */ $mainColor: #666; /*
我想搜索在任何媒体上发布的评论中的任何特定关键字或几个关键字的组合。我的要求是在 API 的帮助下获取包含该关键字的评论。我浏览了 Instagram API 的文档,发现只能通过哈希标签进行搜索,而
在 WordPress 中,您可以在页面加载之前执行以下操作来编辑文章的内容: add_filter('the_content', 'edit_content'); function edit_con
在指示要合并的内容时, checkin 合并的最佳方法是什么?我已经说过 10 个变更集我正在从我的主分支合并到一个发布分支。每一个都包含我在 checkin 主分支时写的详细注释。现在,当我合并时,
我知道如何查询常规网站的社交参与度计数。可以使用Facebook图形浏览器(https://developers.facebook.com/tools/explorer/)或throug api轻松实
我正在尝试从 YouTube 视频中获得特定评论。例如,我想从 YouTube 视频的第 34 条评论中获取详细信息。有谁知道在不阅读所有评论列表的情况下我该怎么做? 或者,如果没有任何解决方案可以仅
我是一名优秀的程序员,十分优秀!