1

I have this array in PHP session, which I want to step through one step at a time.

How do I get the current array index and move one step forward or backward? Here's the code:

for ($i = 0; $ < 10; $i++){    
    $result_array[] = $i;   
}

$_SESSION['theValue'] = $result_array;

I tried:

$_SESSION['theValue'] [0]++;

to move the array one step at a time, but it only displays the same value at that index. Is there a way I can keep incrementing the index?

I'm not sure I'm making much sense.

1
  • to move the array one step at a time What does this line mean? Commented Oct 23, 2011 at 18:43

3 Answers 3

4

$_SESSION['theValue'] [0]++ actually increments the first value in the array rather than incrementing the array index. Use the array functions, and especially key and next, to navigate through the array.

Or, if you want to keep the last array index in the session, you may want to store the index as a separate session value. The length of the array is read using count($array). The indexes reach from 0 to count - 1.

Sign up to request clarification or add additional context in comments.

Comments

3

If I understand your question correctly that you want to loop through the array then you should do something like this:

$size = count($_SESSION['theValue']);
for($i=0; $i < $size; $i++)
{
    $currentValue = $_SESSION['theValue'][$i];
}

Make sure that size is a local variable and you are not using count() directly in the for loop.

Your method of doing $_SESSION['theValue'][0]++ actually increments the value at the zero index.

Comments

1

init session array

$_SESSION['theValue'] = $result_array;
$_SESSION['currentIndex'] = 0;

create buttons next/previous like this

$prev = $_SESSION['currentIndex'] == 0 ? 0 : $_SESSION['currentIndex'] - 1;
$next = $_SESSION['currentIndex'] == sizeof($_SESSION['theValue']) ? $_SESSION['currentIndex'] : $_SESSION['currentIndex'] + 1;

and on going to next/previous page, simply increment/decrement $_SESSION['currentIndex']


usecase:
So when you create link like this:

<a href="index.php?page=1">Next</a>
<a href="index.php?page=0">Prev</a>

you need to catch the value of $_GET['page'] and save it to currentIndex session property before rendering content.

if(isset($_GET['page'])){
    if(is_numeric($_GET['page']){
        if($_GET['page'] >= 0 && $_GET['page'] <= sizeof($_SESSION['theValue'])){
            $_SESSION['currentIndex'] = $_GET['page'];
        }
    }
}
// place code which works with 'currentIndex' and array in session here...

4 Comments

Thank you for your code, but I just keep getting 1 and 0 when I press the prev and next. It does not increment more than 1. Any thoughts? Thanks again.
and are you saving current value to currentIndex session property? you have to save this variable when page reloads
Thanks again, but how do I save currentIndex and refer to it? Forgive me question.
@HelenNeely I've gave you example to end of my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.