Simple PHP Table Question

I have a quick coding problem. I will post my php code below this message. I have everything working except for the column headers for the table. I can get the first column header (“DOLLARS”) to work but I can’t figure out where to put the header code to get the second column header to work. The second header uses the php variable $currency so that whichever foreign currency that the user wants to convert from dollars to $currency (i.e. convert DOLLARS into PESOS or EUROS etc). Anyway here is the code with the problem line highlighted:

Currency Converter
<?php
	$country = $_POST['country'];
		print("<h1>Currency Converter: $country</h1>");
		print("<table border=\"1\">");

[size=18pt]
print(“

DOLLARS $currency ”);[/size]
	for ($usDollar = 100; $usDollar <= 1000; $usDollar = $usDollar + 100)
	{
		print("<tr>");
	if($country == "Chile")
	{
		$currency = "PESOS";
		$conversionFactor = 466;
	
	}
	elseif($country == "Egypt")
	{
		$currency = "POUNDS";
		$conversionFactor = 5.94293;
	}  
	elseif($country == "Italy")
	{
		$currency = "EUROS";
		$conversionFactor = 0.68822;
	}
	elseif($country == "Japan")
	{
		$currency = "YEN";
		$conversionFactor =  80.78;
	}
	elseif($country == "Spain")
	{
		$currency =  "EUROS";
		$conversionFactor = 0.68822;
	
		
		
	
		
	}
	
	$convertedCurrency = $conversionFactor * $usDollar;
	
	
		

		
		print("<tr>");
		print("<td>$".number_format($usDollar, 2)."</td>");
		print("<td>$".number_format($convertedCurrency, 2)."</td>");
		print("</tr>");
	}
	print("</table>");
?>

You need to define the $currency variable.

Try adding this after $country = $_POST[‘country’];
[php]$currency = $_POST[‘currency’];[/php]

[php]

Currency Converter <?php $country = $_POST['country']; print("

Currency Converter: $country

"); print(""); if($country == "Chile") { $currency = "PESOS"; $conversionFactor = 466; } elseif($country == "Egypt") { $currency = "POUNDS"; $conversionFactor = 5.94293; } elseif($country == "Italy") { $currency = "EUROS"; $conversionFactor = 0.68822; } elseif($country == "Japan") { $currency = "YEN"; $conversionFactor = 80.78; } elseif($country == "Spain") { $currency = "EUROS"; $conversionFactor = 0.68822; } print(""); for ($usDollar = 100; $usDollar <= 1000; $usDollar = $usDollar + 100) { $convertedCurrency = $conversionFactor * $usDollar; print(""); print(""); print(""); print(""); } print("
DOLLARS $currency
$".number_format($usDollar, 2)."$".number_format($convertedCurrency, 2)."
"); ?> [/php]

You were trying to use $currency before it had been set. So it was outputting blank then setting it accordingly. Hope this is what you were meaning

Sponsor our Newsletter | Privacy Policy | Terms of Service