<?php 

    include($_SERVER['DOCUMENT_ROOT'] . "/_ps2020/config/connect.php");
    $num = $_REQUEST['num'];
    $option = $_REQUEST['option'];

    $returnValue = 0;

    if($option == "top"){
        $result1 = $conn->query("SELECT * FROM feedback ORDER BY feedback_id ASC LIMIT $num");
        if($result1){
            while($row = $result1->fetch_assoc()){
                $feedId = $row['feedback_id'];
                $result2 = $conn->query("DELETE FROM feedback WHERE feedback_id = '$feedId'");
                if($result2){
                    $returnValue = 1;
                }else{
                    $returnValue = 0;
                    break;
                }
            }  
            echo $returnValue; 
        }
    }

    if($option == "bottom"){
        $result1 = $conn->query("SELECT * FROM feedback ORDER BY feedback_id DESC LIMIT $num");
        if($result1){
            while($row = $result1->fetch_assoc()){
                $feedId = $row['feedback_id'];
                $result2 = $conn->query("DELETE FROM feedback WHERE feedback_id = '$feedId'");
                if($result2){
                    $returnValue = 1;
                }else{
                    //If there is an error it should return an error "0"
                    $returnValue = 0;
                    break;
                }
            }
            echo $returnValue; 
        }
    }

    // echo $returnValue;
        
    $conn->close();

?>