Hi all

I am looking to use an associative array to store the following:

key = Section Name
value = Script name

For example, here is my array:

$sections = array(
	'Section 1' 	=> 'section1',
	'Section 2' 	=> 'section2',
	'Section 3' 	=> 'section3',
	'Section 4' 	=> 'section4',
	'Section 5' 	=> 'section5',
	'Section 6' 	=> 'section6',
	'Section 7'		=> 'section7',
	'Section 8'		=> 'section8',
	'Section 9'		=> 'section9'
);

Now, what I want to do is to be able to:

a) if the current session variable is not set, set it to be the first element of the array
b) if I press either of my next or previous buttons, get the next() or previous() key/value from the array based on my current() position (which I store in a session array as above)

The problem I have is I am failing at point 'A' above!

I have the following code:

if (!isset($_SESSION['S_CURRENT_SECTION'])) {
	$_SESSION['S_CURRENT_SECTION'] = current($sections);
}

echo "Current: " . $_SESSION['S_CURRENT_SECTION'];

But this does not echo anything - any reason why?

p.s. I have session_start() at the top of my page.

Thanks for reading

    maybe $_SESSION['S_CURRENT_SECTION'] is set already by an earlier call to the script and is empty?

    run:

    session_start();
    $_SESSION = array();
    session_destroy();
    

    and try again.

    or maybe the pointer of your array is out of bounds, because you had a foreach running over the array? in this case

    reset($secions);
    

    or

    prev($sections);
    

    will produce a result.

    Bjom

      The array pointer (that current, next, prev, reset, and end work with) isn't saved as part of the session. When the session array is loaded, the pointer is reset.

        Of course not, but for the code fragment provided above, these are the two reasons why $_SESSION['S_CURRENT_SECTION']) could be empty: 1.it has been set to a value that produces an "empty" echo like '' or false before and got saved in the session file, thus preventing the part inside the if-statement getting executed, 2. current() returns nothing which would be the case if somewhere above the provided code fragment the pointer had been set out of bounds (for example by a foreach).

        for completeness: reason 3 out of 2 would be that $sessions is empty 😉

          Or something else: if "it does not echo anything" is accurate, then it's not echoing "Current: " either.

            That's quite true. Now where is the original poster? CONDOUG WHERE ART THOU??!? :p

              I am present bjom!

              The value is now showing and I can only assume I had another instance of the session running in a different tab of my browser (??) Such a stupid mistake if that was the case. Thank you for your suggestions.

              However, this leads me onto the second point of my original post.

              If I store the current() element of the array into a hidden field and then POST the form (when I click my next or previous button), depending on what the value is of that hidden field, how would I:

              a) look in the array to find the value i have POSTed
              b) and then get the next() or prev() value (which would be based on which button I press).

              I do not need advice on how to programme the buttons, I just need to know what the PHP code would be to search and find the next or previous element of the array based on the current value i have posted from my hidden field.

              I hope that makes sense.

              Thanks for reading

                Why store/pass the value and not the key instead?

                If you need to store/pass the value use array_search. While you are in the array section of the manual...explore...PHP has incredible array functions.

                  Hi Bjom

                  To be honest, I actually don't care what I pass back to the page, I just need to know how I would take that passed value and look through the array to find a match, and when a match is found, move to the next or prevous item in the array and stores its key/value to a session variable.

                  Can you help on this?

                    if you loop through the array until the current is found (by key or value) then when current is found, you can use next() or prev() as at this point in execution current() will be... current. make sense?

                      Write a Reply...