我是PHP语言的新手,我现在正在完成一项任务。我必须使用onclick函数进行两次查询。当我点击“最佳价格”来调用按价格排序的表格中的行,当我点击“所有评论”按照创建的日期调用它们时。我需要使用ajax还是有更好的方法来实现它?
<?php
$posted = false;
if(isset($_POST['add']))
{
$posted = true;
$email = $_POST['email'];
$name = $_POST['name'];
$rate = $_POST['rate'];
$comment = $_POST['comment'];
$dth = date("Y-m-d H:i:s");
$q = "INSERT INTO reviews(email, name, rate, comment, date_created) VALUES ('$email', '$name', '$rate', '$comment', '$dth')";
$k = mysqli_query($con,$q);
}
?>
<body>
<p>Best rate</p><?php
$select_reviews = "SELECT comment, rate FROM reviews ORDER BY date_created DESC LIMIT 4" or die("Не може да изпълни заявката");
$run_reviews = mysqli_query($con, $select_reviews);
while ($review = mysqli_fetch_assoc($run_reviews)){
$post_review = $review['comment'];
$post_rate = $review['rate']
?>
<div class='comment'> <?php echo $post_review; ?></div>
<div class='rate'> <?php echo $post_rate; ?> </div>
<div>-----------------</div>
<?php
}
?>
以下是我进行查询的代码以及按日期排序评论的位置。我想点击“最优惠价格”,评价按照价格重新排序。
答案 0 :(得分:-1)
实际上你不需要这个AJAX。
如果您的按钮是“去年最佳价格”或“查看更多房价”,那么Ajax会更好---但是因为你只想重新排序数据,所以你真的不需要使用AJAX ......
你可以简单地用javascript重新排序“rate”值 - !
答案 1 :(得分:-1)
在像SelectByRate.php
这样的php文件上插入代码<?php
$select_reviews_By_Rate = "SELECT comment, rate FROM reviews ORDER BY rate DESC LIMIT 4" or die("Не може да изпълни заявката");
$run_reviews = mysqli_query($con, $select_reviews_By_Rate);
while ($review = mysqli_fetch_assoc($run_reviews)){
$post_review = $review['comment'];
$post_rate = $review['rate']
?>
<div class='comment'> <?php echo $post_review; ?></div>
<div class='rate'> <?php echo $post_rate; ?> </div>
<div>-----------------</div>
为php文件加载此代码,然后使用ajax加载结果
$('button.showByRate').click(function(){
$.get('your SelectByRate.php file url',{DataName:DataValue},function(data){
//insert your data on page with javascript
//for example use this
$('p.showByRate').html(data);
}
});