Smudly Posted July 13, 2010 Share Posted July 13, 2010 Hi, I'm working on a simple calculation script that will start with two numbers (these chosen by user). The user chooses a number, and it is set for both numbers. These two values are placed inside an array. The script adds these two numbers, and gets a new value. The value that was in $array[1] moves to $array[0], and the new value is now placed inside $array[1]. This calculation keeps going one and on, adding the values inside the array. By typing in 1 for my start value, I expected my code to output: 1, 1, 3. 5, 8, 13, 21, 34, 55, etc. However, right now it is outputting the following: 1 1 2 3 5 8 13 9 10 10 2 3 5 8 13 9 10 10 2 3 5 8 13 here is my code below. What can you see that I have done wrong? Any help appreciated. <?php $submit = $_POST['submit']; $base = $_POST['base']; $array = ""; ?> <html> <body OnLoad="document.price.base.focus();"> <center> <form name="price" action="calculate.php" method="POST"> Base Price: $<input type="text" name="base"> <input type="submit" name="submit" value="Submit"> </form> <h1>$<?php echo $base; ?></h1> <br /> </center> </body> </html> <?php if($submit){ $array .= $base; $array .= $base; echo $array[0]."<br />".$array[1]."<br />"; $newvalue = $array[0] + $array[1]; $array[0] = $array[1]; $array[1] = $newvalue; echo $newvalue."<br />"; $end = 0; while ($end<1000){ $number = $array[0] + $array[1]; echo $number."<br />"; $array[0] = $array[1]; $array[1] = $number; $end += 1; } } ?>[/php Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 13, 2010 Share Posted July 13, 2010 What exactly are we talking about here.... I can follow the code and see why it prints what it prints exactly. Are you trying to do this instead? say 1 & 1 entered..... so then next you want to push the second 1 to replace the first 1 right and put the sum in the second element so end up with 1 & 2... then print the sum "3" then push 2 to array[0] and 3 to [1] right.... then print "5" ... and so on and so forth? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 13, 2010 Share Posted July 13, 2010 I think your expected results are wrong. According to your explanation the actual results you show appear to be correct. In your example your starting two numbers are 1 and 1. You state that the two number should be added and that the first number will be removed and the seconf number will move to the first position while the result will go to position 2. Since you start with 1 and 1, the first sum would be 2. What are you really trying to achieve? Because what you are asking doesn't seem to make sense based upon the context of the code where you seem to be trying to calculate proce. Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted July 13, 2010 Share Posted July 13, 2010 Not sure what if what expected is actually what you wanted... need to change the initial array assignments from $array .= $base; $array.=$base; to $array[0] = $base; $array[1] = $base; But the following should work for what you were expecting to get <?php $submit = $_POST['submit']; $base = $_POST['base']; $array = ""; ?> <html> <body OnLoad="document.price.base.focus();"> <center> <form name="price" action="calculate.php" method="POST"> Base Price: $<input type="text" name="base"> <input type="submit" name="submit" value="Submit"> </form> <h1>$<?php echo $base; ?></h1> <br /> </center> </body> </html> <?php if($submit){ $array[0] = $base; $array[1] = $base; echo $array[0]."<br />".$array[1]."<br />"; $newvalue = $array[0] + $array[1]; $array[0] = $array[1]; $array[1] = $newvalue; echo $newvalue."<br />"; $end = 0; while ($end<1000){ $number = $array[0] + $array[1]; echo $number."<br />"; $array[0] = $array[1]; $array[1] = $number; $end += 1; } } ?> Quote Link to comment 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.