<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Alexandria:wght@300&display=swap" rel="stylesheet">
<style>
i.fas.fa-arrow-down {
color: red;
}
i.fas.fa-arrow-up {
color: yellowgreen;
}
i.fas.fa-circle {
color: yellow;
}
po {
color: white;
font-size: 14px;
font-family: 'Alexandria', sans-serif;
}
.fa-solid, .fas {
font-family: "Font Awesome 6 Free" !important;
font-weight: 900;
}
</style>
<div class="navbar" id="navGold">
<?php
function getGoldPrice($apiKey,$symbol) {
$curr = "USD";
$date = "";
$url = "https://www.goldapi.io/api/{$symbol}/{$curr}{$date}";
$myHeaders = array(
'x-access-token: ' . $apiKey,
'Content-Type: application/json'
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => $myHeaders
));
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
return 'Error: ' . $error;
} else {
$data = json_decode($response, true);
// Check if the response contains a 'price' key
if (isset($data['price'])) {
return $data['price'];
}
}
}
function fetchDataAndInsertIntoDatabase($pdo)
{
$apiKey = "goldapi-xxd2ktl4miz8nf-io";
$symbol = "XAU";
$curr = "SAR";
$date = "";
$myHeaders = array(
'x-access-token: ' . $apiKey,
'Content-Type: application/json'
);
$curl = curl_init();
$url = "https://www.goldapi.io/api/{$symbol}/{$curr}{$date}";
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => $myHeaders
));
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
// echo 'Error: ' . $error;
} else {
$data = json_decode($response, true);
if ($data !== null) {
$price_gram_24k = $data['price_gram_24k'];
$price_gram_22k = $data['price_gram_22k'];
$price_gram_21k = $data['price_gram_21k'];
$price_gram_20k = $data['price_gram_20k'];
$price_gram_18k = $data['price_gram_18k'];
$price_gram_16k = $data['price_gram_16k'];
$price_gram_14k = $data['price_gram_14k'];
$price_gram_10k = $data['price_gram_10k'];
$timestamp = $data['timestamp'];
$goldPrice = getGoldPrice($apiKey,$symbol);
try {
$stmt = $pdo->prepare("INSERT INTO gold (price_gram_24k, price_gram_22k, price_gram_21k, price_gram_20k, price_gram_18k, price_gram_16k, price_gram_14k, price_gram_10k, timestamp,price) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)");
// $stmt->execute([$price_gram_24k,$price_gram_22k, $price_gram_21k, $price_gram_20k, $price_gram_18k, $price_gram_16k,$price_gram_14k,$price_gram_10k, $timestamp,$goldPrice]);
$stmt->execute([rand(100000,200000),rand(100000,200000), rand(100000,200000), rand(100000,200000), rand(100000,200000), rand(100000,200000),rand(100000,200000),rand(100000,200000), $timestamp,$goldPrice]);
// echo 'Data inserted successfully!';
} catch (PDOException $e) {
// echo 'Error: ' . $e->getMessage();
} finally {
$pdo = null;
}
} else {
// echo 'Error decoding JSON';
}
}
}
function getLastButOneRecord($pdo)
{
try {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM gold ORDER BY id DESC LIMIT 1 OFFSET 1";
$statement = $pdo->query($sql);
$result = $statement->fetch(PDO::FETCH_ASSOC);
$pdo = null;
return $result;
} catch (PDOException $e) {
}
}
function icon($current_price, $previous_price)
{
$icon = '';
if ($current_price > $previous_price) {
$icon = 'fas fa-arrow-up';
} elseif ($current_price < $previous_price) {
$icon = 'fas fa-arrow-down';
} else {
$icon = 'fas fa-circle';
}
return $icon;
}
function display_price_status($current_price, $previous_price, $string)
{
$icon=icon($current_price,$previous_price); // سيحتوي على النص الناتج
return $result = sprintf(
"<a href='#'><i class=%s> <po>%s</po> : %s</i></a>",
$string,
$current_price
);// تعديل النص الناتج حسب الحالة
}
$host = 'localhost';
$dbname = 'bahrgold_aa';
$username = 'bahrgold_aa';
$password = 'm-rScfOBs39u';
try {
// $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// fetchDataAndInsertIntoDatabase($pdo);
$sqlLatest = "SELECT * FROM gold ORDER BY id DESC LIMIT 1";
$stmtLatest = $pdo->query($sqlLatest);
// $rowLatest = $stmtLatest->fetch();
$rowLatest=array();
$rowLatest['price_gram_24k'] = '';
$rowLatest['price_gram_22k'] = '';
$rowLatest['price_gram_21k'] = '';
$rowLatest['price_gram_20k'] = '';
$rowLatest['price_gram_18k'] = '';
$rowLatest['price_gram_16k'] = '';
$rowLatest['price_gram_14k'] = '';
$rowLatest['price_gram_10k'] = '';
$currentFilePath = __FILE__;
$currentFilePath;
if ($rowLatest['price_gram_10k'] !== '') {
// $id = $rowLatest['id'];
$price_gram_24k_latest = $rowLatest['price_gram_24k'];
$price_gram_22k_latest = $rowLatest['price_gram_22k'];
$price_gram_21k_latest = $rowLatest['price_gram_21k'];
$price_gram_20k_latest = $rowLatest['price_gram_20k'];
$price_gram_18k_latest = $rowLatest['price_gram_18k'];
$price_gram_16k_latest = $rowLatest['price_gram_16k'];
$price_gram_14k_latest = $rowLatest['price_gram_14k'];
$price_gram_10k_latest = $rowLatest['price_gram_10k'];
$timestamp_latest = $rowLatest['timestamp'];
$price = $rowLatest['price'];
$rowPrevious=getLastButOneRecord($pdo);
$price_gram_24k_previous = $rowPrevious['price_gram_24k'];
$price_gram_22k_previous = $rowPrevious['price_gram_22k'];
$price_gram_21k_previous = $rowPrevious['price_gram_21k'];
$price_gram_20k_previous = $rowPrevious['price_gram_20k'];
$price_gram_18k_previous = $rowPrevious['price_gram_18k'];
$price_gram_16k_previous = $rowPrevious['price_gram_16k'];
$price_gram_14k_previous = $rowPrevious['price_gram_14k'];
$price_gram_10k_previous = $rowPrevious['price_gram_10k'];
$timestamp_previous = $rowPrevious['timestamp'];
$price_previous = $rowPrevious['price'];
function displayPriceChange($latestPrice, $previousPrice, $caliber)
{
if ($latestPrice > $previousPrice) {
$icon = 'fas fa-arrow-up';
} elseif ($latestPrice < $previousPrice) {
$icon = 'fas fa-arrow-down';
} else {
$icon = 'fas fa-arrow-up';
}
$latestPrice=round($latestPrice,2);
echo sprintf(
"<a href='#'> <i class='%s'> <po>%s</po> : %s </i> ريال </a>",
$icon,
$caliber,
$latestPrice,
);
}
function displayPriceChange__($latestPrice, $previousPrice, $caliber)
{
if ($latestPrice > $previousPrice) {
$icon = 'fas fa-arrow-up';
} elseif ($latestPrice < $previousPrice) {
$icon = 'fas fa-arrow-down';
} else {
$icon = 'fas fa-arrow-up';
}
$latestPrice=round($latestPrice,2);
echo sprintf(
"<a href='#'> <i class='%s'> <po>%s</po> : %s </i> </a>",
$icon,
$caliber,
$latestPrice,
);
}
displayPriceChange($price_gram_24k_latest, $price_gram_24k_previous, " عيار 24");
displayPriceChange($price_gram_22k_latest, $price_gram_22k_previous, " عيار 22");
displayPriceChange($price_gram_21k_latest, $price_gram_21k_previous, " عيار 21");
displayPriceChange($price_gram_18k_latest, $price_gram_18k_previous, " عيار 18");
displayPriceChange__($price, $price_previous, "الاونصة" );
} else {
// echo "لم يتم العثور على سجلات";
}
} catch (PDOException $e) {
// التعامل مع الأخطاء في حالة حدوث أي استثناء
// echo "خطأ في الاتصال بقاعدة البيانات: " . $e->getMessage();
}
?>
</div>
Comments