<?php require_once('../Connections/conn_ama.php');
include("../../../../../../functions/functions.php");

if (!isset($_SESSION)) {
  session_start();
}

    //$editDeleteAction = $_SERVER['PHP_SELF'];
    if (isset($_GET['del'])) {
        // echo 'asdfdasf';
        $deleteID = $_GET['del'];

        //echo $deleteID;


            $deleteSql = "DELETE FROM newsarticles WHERE ah = '".$deleteID."'";
            $query_result = mysqli_query($conn_ama,$deleteSql) or die(mysqli_error($conn_ama));
            //$affectedRows = mysqli_affected_rows($conn_ama,$query_result);

            if($query_result){

                $query_rsForDel ="SELECT imageID, image, thumbnail FROM articleimages WHERE ah = '".$deleteID."'";
                $rsForDel = mysqli_query($conn_ama, $query_rsForDel ) or die(mysqli_error($conn_ama));
                $row_rsForDel = mysqli_fetch_assoc($rsForDel);
                $totalRows_rsForDel = mysqli_num_rows($rsForDel);

                $msg = NULL;
                do{
                    // assign all the names of the image files to variable
                    $mainImage = $row_rsForDel['image'];
                    $thumbnail = $row_rsForDel['thumbnail'];

                    //Deleting the main Image
                    $mainfolder = "../../articles-images/news/";
                    $filename = $mainfolder.$mainImage;
                    // @chmod($filename,0700);
                    $delete_main = @unlink($filename);
                    if($delete_main){$msg[] = "maindeleted";}

                    //Deleting the thumbnail Image
                    $thumbfolder = $mainfolder."thumbs/";
                    $filename_1 = $thumbfolder.$thumbnail;
                    //@chmod($filename_1,0700);
                    $delete_thumb = @unlink($filename_1);
                    if($delete_thumb){$msg[] = "thumbnaildelete";}

                    if(isset($msg) && $msg != NULL)
                    {
                        $query_rsDeleteDBImages = "DELETE FROM articleimages WHERE ah = '".$deleteID."'";
                        $deleteResult = mysqli_query($conn_ama, $query_rsDeleteDBImages) or die(mysqli_error($conn_ama));

                       /*  if($deleteResult)
                        {
                            echo "deleted";
                        } */
                    }

                } while($row_rsForDel = mysqli_fetch_assoc($rsForDel));

                echo "deleted";
            }

        //echo "deleted";



}


?>
