We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,664 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Print fibonacci series using cookies

I'm trying to create a php script to print fibonaaci series. The idea is to add a new number to the series everytime the page is refreshed and print the series.

<?php

if (!isset($_COOKIE["fno"]))
    {setcookie("fno",0,time()+3600);}
if (!isset($_COOKIE["sno"]))
    {setcookie("sno",1,time()+3600);}
if (!isset($_COOKIE["series"]))
    {
    $text="0 1 ";
    setcookie("series",$text,time()+3600);
    }
    $fno=$_COOKIE["fno"];
    $sno=$_COOKIE["sno"];
    $fibostring=$_COOKIE["series"];
    //echo $fibostring;
    $tno=$fno+$sno;
    $fibostring.=$tno . " ";
    setcookie("fno",$sno,time()+3600);
    setcookie("sno",$tno,time()+3600);
    setcookie("series",$fibostring,time()+3600);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Fibonacci Series</title>
</head>
<body bgcolor="#FFFFFF">
<div style="font-size:20px; color:#cc0000; margin-top:10px"><?php echo $fibostring ?></div>
</body>
</html>

Check out : din3shie.x10.mx/fibo.php
Can anybody tell me what's wrong ?

4
Contributors
4
Replies
4 Months
Discussion Span
4 Months Ago
Last Updated
5
Views
din3sh
Newbie Poster
12 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@din3sh

I'm trying to create a php script to print fibonaaci series. The idea is to add a new number to the series everytime the page is refreshed and print the series.

I know it's couple months late.

You need to add a loop to create a php fibonacci.

You can read this and try the code:

http://www.phpmysqlbrain.com/scripts/php-fibonacci-series-printing/

and read this and try the code too:

http://www.w3programmers.com/number-manipulation-with-php/

LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

To add a new fibonacci number on each refresh you should store each previous and current value in a session.

session_start();

// on the beginning you have 0 and 1
if(!isset($_SESSION['previous']) || !isset($_SESSION['current'])) {

    $_SESSION['previous'] = 0;
    $_SESSION['current'] = 1;

    echo $_SESSION['previous'];
    echo ' ';
    echo $_SESSION['current'];

} else {

    $current = $_SESSION['previous'] + $_SESSION['current'];

    echo $current;

    $_SESSION['previous'] = $_SESSION['current'];
    $_SESSION['current'] = $current;
}
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

I doubt the OP is still awaiting an answer, but for anybody else -

Agreed - sessions would be the way to go IMO.

diafol
Keep Smiling
Moderator
10,625 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

I have just noticed that the question is mentioning cookies. Well the principle is the same as using session.

Anyway, it was just nice to do a little brain exercise.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1019 seconds using 2.66MB