Jump to content

Foreach loop help ----- Calculator . . .


dlf

Recommended Posts

If anyone knows of the Nintendo Wii Virtual Console; that's good, I am making a 'simple' calculator that allows you (regardless of region) to find how many Wii points you would have [i]after[/i] you downloaded some retro games onto the Wii. All I need is help with a foreach loop. Here is the PHP file (that does the interpreting of the form):

<?PHP
// ======================================================================
=====
// NORTH AMERICA
// ======================================================================
=====
$_PRICES['NES']  = 500;
$_PRICES['SNES']  = 800;
$_PRICES['N64']  = 1000;
$_PRICES['GEN']  = 800;
$_PRICES['TURBO']  = 600;
//---------------------------------------------------------------------------
// SOUTH AMERICA
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// EUROPE
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ASIA
//---------------------------------------------------------------------------
/*
$_PRICES['FAM']  = 500;
$_PRICES['SFAM']  = 800;
$_PRICES['N64']  = 1000;
$_PRICES['MASTER']  = 800;
$_PRICES['PC']  = 600;
*/
//---------------------------------------------------------------------------
// OTHER
//---------------------------------------------------------------------------

$total = 0;

foreach ($_PRICES as $current_game {

}

?>


Early December I had help with this same thing from my brother; it went onto a (currently down) 'server' as such I can't get the code but from the 1st calclulator I remember the loop having:

$_POST [i]in it (I think [b]$_POST['before'])[/b][/i], [b]$total[/b], [i]+=[/i], [i]$current_game[/i]. And another variable that I can't remember the name or what it was set as.


Also as I said before the PHP file interpreted a form which is:


<html>
<head>
<title>Virtual Console Calculator</title>
Welcome one, welcome all! <br />To this <em>Virtual Console Calculator</em>. This script is to help you decide on what <em>Virtual Console</em> games you'd like to download onto your Nintendo Wii. Simply check the boxes of the game(s) you'd like to download &amp; (how many wii points you'd have after the last download is done), and input (at the bottom) amount of Wii points you have, and submit.
<body>
<form action=wii.php method=POST>
<table border=0 width=100%>
<tr>
<td><input type="checkbox" /><strong>N</strong>intendo <strong>E</strong>ntertainment <strong>S</strong>ystem
<td><input type="checkbox" name="NES" />Donkey Kong </td>
<td><input type="checkbox" name="NES" />Mario Bros </td>
<td><input type="checkbox" name="NES" />Pinball </td>
<td><input type="checkbox" name="NES" />Soccer </td>
<td><input type="checkbox" name="NES" />Solomon's Key </td>
<td><input type="checkbox" name="NES" />Legend of Zelda </td>
<td><input type="checkbox" name="NES" />Wario's Woods </td>
<td><input type="checkbox" name="NES" />Donkey Kong Jr </td>
<td><input type="checkbox" name="NES" />Ice Hockey </td>
<td><input type="checkbox" name="NES" />Tennis </td>
<td><input type="checkbox" name="NES" />Super Mario Bros </td>
<td><input type="checkbox" name="NES" />Baseball </td>
<td><input type="checkbox" name="NES" />Urban Champion </td>
</tr>
<tr>
<td><input type="checkbox" /><strong>S</strong>uper <strong>N</strong>intendo <strong>E</strong>ntertainment <strong>S</strong>ystem</td>
<td><input type="checkbox" name="SNES" />F-Zero </td>
<td><input type="checkbox" name="SNES" />SimCity </td>
<td><input type="checkbox" name="SNES" />Street Fighter II: The World Warrior </td>
<td><input type="checkbox" name="SNES" />Super Castlevania IV </td>
</tr>
<tr>
<td><input type="checkbox" /><strong>N</strong>intendo <strong>64</strong>
<td><input type="checkbox" name="N64" />Mario 64 </td>
</tr>
<tr>
<td><input type="checkbox" name="GEN" />Sega Genesis
<td><input type="checkbox" name="GEN" />Altered Beast </td>
<td><input type="checkbox" name="GEN" />Sonic The Hedgehog </td>
<td><input type="checkbox" name="GEN" />Ecco the Dolphin </td>
<td><input type="checkbox" name="GEN" />Golden Axe </td>
<td><input type="checkbox" name="GEN" />Columns </td>
<td><input type="checkbox" name="GEN" />Ristar </td>
<td><input type="checkbox" name="GEN" />Dr. Robotnik's Mean Bean Machine </td>
<td><input type="checkbox" name="GEN" />Gunstar Heroes </td>
<td><input type="checkbox" name="GEN" />Space Harrier II </td>
<td><input type="checkbox" name="GEN" />ToeJam & Earl </td>
</tr>
<tr>
<td><input type="checkbox" name="TURBO" />Turbo Grafix 16 </td>
<td><input type="checkbox" name="TURBO" />Bomberman '93 </td>
<td><input type="checkbox" name="TURBO" />Bonk's Adventure </td>
<td><input type="checkbox" name="TURBO" />Super Star Soldier </td>
<td><input type="checkbox" name="TURBO" />Victory Run </td>
<td><input type="checkbox" name="TURBO" />Alien Crush </td>
<td><input type="checkbox" name="TURBO" />Military Madness </td>
<td><input type="checkbox" name="TURBO" />R-Type </td>
</tr>
</table>
<input type="text" value="" /> <input type="submit" value="Calculate Wii points" /> <input type="reset" value="Reset this form" />
</form>
</body>
</head>
</html>



Think someone could help me?  To confusing/long?
Link to comment
Share on other sites

1st change names of your checkbox - add [] in the end of name (name="NES" => name="NES[]" etc.) [code]
$total = 0;
foreach ($_PRICES as $name => $price) {
      $total += count($_POST[$name]) * $price;
}
echo $total;
[/code]

Link to comment
Share on other sites

Whoops . . . The original had (for the operators/operands) just +=. & No outside 'functions like count(). The script should actually function like a calculator: Input (select the check boxes of the games) & amount of Wii points you have & it would/will output how many you have left.

Example:

NES are 500 a piece. I 'download' 3 (1500) and have 3,000 'total' (before the download) after I should have 1500. And [i]1500[/i] should be outputted to the screen if I select three NES games & input 3000 wii points.
Link to comment
Share on other sites

  • 2 weeks later...
Sorry for the triple post but may I have some help? The script (wii.php or form.html) does [b]NOT[/B] use functions, if statements, case-statements, tearny operators. Just $_PRICES, $current_game, $_POST, $total, += & a foreach loop.
Link to comment
Share on other sites

Ahh Super Mario Bro's... I remember that game - might play that some later...

Anyways

the form page
[code=php:0]
<html>
<head>
<title>Virtual Console Calculator</title>
Welcome one, welcome all!
To this Virtual Console Calculator. This script is to help you decide on what Virtual Console games you'd like to download onto your Nintendo Wii. Simply check the boxes of the game(s) you'd like to download &amp; (how many wii points you'd have after the last download is done), and input (at the bottom) amount of Wii points you have, and submit.
<body>
<form action="wii.php" method=POST>
<table border=0 width=100%>
<tr>
<td><input type="checkbox" /><strong>N</strong>intendo <strong>E</strong>ntertainment <strong>S</strong>ystem
<td><input type="checkbox" name="NES[]" />Donkey Kong </td>
<td><input type="checkbox" name="NES[]" />Mario Bros </td>
<td><input type="checkbox" name="NES[]" />Pinball </td>
<td><input type="checkbox" name="NES[]" />Soccer </td>
<td><input type="checkbox" name="NES[]" />Solomon's Key </td>
<td><input type="checkbox" name="NES[]" />Legend of Zelda </td>
<td><input type="checkbox" name="NES[]" />Wario's Woods </td>
<td><input type="checkbox" name="NES[]" />Donkey Kong Jr </td>
<td><input type="checkbox" name="NES[]" />Ice Hockey </td>
<td><input type="checkbox" name="NES[]" />Tennis </td>
<td><input type="checkbox" name="NES[]" />Super Mario Bros </td>
<td><input type="checkbox" name="NES[]" />Baseball </td>
<td><input type="checkbox" name="NES[]" />Urban Champion </td>
</tr>
<tr>
<td><input type="checkbox" /><strong>S</strong>uper <strong>N</strong>intendo <strong>E</strong>ntertainment <strong>S</strong>ystem</td>
<td><input type="checkbox" name="SNES[]" />F-Zero </td>
<td><input type="checkbox" name="SNES[]" />SimCity </td>
<td><input type="checkbox" name="SNES[]" />Street Fighter II: The World Warrior </td>
<td><input type="checkbox" name="SNES[]" />Super Castlevania IV </td>
</tr>
<tr>
<td><input type="checkbox" /><strong>N</strong>intendo <strong>64</strong>
<td><input type="checkbox" name="N64" />Mario 64 </td>
</tr>
<tr>
<td><input type="checkbox" name="GEN[]" />Sega Genisis
<td><input type="checkbox" name="GEN[]" />Altered Beast </td>
<td><input type="checkbox" name="GEN[]" />Sonic The Hedgehog </td>
<td><input type="checkbox" name="GEN[]" />Ecco the Dolphin </td>
<td><input type="checkbox" name="GEN[]" />Golden Axe </td>
<td><input type="checkbox" name="GEN[]" />Columns </td>
<td><input type="checkbox" name="GEN[]" />Ristar </td>
<td><input type="checkbox" name="GEN[]" />Dr. Robotnik's Mean Bean Machine </td>
<td><input type="checkbox" name="GEN[]" />Gunstar Heroes </td>
<td><input type="checkbox" name="GEN[]" />Space Harrier II </td>
<td><input type="checkbox" name="GEN[]" />ToeJam & Earl </td>
</tr>
<tr>
<td><input type="checkbox" name="TURBO[]" />Turbo Grafix 16 </td>
<td><input type="checkbox" name="TURBO[]" />Bomberman '93 </td>
<td><input type="checkbox" name="TURBO[]" />Bonk's Adventure </td>
<td><input type="checkbox" name="TURBO[]" />Super Star Soldier </td>
<td><input type="checkbox" name="TURBO[]" />Victory Run </td>
<td><input type="checkbox" name="TURBO[]" />Alien Crush </td>
<td><input type="checkbox" name="TURBO[]" />Military Madness </td>
<td><input type="checkbox" name="TURBO[]" />R-Type </td>
</tr>
</table>
<input type="text" value="0" name="startpoints" /> <input type="submit" value="Calculate Wii points" /> <input type="reset" value="Reset this form" />
</form>
</body>
</head>
</html>
[/code]

And wii.php

[code=php:0]
<?
$start = $_POST['startpoints'];

if(!is_numeric($start)) {
die("Please enter your starting points.");
}
$_PRICES['NES']  = 500;
$_PRICES['SNES']  = 800;
$_PRICES['N64']  = 1000;
$_PRICES['GEN']  = 800;
$_PRICES['TURBO']  = 600;

$nescount = count($_POST['NES']);
$snescount = count($_POST['SNES']);
$n64count = count($_POST['N64']);
$gencount = count($_POST['GEN']);
$turbocount = count($_POST['TURBO']);

$start -= ($nescount * $_PRICES['NES']);
$start -= ($snescount * $_PRICES['SNES']);
$start -= ($n64count * $_PRICES['N64']);
$start -= ($gencount * $_PRICES['GEN']);
$start -= ($turbocount * $_PRICES['TURBO']);

echo "You would have {$start} points remaining!";
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.