1

I have a session that stores in an array and I was wondering how I would go about if the person visits the same page it doesn't add it to the array and if an array value is empty don't echo the results, this is an example

$SESSION['image'][0] => page1

they visited page1 so it is logged. Now if they go back to the page the array looks like this

$SESSION['image'][0] => page1
$SESSION['image'][1] => page1

Also I echoed out the title and thumbnail image of the last 3 pages visited, but if they have cleared their history or first time visiting the site the images are broken because there is no data in the array. This is what I have tried

if(!empty($_SESSION['image'][1]) {
echo ' <div>$_SESSION[/'image/'][1]</div>
}

I use this to keep the array updating the first 4

if (!isset($_SESSION['image']))
{
$_SESSION['image'] = array();
}
array_unshift($_SESSION['image'], $rentals['Image']);
if (count($_SESSION['image']) > 4)
{
array_pop($rentals['Image']);
}

However is there a better way to just keeping the array at 4 values and dumping the values after 4?

Wow this turned out to a long question, my apologies :-/

2 Answers 2

1

I think array_slice function is what you need to keep array size in bounds. Check this out:

<?php 
$a = array(1);
print_r(array_slice($a, -4));

$a = array(1, 2, 3, 4, 5);
print_r(array_slice($a, -4));
Sign up to request clarification or add additional context in comments.

Comments

0

Check the image array with in_array to make sure the value is not set yet. And don't set anything if the array when it's 4 or larger..

if (!in_array($SESSION['image'], $page) && count($SESSION['image']) <= 4) {
    $SESSION['image'][] = $page;
}

Comments

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.