-1

I'm new in PHP language and i doing a task now. I have to take two queries with onclick function. When i click "Best rate" to call the rows from the table ordered by rate and when i click "All reviews" to call them by date created. Do i need to use ajax or there is better way to do it ?

<?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
    }
?>

Here is the code where i make the query and where the reviews are ordered by date. I want to make it when i click "Best rate", the reviews to reorder by rate.

lfc123
  • 141
  • 8
  • ajax need if you want to do it without page refresh. so your page will be "live". – vaso123 Nov 28 '14 at 16:22
  • Don't be afraid by ajax, it is quite easy to handle it once you understand synchron vs asychron behaviour: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – A. Wolff Nov 28 '14 at 16:24
  • Sanitise your data before going to the database (better yet, use bound parameters on a prepared statement) or you're in real danger of a Bobby Tables situation... as for AJAX, it'll be slicker if you do. Using JQuery.ajax is not a bad idea as it makes life a lot easier. – CD001 Nov 28 '14 at 16:31
  • 1
    If you want sort your data, you dont need AJAX. You can check [tablesorter](http://tablesorter.com/docs/)(if you use a table html) or make your own method – Benjamin Poignant Nov 28 '14 at 16:35

2 Answers2

-1

You actually don't need AJAX for this one.

Ajax would be better if your button was "Best Rates of Last year" or "View More Rates" ---

But since you only want the data to be re-ordered, you really don't need to use AJAX...

You can simply re-order the "rate" values with javascript -!

rm-vanda
  • 3,122
  • 3
  • 23
  • 34
-1

insert below code on a php file like SelectByRate.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>

load this code for php file and then load the result with 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); 
    }
});

example for use ajax

Mortie
  • 390
  • 9
  • 24