lundi 20 février 2017

Laravel Sessions: on page reload (refresh) display the other group of images

I want to create a simple app that displays a group of images (banners), and when a page is reloaded - it should display the other group of images.

Each image has a property called order which is an integer. For example, when you open the home page for the first time, three images with order values in [1, 3, 5] should be displayed. After the page reload, then the images with order values in [2, 4, 6] should be displayed. And if you re-open the page again - then the images with order values in [1, 2, 3] should be displayed again. And so on...

So, basically, images with order values in [1, 3, 5] and [2, 4, 6] should toggle on every page reload.

In my controller I've tried the following:

public function index() {

    if (!\Session::has('top_order')) {
        $top_banners = TopBanner::where('position', 'top')
            ->whereIn('order', [1, 3, 5])
            ->orderBy('order')
            ->get();
        \Session::put('top_order', [2, 4, 6]);
    } else {
        $next_orders = \Session::get('top_order');

        $top_banners = TopBanner::where('position', 'top')
            ->whereIn('order', $next_orders)
            ->orderBy('order')
            ->get();

        if ($next_orders[0] == 1) {
            for ($i = 0; $i < count($next_orders); $i++) {
                $next_orders[$i]++;
            }
        } else {
            for ($i = 0; $i < count($next_orders); $i++) {
                $next_orders[$i]--;
            }
        }

        \Session::put('top_order', $next_orders);
    }

    return view('welcome')->with('top_banners', $top_banners);   
}

But for some reason, the images are not toggled. As you can see in the else block, when a page is reloaded (when an HTTP request is sent and the index method in the controller is executed) - first I get from the 'top_order' session the next group of images that should be displayed:

$next_orders = \Session::get('top_order');

... and then I want to update the 'top_order' session - I want to set its value to the other order group. So, if the current value of the 'top_order' session is [1, 3, 5] - then update it to [2, 4, 6]:

        if ($next_orders[0] == 1) {
            for ($i = 0; $i < count($next_orders); $i++) {
                $next_orders[$i]++;
            }
        }

... and if the current value of the 'top_order' session is [2, 4, 6] - then update it to [1, 3, 5]:

        else {
            for ($i = 0; $i < count($next_orders); $i++) {
                $next_orders[$i]--;
            }
        }

Finally, save:

\Session::put('top_order', $next_orders);

But it doesn't work and I can't see what am I doing wrong. Once the 'top_order' session is set to [2, 4, 6] - it stay there, only that group of images will be displayed no matter how many times I refresh the page.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lndztN
via IFTTT

Aucun commentaire:

Enregistrer un commentaire