global variables

Discussion in 'PHP' started by jjh, Apr 13, 2007.

  1. #1
    I have a program that is to count the number of times a button is pressesd and then do differnet things eg
    $k1 = 0;
    $k2 = 0;
    if ($_SESSION['init'] == 1) {
    $k1 ++;
    echo $k1;
    }
    elseif ($_SESSION['init'] == 2)
    {
    $k2 ++;
    echo $k2;
    }
    This updates the values but what I need to do is for the increment to update the value of k1 and k2 so that they can be summed when
    ($_SESSION['init'] == 10). Any ideas?
     
    jjh, Apr 13, 2007 IP
  2. OoteR

    OoteR Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    uhh, from what you are asking it looks like all you want is something like this?
    
    
    elseif($_SESSION['init'] == 10)
    {
    $k3 = $k2+$k1;
    echo $k3;
    }
    
    PHP:
     
    OoteR, Apr 13, 2007 IP
  3. jjh

    jjh Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That is what I am trying to do but the problem is that the values of k1 and k2 do not hold when the session changes so all that is output is 0 (there initial values)
     
    jjh, Apr 14, 2007 IP
  4. PresFox

    PresFox Active Member

    Messages:
    218
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #4
    Do you have session_start(); in your code? else $_SESSION will allways be empty
     
    PresFox, Apr 14, 2007 IP
  5. jjh

    jjh Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yeah I have a session start but cant get the new variables to remain permanently whatever the session. Any ideas??

    <?php
    session_start();
    $k1 = 0;
    $k2 = 0;
    if(isset($_GET['rst']))
    { session_destroy();
    header("Location: $_SERVER[PHP_SELF]");
    }
    function add()
    {
    if (!isset($_SESSION['init']))
    {
    $_SESSION['init'] = 1;
    }
    else
    {
    $_SESSION['init']++;
    }
    return $_SESSION['init'];
    }
    if ($_SESSION['init'] == 1)
    {
    $k1 ++;
    echo $k1;
    }
    elseif ($_SESSION['init'] == 2)
    {
    $k2 ++;
    echo $k2;
    }
    elseif ($_SESSION['init'] == 3)
    {
    $k3 = $k1+$k2;
    echo $k3;
    }
    ?>
     
    jjh, Apr 14, 2007 IP
  6. PresFox

    PresFox Active Member

    Messages:
    218
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #6
    not sure what you are doing here, where are you executing your functions?
     
    PresFox, Apr 14, 2007 IP
  7. jjh

    jjh Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The functions are writtten in javascript

    <html>
    <head>
    <title>Hello!</title>
    <script language="javascript">
    <!--
    function refresh()
    {
    window.location.reload( false );
    }
    //-->
    </script>
    </head>
    <body>
    <div align="center">
    <?=add()?>
    <p>
    <input type="button" onclick="refresh()" value="Refresh" name="button1">
    <input type="button" value="Reset" onClick="document.location='<?=$_SERVER['PHP_SELF'].'?rst'?
    >';">

    </body>
    </html>
     
    jjh, Apr 14, 2007 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Just because you're using a session doesn't mean that all variables are registered with that session. Hence the reason you need to use $_SESSION['init'] instead of just $init!

    If you want to remember the values of k1 and k2 across a session, you have to have them registered with the session. Instead of $k1 you have to use $_SESSION['k1'] (and the same goes for k2) just as you did with the 'init' value.
     
    TwistMyArm, Apr 14, 2007 IP
  9. OoteR

    OoteR Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ^^^ what he said.
     
    OoteR, Apr 14, 2007 IP
  10. PresFox

    PresFox Active Member

    Messages:
    218
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #10
    also, you are declaring a add() function, but you arent executing it?

    Read the PHP manual
     
    PresFox, Apr 14, 2007 IP