Log in

View Full Version : Foreach loop help



dlf
01-04-2007, 09:28 PM
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 after 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 in it (I think $_POST['before']), $total, +=, $current_game. 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!
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 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?

thetestingsite
01-05-2007, 03:27 AM
I think I know what you are trying to accomplish but the code you posted (unless what I'm thinking you want is wrong) makes no sense as to how to accomplish your goal. Try the following code for wii.php:



<?php

$NES = $_REQUEST['NES'];
$SNES = $_REQUEST['SNES'];
$N64 = $_REQUEST['N64'];
$SEGA = $_REQUEST['GEN'];
$TURBO = $_REQUEST['TURBO'];

$nesCount = count($NES);
$snesCount = count($SNES);
$n64Count = count($N64);
$segaCount = count($SEGA);
$turboCount = count($turboCount);

$PRICES['NES'] = 500;
$PRICES['SNES'] = 800;
$PRICES['N64'] = 1000;
$PRICES['GEN'] = 800;
$PRICES['TURBO'] = 600;

$gTotal = ($PRICES['NES'] * $nesCount) + ($PRICES['SNES'] * $snesCount) + ($PRICES['N64'] * $n64Count) + ($PRICES['SEGA'] * $segaCount) + ($PRICES['TURBO'] * $turboCount);

echo 'Grand Total: <b>'.$gTotal.'</b> Points';
?>


Then the following for form.html (you should be able to see the changes I made):



<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><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><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><strong>N</strong>intendo <strong>64</strong>
<td><input type="checkbox" name="N64[]" />Mario 64 </td>
</tr>
<tr>
<td>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>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="submit" value="Calculate Wii points" /> <input type="reset" value="Reset this form" />
</form>
</body>
</head>
</html>


That will produce the following example:

If on form.html you choose "Donkey Kong", "Donkey Kong, Jr" (both NES), and (on N64) "Mario 64":

The page will show



Grand Total: 2000 points


If you need any further help, or if I am wrong as to what you want to accomplish, let me know.

dlf
01-05-2007, 03:35 AM
Uh no . . I want it output you'd have after you downloaded the games onto the Wii. If I select 3 NES games & input 3,000 Wii points I should get back like "You would have 1500 Wii points after your downloads".

thetestingsite
01-05-2007, 03:41 AM
So basically what you have now minus (-) what you select, right?

dlf
01-05-2007, 05:18 AM
So basically what you have now minus (-) what you select, right?

I guess, {but} add all the selections together - the Wii points you have.

thetestingsite
01-05-2007, 03:59 PM
Well then, add the following anywhere in form.html:



Current Points: <input type="text" name="current">


Then the following just underneath "<?php" in wii.php:



$current = $_REQUEST['current'];


Finally change the following at the bottom of the php (You will see what I have changed in red):



$gTotal = ($PRICES['NES'] * $nesCount) + ($PRICES['SNES'] * $snesCount) + ($PRICES['N64'] * $n64Count) + ($PRICES['SEGA'] * $segaCount) + ($PRICES['TURBO'] * $turboCount);

$gdTotal = $current - $gTotal;

echo 'Grand Total: <b>'.$gdTotal.'</b> Points';


Hope this helps.

dlf
01-06-2007, 03:52 AM
Still doesn't calculate.



have said COUNTLESS amount(s) of times that I want to use:

* $_POST*
* $current_game
* +=
* $total

How hard is it to get that? None of the topics i've posted about this (weather it be here, or other forums) uses $_POST, $current_game. Always changing it around, i'm trying to replicate (dupe/duplicate) exactly the original & the original had those in it.

* Possibly $_POST['before'] I can't remember.

That was from another topic I made on another forum. Note that wasn't aimed at anyone at this forum. I am just sick & tierd of pepeople changing what i've seen in the posts of the topics.