Page 1 of 1

Array Trouble, calculating numbers

Posted: Tue Jul 13, 2010 1:42 pm
by Smudly
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.

Code: Select all

<?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;
	
}
}
?>

Re: Array Trouble, calculating numbers

Posted: Tue Jul 13, 2010 2:36 pm
by requinix
You aren't using arrays.

PHP manual