tags:

views:

92

answers:

4

Goal: User only selects one, only one option at a given time for the denomination, either some value of Nickels only, some value of Dimes only, or some value of Quarters only.

Problems: Currently the code only defaults to Nickels only?

Pre-existing condtion: If I select a different denomination, for example, Quarters only, I only get the values for Nickels, the same applies for Dimes?

Code Snippet:

if($denomination["Nickels"] != NULL)

{
$value = $denomination["Nickels"];
echo $value . " is the value of selected Nickels";
}

else if ($denomination["Dimes"] != NULL)
{
$value = $denomination["Dimes"];
echo $value . " is the value of  selected Dimes";
}

else if ($denomination["Quarters"] != NULL)
{
$value = $denomination["Quarters"];
echo  $value . "is the value of selected Quarters";
}
+2  A: 

The "elseif" stops processing when the first condition is true (i.e. "Nickels"). Remove the "else"s and leave plain "if"s.

Pekka
A: 

Are you explicitly setting the values in the $denomination array to null?

Have you trying something like...

if($denomination["Nickels"])
{
$value = $denomination["Nickels"];
echo $value . " is the value of selected Nickels";
}

else if ($denomination["Dimes"])
{
$value = $denomination["Dimes"];
echo $value . " is the value of  selected Dimes";
}

else if ($denomination["Quarters"])
{
$value = $denomination["Quarters"];
echo  $value . "is the value of selected Quarters";
}
Chris Gutierrez
No, but I assure you, I will be in the code cave with all this good stuff experimenting, until I get this baby of the ground, so yes I will by trying your code Chris Gutierrez, and thank you.
Newb
+2  A: 

To make it short:

<?php
$value = array_shift(array_filter($denomination));
echo  $value . "is the value of selected Quarters";

With this you don't have to put in a if for every new element.

hubbl
That's an interesting technique, thanks.
Newb
A: 

Try not using the elseifs, replace them with ifs

if($denomination["Nickels"] != NULL)

{
$value = $denomination["Nickels"];
echo $value . " is the value of selected Nickels";
}

if ($denomination["Dimes"] != NULL)
{
$value = $denomination["Dimes"];
echo $value . " is the value of  selected Dimes";
}

if ($denomination["Quarters"] != NULL)
{
$value = $denomination["Quarters"];
echo  $value . "is the value of selected Quarters";
}
Kshitij Parajuli