static variable in xajax?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • afraze
    New Member
    • Aug 2007
    • 18

    static variable in xajax?

    I want to use static variable in xajax but always, it executes only one time.. When i call again, value of static variable isn't changing

    how can i fix the problem? or how can i use static variable in xajax?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, afraze.

    JavaScript doesn't have static variables.

    Instead, you can create a global variable, or if you are using a class that you created, you can declare a private variable in the class' scope and reference it in the method.

    Have a look at this article.

    Comment

    • afraze
      New Member
      • Aug 2007
      • 18

      #3
      hi i just want to do this:

      here is my code sample:
      [PHP]<?php
      //xajax definiton
      //in this part i have a xajax function that name is GetSelectedInde x
      function test()
      {
      static $i = 0;
      $i = $i + 10;
      return $i;
      }//this is php function

      function GetSelectedInde x($selectedInde x)
      {
      ......
      ......

      $abc = test();
      ...
      }

      ?>
      [/PHP]

      So my first try $i equals 10 but if i call again xajax function's GetSelectedInde x
      $i is not counting..

      i think my question is clear now :)

      or should i ask this question in php forum?

      Comment

      • newlearner
        New Member
        • May 2007
        • 67

        #4
        Originally posted by afraze
        hi i just want to do this:

        here is my code sample:
        [PHP]<?php
        //xajax definiton
        //in this part i have a xajax function that name is GetSelectedInde x
        function test()
        {
        static $i = 0;
        $i = $i + 10;
        return $i;
        }//this is php function

        function GetSelectedInde x($selectedInde x)
        {
        ......
        ......

        $abc = test();
        ...
        }

        ?>
        [/PHP]

        So my first try $i equals 10 but if i call again xajax function's GetSelectedInde x
        $i is not counting..

        i think my question is clear now :)

        or should i ask this question in php forum?
        Hi,
        When ever the test function is called i is initialised to zero and then its incremented to ten.

        In the first call i is zero and u can see i is incremented.
        In the second call i is already 10 and when u call test.. I is set to 0 and again incremented to 10 so u cant feel the change..

        kind regards..

        Comment

        • afraze
          New Member
          • Aug 2007
          • 18

          #5
          newlearner,

          i know but $i is a static variable, so when i try to use this function( test() ) in a php file.. It counts 10 by 10...

          The problem is that it is not counting with xajax tech..

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, afraze.

            When you load the script, $i gets reinitialized. The only way to save the value of $i in this case is to use a $_SESSION variable.

            Comment

            • afraze
              New Member
              • Aug 2007
              • 18

              #7
              pbmods,

              thnks so much, i tried $_SESSION variable and, i got it. :)

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, afraze.

                Originally posted by afraze
                pbmods,

                thnks so much, i tried $_SESSION variable and, i got it. :)
                Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

                Comment

                Working...