JezK
Edit File: alloysprice.php
<?php ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); include('db.php'); $matrialTypeToGetEle = $_POST["matrialTypeToGetEle"]; $ore = $_POST["ore"]; $dateUserSelected = $_POST['dateSelectedFromCalender']; // $duration = "-". $_POST['duration'] . " month"; // $selectedAlloy = $_POST["selectedAlloy"]; $selectedAlloy = $_POST["selectedAlloy"]; $defaultDuration ; // added here to check if all recrds btton is checkd or not if (empty($_POST['duration']) || $_POST['duration'] === 'all') { // Set a default duration to one month $defaultDuration = 0; $duration = "-$defaultDuration month"; } else { $defaultDuration = $_POST['duration']; $duration = "-".$_POST['duration']." month"; } $db_first_inserted_date_query = "SELECT DISTINCT `date` FROM `market_prices` WHERE type = '$matrialTypeToGetEle' AND description = '$selectedAlloy' ORDER BY date ASC LIMIT 1"; $db_first_inserted_date_query_result = mysqli_fetch_all(mysqli_query($conn, $db_first_inserted_date_query, MYSQLI_ASSOC))[0][0]; $db_current_date_query = "SELECT DISTINCT `date` FROM `market_prices` WHERE type = '$matrialTypeToGetEle' AND description = '$selectedAlloy' ORDER BY date DESC LIMIT 1"; $db_current_date = mysqli_fetch_all(mysqli_query($conn, $db_current_date_query, MYSQLI_ASSOC))[0][0]; $today = new DateTime($db_current_date); $oneMonthAgo = $today->modify($duration); $formattedBeforeDate = $oneMonthAgo->format('Y-m-d'); // if Duration selected is all it should take start date as initial insert reord if($defaultDuration===0){ $formattedBeforeDate =$db_first_inserted_date_query_result; } $query = "SELECT `date` AS x, `price` AS 'y' FROM market_prices WHERE `date` BETWEEN '$formattedBeforeDate' AND '$db_current_date' AND description = '$selectedAlloy' AND price > 0 ORDER BY `date` DESC LIMIT 0, 100000"; $ores = mysqli_query($conn, $query); //echo $query; $finalArry = mysqli_fetch_all($ores,MYSQLI_ASSOC); //$resultArry = []; // foreach ($finalArry as $item) { // $resultArry[] = array('label' => $item["date"],"y" => (double)(json_decode($item[$ore],true)[$selectedAlloy])); // } echo json_encode($finalArry); ?> <?php // ini_set('display_errors', '1'); // ini_set('display_startup_errors', '1'); // error_reporting(E_ALL); // include('db.php'); // $matrialTypeToGetEle = $_POST["matrialTypeToGetEle"]; // $ore = $_POST["ore"]; // $dateUserSelected = $_POST['dateSelectedFromCalender']; // $duration = "-". $_POST['duration'] . " month"; // $selectedAlloy = $_POST["selectedAlloy"]; // $db_current_date_query = "SELECT DISTINCT `date` FROM `market_prices` WHERE type = '$matrialTypeToGetEle' AND description = '$selectedAlloy' ORDER BY date DESC LIMIT 1"; // $db_current_date = mysqli_fetch_all(mysqli_query($conn,$db_current_date_query,MYSQLI_ASSOC))[0][0]; // $today = new DateTime($db_current_date); // Create a DateTime object for today's date // // $monthString = // $oneMonthAgo = $today->modify($duration); // Modify the object to go back one month // // Format the date in your desired format // $formattedBeforeDate = $oneMonthAgo->format('Y-m-d'); // // echo json_encode($db_current_date);exit; // // echo $formattedBeforeDate;exit; // $before_date = date("Y-m-d",strtotime($duration)); // //$query ="SELECT `date` AS x,`price` AS 'y' FROM `reia_market_prices` where `date` BETWEEN '$before_date' and '$dateUserSelected'AND description='$selectedAlloy' GROUP BY `date` ORDER BY `date` ASC"; // //$query = "SELECT `date`, `$ore` FROM `market_data` where `date` BETWEEN '$before_date' and '$today_date' GROUP BY `date` ORDER BY `date` ASC "; // $query ="SELECT `date` AS x,`price` AS 'y' FROM market_prices WHERE `date` BETWEEN '$formattedBeforeDate' and '$db_current_date' AND description = '$selectedAlloy' AND price > 0 ORDER BY `date` DESC LIMIT 0, 100000"; // $ores = mysqli_query($conn, $query); // //echo $query; // $finalArry = mysqli_fetch_all($ores,MYSQLI_ASSOC); // //$resultArry = []; // // foreach ($finalArry as $item) { // // $resultArry[] = array('label' => $item["date"],"y" => (double)(json_decode($item[$ore],true)[$selectedAlloy])); // // } // echo json_encode($finalArry); ?>