if(isset($_POST["action"]))
{
$average_rating = 0;
$total_review = 0;
$five_star_review = 0;
$four_star_review = 0;
$three_star_review = 0;
$two_star_review = 0;
$one_star_review = 0;
$total_user_rating = 0;
$review_content = array();
$query = "
SELECT * FROM review_table
ORDER BY review_id DESC
";
$result = $conn->query($query, PDO::FETCH_ASSOC);
foreach($result as $row)
{
$review_content[] = array(
'user_name' => $row["user_name"],
'user_review' => $row["user_review"],
'rating' => $row["user_rating"],
'datetime' => date('l jS, F Y h:i:s A', $row["datetime"])
);
if($row["user_rating"] == '5')
{
$five_star_review++;
}
if($row["user_rating"] == '4')
{
$four_star_review++;
}
if($row["user_rating"] == '3')
{
$three_star_review++;
}
if($row["user_rating"] == '2')
{
$two_star_review++;
}
if($row["user_rating"] == '1')
{
$one_star_review++;
}
$total_review++;
$total_user_rating = $total_user_rating + $row["user_rating"];
}
$average_rating = $total_user_rating / $total_review;
$output = array(
'average_rating' => number_format($average_rating, 1),
'total_review' => $total_review,
'five_star_review' => $five_star_review,
'four_star_review' => $four_star_review,
'three_star_review' => $three_star_review,
'two_star_review' => $two_star_review,
'one_star_review' => $one_star_review,
'review_data' => $review_content
);
echo json_encode($output);
}
?>这是我的审查系统的submit_review.php文件,我试图在产品页面中检索,因为我需要product_id,而在产品页面中,我使用<input type="hidden">来给出如何在isset()中检索这个id。
($_POST["action"])这是我的product.php页面:
load_rating_data();
function load_rating_data()
{
$.ajax({
url:"submit_rating.php",
method:"POST",
data:{action:'load_data'},
dataType:"JSON",
success:function(data)
{
$('#average_rating').text(data.average_rating);
$('#total_review').text(data.total_review);
var count_star = 0;
$('.main_star').each(function(){
count_star++;
if(Math.ceil(data.average_rating) >= count_star)
{
$(this).addClass('text-warning');
$(this).addClass('star-light');
}
});
$('#total_five_star_review').text(data.five_star_review);
$('#total_four_star_review').text(data.four_star_review);
$('#total_three_star_review').text(data.three_star_review);
$('#total_two_star_review').text(data.two_star_review);
$('#total_one_star_review').text(data.one_star_review);
$('#five_star_progress').css('width', (data.five_star_review/data.total_review) * 100 + '%');
$('#four_star_progress').css('width', (data.four_star_review/data.total_review) * 100 + '%');
$('#three_star_progress').css('width', (data.three_star_review/data.total_review) * 100 + '%');
$('#two_star_progress').css('width', (data.two_star_review/data.total_review) * 100 + '%');
$('#one_star_progress').css('width', (data.one_star_review/data.total_review) * 100 + '%');
if(data.review_data.length > 0)
{
var html = '';
for(var count = 0; count < data.review_data.length; count++)
{
html += '<div class="row mb-3">';
html += '<div class="col-sm-1"><div class="rounded-circle bg-danger text-white pt-2 pb-2"><h3 class="text-center">'+data.review_data[count].user_name.charAt(0)+'</h3></div></div>';
html += '<div class="col-sm-11">';
html += '<div class="card">';
html += '<div class="card-header"><b>'+data.review_data[count].user_name+'</b></div>';
html += '<div class="card-body">';
for(var star = 1; star <= 5; star++)
{
var class_name = '';
if(data.review_data[count].rating >= star)
{
class_name = 'text-warning';
}
else
{
class_name = 'star-light';
}
html += '<i class="fa fa-star '+class_name+' mr-1"></i>';
}
html += '<br />';
html += data.review_data[count].user_review;
html += '</div>';
html += '<div class="card-footer text-right">On '+data.review_data[count].datetime+'</div>';
html += '</div>';
html += '</div>';
html += '</div>';
}
$('#review_content').html(html);
}
}
})
}
});我希望产品id在我的动作部分,以便我可以很容易地应用条件,如哪里的product_id=abavjhj变量。
发布于 2022-12-03 08:57:32
要检索上面代码中的产品ID,您可以在产品页面的HTML中添加一个隐藏的输入字段,其名称为"product_id“,并将值设置为产品ID。然后,在PHP代码中,可以使用$_POST"product_id“变量访问产品ID。
例如,可以将以下代码添加到产品页:
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">然后,在PHP代码中,可以使用以下代码访问产品ID:
$product_id = $_POST["product_id"];然后,您可以在SQL中使用$product_id变量来检索特定产品的评论。
https://stackoverflow.com/questions/74665140
复制相似问题