2
<h2>Fibonacci</h2>
        <form method="$_GET" action="Wk4.php">
            <fieldset>

                <label for="Number">Fibonacci: </label>
                <input type="text" name="Number" value="<?php echo $_GET['powerof']; ?>"/>
                <input type="submit" name='Go' value="Calculate" />
            </fieldset>
        </form>

        <?php
$message = 'The fibonacci sequence is: <br />1<br />2<br />';
$powerof = 0;
$max = 10;
$temp = $max;

if(isset($_GET['Go'])) {
    $powerof = $_GET['Go'];}else{$powerof = 2;
}

if($powerof > 100) {
    $powerof = 100;
    $message = 'Sorry, your input was too high. I converted it to the maximum value of 100.<br />The fibonacci sequence is: <br />1<br />2<br />';
}

$i = 1;

for($i;$i<$powerof;$i++){
    $max = $max * $temp;
}

$x = 1;
$y = 2;
$z = $x + $y;

echo($message);

while($z < $max) {

    $z = $x + $y; 
    echo($z."<br />"); 
    $x = $y;
    $y = $z;
}
    ?>

im trying to create a mechanism that allows me to enter an integer into the text box then once i hit the button it is returned listing all the fibonacci sequence up ton that number.

  • 1
    the method of the form should be `"get"` not `$_GET`. also `'go'` is the name of your submit button, whose value is `'Calculate'`, thus `$powerof` will be a string and not an integer. – Jeff Lambert Jun 03 '14 at 20:13
  • Welcome to SO! Please describe what exactly doesn't work. Wrong results? Error messages? – georg Jun 03 '14 at 20:14
  • the form just doesnt do anything will not work or return sequence upto said number – user3704591 Jun 03 '14 at 20:15
  • [`This is why`](http://stackoverflow.com/questions/24024105/fibonacci-sequence-listing-numbers-upto-integer#comment37031900_24024105) or a big reason why. – Funk Forty Niner Jun 03 '14 at 20:17
  • no it definitely needs to be an integer im just strugglin because i dont know what parts of my form should be called what ive just tried renaming it to powerof to try get it work – user3704591 Jun 03 '14 at 20:17

1 Answers1

0

Pretty sure now my comment should be an answer, so here it is. Couple of issues here:

1) $_GET is not a valid HTTP verb. Valid values* of the method attribute of a form are one of get, post, put, delete, and 'dialog' (HTML5+) (all case insensitive).

  • Since the default value for the method attribute is get for both empty an method attribute as well as when it is provided an invalid value, leaving it as is may still work, but it is not standard and how browsers behave when they see that may not be what you expect.

2) I believe your main problem is how you're trying to retrieve the input after the form submission. You named your input as powerof in this line:

<input type="text" name="powerof" value="<?php echo $_GET['powerof']; ?>"/>

Therefore you should change this:

if(isset($_GET['Go'])) {
    $powerof = $_GET['Go'];}else{$powerof = 2;
}

To this:

if(isset($_GET['powerof'])) {
    $powerof = $_GET['powerof'];}else{$powerof = 2;
}

Update

From your comments, I don't think you're exactly answering the question correctly for the interview. Also, if the user enters 1 or 2, you're not going to return the correct value. (The fibonacci sequence begins as 1, 1, 2, 3, 5, 8**, ..., so input of 2 should return 1).

Try changing your while loop to look like this:

$x = 0;
$y = 1;
$z = 0;
$counter = 0;

while($counter < $powerof) {
    if($counter <= 1) {
        $z = 1;
    } else {
        $z = $x + $y;
    }
    $x = $y;
    $y = $z;
    $counter++;
}

echo 'Ouput: ' . $z . "<br>";

* According to this, only get and post were standardized in HTML v4, but today all of the others are supported by all the major browsers. Interested readers may find the bug submission regarding the HTML v5 standard related to this issue.. interesting. Also, according to the HTML5 standard, a value of 'dialog' is also supported.

** According to Wikipedia, there's not a 'standard' starting point of the sequence (should 0 be considered a fibonacci number or not?). According to this answer, using 0 as the starting point is known as the 'classical' definition, but using 1 as the starting point is known as the 'combinatorial' definition. You might get bonus points in the interview if you bring that up.

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • i'll try this then get back to you with result thanks a lot for help as i need to do and learn this for an interview and to be honest i dont have a clue about how to write this sequence into php ive been struggling so much – user3704591 Jun 03 '14 at 20:23
  • it works but there is an issue and i cant see what's making it do it, when i enter any number its listing 5 numbers for example if i say 1 it displays as normal 1 2 3 5 8 13 whereas if i say 2 it lists 1 3 5 8 13 21 34 55 89 144 rather than just 2 numbers after 13 – user3704591 Jun 03 '14 at 20:39
  • I think that issue is probably related to how you're setting `$max`. What exactly are you trying to do? If you input 2 does that mean I want the first 2 fibbonacci numbers, or is it I want all the fibonacci numbers less than or equal to 2? – Jeff Lambert Jun 03 '14 at 20:42
  • 1. Create a mechanism to pass in a positive integer and return that value in the Fibonacci series.. thats the qustion im trying to model my code around – user3704591 Jun 03 '14 at 20:47
  • Thats still slightly ambiguous, but I read that as the input of 2 should return the second fibonacci number (i.e. 1) – Jeff Lambert Jun 03 '14 at 20:51
  • i tried changing the for($i;$i<$powerof;$i++){ $max = $max * $temp; } too for($i;$i<$powerof;$i){ $max = $max * $temp; } but that seemed to just break my code, do you know any good sites that would be helpful for this kind of thing? – user3704591 Jun 03 '14 at 20:59
  • see my update. that should get you the correct nth fibonacci number – Jeff Lambert Jun 03 '14 at 21:01
  • your knowledge of this is brilliant, right so instead of listing them all the question just wants to see the output showing that nth numbers value, any chance you'd want to assist me on the other four questions haha – user3704591 Jun 03 '14 at 21:09
  • accept my answer and post the other questions and we'll see ;) however, keep in mind if you do that you'd probably be doing yourself a minor disservice. sometimes the only way to gain knowledge is through pain and practice. – Jeff Lambert Jun 03 '14 at 21:11
  • ive tried the hard way mate and im running low on time my interview is on thursday, i tried asking my tutor for help but he's on holiday. 2. Create a mechanism to pass in a positive integer and display all the values of the Fibonacci series up to and including the specified value 3. Create a mechanism to pass in a positive integer and display all the values of the Fibonacci series up to and including the specified value in reverse order – user3704591 Jun 03 '14 at 21:17
  • 4. Create a mechanism to pass in a positive integer and display the operand, the Fibonacci series number of the operand and the sum of all the values of the Fibonacci series up to and including the specified value. 5. Create a mechanism to pass in a positive integer and display the operand, the Fibonacci series number of the operand and the sum of all the values of the Fibonacci series up to and including the specified value, then decrease the operand by one and repeat until the operand is zero. – user3704591 Jun 03 '14 at 21:18
  • I'll say that for #2 and #3, you're practically already there or really close. I'm hesitant to put too much more here in the comments of this answer to this specific question due to the [format of the site](http://stackoverflow.com/help), but feel free to [keep asking questions](http://stackoverflow.com/questions/ask) – Jeff Lambert Jun 04 '14 at 03:41
  • have you got like a contact email or anything? – user3704591 Jun 04 '14 at 16:21