Help with array with no sessions

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
jsnip4
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Mar 07, 2017 7:56 am

Tue Mar 07, 2017 8:08 am

I have the code below I found online and need to modify it but do not know php very well at all.
I need the items in the array to be echo'd one after the other but in order and I do not want the session to be remembered.

Right now, each time I go to the URL of this script, it always starts off with "Ad2". Each time I refresh the page, it will then display Ad3, Ad4, Ad5, Ad1.

What I need is each time the webpage is opened or refreshed, whatever item in the array that is next in line to show up, will show up.
Example,
user1 goes to my webpage: Ad1 is displayed
user2 goes to my webpage: Ad2 is displayed
user3 goes to my webpage: Ad3 is displayed
user1 refreshes webpage: Ad4 is displayed
user3 refreshes webpage: Ad5 is displayed

Code: Select all

<?php

// start session
session_start();
$ads = array ('Ad1', 'Ad2', 'Ad3', 'Ad4', 'Ad5');

// rotate
$id = ++$_SESSION['ad_id'] % count($ads);
$_SESSION['ad_id'] = $id;

// display ad
echo $ads[$id];
?>
If I need to store or get any data from a database, I can set that up but would still need the code to get the data from the table please.
Thanks

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 167
Joined: Wed Jun 15, 2016 8:35 am

Tue Mar 07, 2017 11:53 am

Arrays start numbering at 0 (zero).

If you open the page for the first time there is no Session and $_SESSION['ad_id'] will be unknown.

You then increase the number of $_SESSION['ad_id'] which will be 1.

So the second item in the array will show.

If you do not know the value of a var, echo them before and after the change.

jsnip4
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Mar 07, 2017 7:56 am

Tue Mar 07, 2017 7:10 pm

How will that do what I need it to do? Each person that visits that web page starts off the same way no matter what item in the array should be displayed next.

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Wed Mar 08, 2017 12:02 am

If you want a value to be across all users and pages you will have to store this value in some form of persistent store. This could be a database or a cache (AOC, Memcache). So for example - just have a table with last visit on and the next person retrieves it and updates it. (Not going to go into race conditions and multiple users accessing data at the same time)
SESSION variables are just for a particular user and even that may just be for a period of time rather than anytime that user uses your application.

jsnip4
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Mar 07, 2017 7:56 am

Wed Mar 08, 2017 4:55 am

Would you mind showing me some code to do this?
In other words, could you fix my code to work with the code that gets/sets the info from the database?
Thanks again

(Actually, I think I've got it figured out. I will read the data from the table which contains a single column and if it = Ad1 then we will update it to Ad2 and so on, etc.)

Post Reply