HI There:

I have a button which each time is pressed increments the value of a variable by one, I want to use the value of this variable to index elements in an array:

$array[$var] = "what ever"

this is what happens when I press the button and I view
print_r($array)

Array ( [0] => product [5] => whatever )

I intitialized $array[0] to "product" and pressed 5 times the add one button in this example. The second element of the array index changes but the ones before (1,2,3,4 and so on) dissapear so basicaly I am not building the arrray (it should now have 6 entries)

I also tried array_push with the same result

does somebody know what I am doing wrong?

thanks

    Originally posted by soundobj
    does somebody know what I am doing wrong?

    you don't know that variables in php will not be saved afer the generated page has been sent to the browser

    search the manual for sessions

      HI there,

      from what I gathered It is ok to have $_SESSION variables but what about arrays?

        from what I gathered It is ok to have $_SESSION variables but what about arrays?

        You can save arrays as well as scaler variables in the session.

          here is the code anyway, I know is daed easy but Icannot get my head round the bloddy thing : )

          i named it sum.php

          <?php

          session_start();

          print_r($_SESSION);

          if(!$_POST){

          $day = 0;

          $stack= array();

          }else

          $stack = $SESSION['get'];

          $day = $_POST['day_on'];

          echo '<FORM ACTION="sum.php" METHOD="post">';

          if($_POST){

          $stack[$day]="";

          $day = $day + 1;

          }

          echo $day;

          echo '<INPUT TYPE="hidden" VALUE='.$day.' name="day_on" onclick="return OnButton2();"><BR />';

          echo '<INPUT TYPE="submit" VALUE="add one" name="day" onclick="return OnButton2();"><BR />';

          $stack[$day] = $day;

          $SESSION['get']= $stack;

          print_r($SESSION['get']);

          ?>

            I finally got it working , I had some synthax error in $SESSION rather than $_SESSION (bloody subtleness) anyway here is the working code:

            <?php

            session_start();

            print_r($_SESSION);

            if(!$_POST){

            $day = 0;

            $stack= array();

            }else

            $stack = $_SESSION['get'];

            $day = $_POST['day_on'];

            echo '<FORM ACTION="sum.php" METHOD="post">';

            if($_POST){

            //$stack[$day]="";

            $day = $day + 1;

            }

            echo $day;

            echo '<INPUT TYPE="hidden" VALUE='.$day.' name="day_on" onclick="return OnButton2();"><BR />';

            echo '<INPUT TYPE="submit" VALUE="add one" name="day" onclick="return OnButton2();"><BR />';

            $stack[$day] = $day;

            $_SESSION['get']= $stack;

            print_r($_SESSION['get']);

            ?>

            thanks a lot for youer input all

              Write a Reply...