lastErrorMsg()); } // Initialize the highest value $highestValue = 0; // Retrieve data from the "inv" column in the "figures" table $query = $db->query('SELECT invoicefull FROM figures'); while ($row = $query->fetchArray(SQLITE3_ASSOC)) { $inv_value = $row['invoicefull']; $result = preg_replace("/[^0-9]/", "", $inv_value); // Extract the last 4 digits from the result $last4Digits = substr($result, -4); $currentYear = date("Y"); if (strpos($last4Digits, $currentYear) !== false) { $result = substr($result, 0, -4); $result = intval($result); // Update the highest value if the current value is greater if ($result > $highestValue) { $highestValue = $result; } } else { $result = intval(0); } } // Close the database connection $db->close(); // Check for errors and handle them if ($highestValue === 0) { echo json_encode(["highestValue" => $highestValue]); } else { if (!$highestValue){ $highestValue = intval(0); } else { // Output the processed result echo json_encode(["highestValue" => $highestValue]); } } ?>