Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Array Trouble, calculating numbers
Old 07-13-2010, 02:44 PM Array Trouble, calculating numbers
Skilled Talker

Posts: 83
Trades: 0
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 Code:
<?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;
    
}
}
?>

Last edited by chrishirst; 07-13-2010 at 02:53 PM..
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-13-2010, 03:08 PM Re: Array Trouble, calculating numbers
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
it is called a Fibonacci sequence

http://www.codewalkers.com/c/a/Misce...sion-in-PHP/2/


You need to STEP THROUGH the arrary not keep iterating the loop using array[0] and array[1]
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Array Trouble, calculating numbers
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38195 seconds with 12 queries