dlf Posted January 4, 2007 Share Posted January 4, 2007 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 & (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? Quote Link to comment https://forums.phpfreaks.com/topic/32892-foreach-loop-help-calculator/ Share on other sites More sharing options...
sasa Posted January 4, 2007 Share Posted January 4, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/32892-foreach-loop-help-calculator/#findComment-153137 Share on other sites More sharing options...
dlf Posted January 5, 2007 Author Share Posted January 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32892-foreach-loop-help-calculator/#findComment-153203 Share on other sites More sharing options...
dlf Posted January 6, 2007 Author Share Posted January 6, 2007 Sorry for the double post but I [i]do[/i] want this done. Heh. Quote Link to comment https://forums.phpfreaks.com/topic/32892-foreach-loop-help-calculator/#findComment-154304 Share on other sites More sharing options...
dlf Posted January 14, 2007 Author Share Posted January 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32892-foreach-loop-help-calculator/#findComment-160691 Share on other sites More sharing options...
corbin Posted January 14, 2007 Share Posted January 14, 2007 Ahh Super Mario Bro's... I remember that game - might play that some later...Anywaysthe 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 & (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] Quote Link to comment https://forums.phpfreaks.com/topic/32892-foreach-loop-help-calculator/#findComment-160720 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.