John Davidson

php - Foreach loop shows the last key in select menu

0 comments
Message:


i am making a crypto converter as a project for my school. I am using a api key for that and the array of the api is:


array(10) {
["ADA"]=>
float(1.024301)
["BNB"]=>
float(402.82399)
["BTC"]=>
float(40678.195596)
["DOGE"]=>
float(0.140886)
["ETH"]=>
float(2877.712908)
["LTC"]=>
float(119.283982)
["NEO"]=>
float(26.72906)
["USDT"]=>
float(1.008932)
["XMR"]=>
float(163.338038)
["XRP"]=>
float(0.780664)
}

And this the loop i am using to loop through array in a select menu:


<form method="post" action="">
<input type="text" name="amount" placeholder="Enter Amount"><br><br>
<select name="from">
<?php
Foreach ($arr_usd["rates"] as $currency => $rates) {
print "<option value='$rates'>$currency</option>";
}
?>
</select>
<select name="to">
<option value="USD">US Dollar</option>
<option value="EUR">Euro</option>
</select>
<br><br>
<button class="btn" name="convert_crypto">Convert</button>
<div id="result"><?php include "post.php" ?>
</div>
</form>

The select menu works as it should be, it displays the crypto names. The calculation of the rates works too.

But when i echo the answer, it echoes the last key of the currency in this case XRP.


if(isset($_POST['convert_crypto'])) {
$amount = $_POST['amount'];
$from = $_POST['from'];
$to = $_POST['to'];
if ($to == "USD") {
$result = ($amount * round($from,2));
echo $amount . " " . $currency . " = " . $result . " US Dollar";
}
}


My Answer:
BTC] => Bitcoin
[ETH] => Ethereum
[XRP] => Ripple
[LTC] => Litecoin
[BCH] => Bitcoin Cash
[ADA] => Cardano
[XLM] => Stellar
[NEO] => NEO
[EOS] => EOS
[XMR] => Monero
}

I am using a foreach loop to display these currencies in a select menu in my HTML form. However, when I run the code, it only shows the last key "XMR" in the select menu. Here is my code:

php



Can someone please help me figure out why only the last key is being displayed in the select menu and how I can fix this issue?

Rate this post

3 of 5 based on 8243 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us